spring3注解与main方法
我有以下课程:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/com/home/app/Home-ctx.xml"})
public class LoginDetailsTest {
public static void main(String[] args) {
new LoginDetailsTest().testLoginDetails();
}
@Inject
@Named(HomeConstants.loginDetailsService)
private LoginDetailsService loginDetailsService;
private List<UserLogin> loginDetails;
@Test
public void testLoginDetails() {
UserLogin login = new UserLogin();
login.setLoginName("test");
login.setLoginPassword("test123");
loginDetails = loginDetailsService.loginDetails(login);
for (UserLogin loginDet : loginDetails) {
System.out.println(loginDet.getLoginName());
System.out.println(loginDet.getLoginPassword());
}
}
}
如果我将上面的代码作为 junit 测试运行,那么它会给出预期的结果。 如果我作为 Java 应用程序(即 main 方法)运行,那么它会给出空指针异常 loginDetailsService.loginDetails(登录)。如何作为 main 方法运行而不出错?
I have following class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/com/home/app/Home-ctx.xml"})
public class LoginDetailsTest {
public static void main(String[] args) {
new LoginDetailsTest().testLoginDetails();
}
@Inject
@Named(HomeConstants.loginDetailsService)
private LoginDetailsService loginDetailsService;
private List<UserLogin> loginDetails;
@Test
public void testLoginDetails() {
UserLogin login = new UserLogin();
login.setLoginName("test");
login.setLoginPassword("test123");
loginDetails = loginDetailsService.loginDetails(login);
for (UserLogin loginDet : loginDetails) {
System.out.println(loginDet.getLoginName());
System.out.println(loginDet.getLoginPassword());
}
}
}
if i run above code as junit test, then it gives expected result.
If I run as Java application ie main method, then it gives null pointer exception for
loginDetailsService.loginDetails(login). how can run as main method without error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您“将代码作为 junit 测试运行”时,您仍然需要执行 JUnit 所做的操作来引导您的应用程序上下文和依赖项注入:
You still need to do what JUnit does when you "run code as junit test" to bootstrap your application context and dependency injection:
main
是另一回事。因为通过new LoginDetailsTest()
实例化该类,它不是由 Spring 构建的 - 不可能有依赖注入。您需要做的是:
Home-ctx.xml
的main
方法创建一个新的应用程序上下文appctx.xml
并声明一个new bean在
main
方法中获取 bean 的实例并调用testLoginDetails()
像这样:一般来说,您应该将 JUnit 测试与 main 分开方法和业务逻辑。
The
main
is a different thing. Because by instantiating the class bynew LoginDetailsTest()
it is not build by Spring - there can be no dependency injection.What you need to do is:
appctx.xml
for yourmain
method that importsHome-ctx.xml
and declare a new bean<bean id="loginDetailsTest" class="LoginDetailsTest"/>
in your
main
method get an instance of the bean and calltestLoginDetails()
like this:In general you should separate the JUnit test, main method and business logic.
首先,您的测试工作的原因是(SpringJUnit4ClassRunner),它做了很多事情,但为了简单起见,它引导 spring 容器并注入您在上下文文件(Home-ctx.xml)中定义的所有依赖项,包括您注入到测试用例中的一个。有关更多详细信息,请查看这些类
要解决 main 方法的问题,您必须加载spring 上下文你自己并注入依赖关系像这样
First things first the reason your test works is (SpringJUnit4ClassRunner), It does a lot but to keep it simple it boot straps the spring container and injects all the dependencies like that you defined in your context file (Home-ctx.xml) including the one you inject into the test case. For more details look at these classes
To solve the problem with your main method, You have to load the spring context your self and inject the dependency some thing like this