spring3注解与main方法

发布于 2024-12-04 09:10:50 字数 1031 浏览 0 评论 0原文

我有以下课程:

@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 方法)运行,那么它会给出空指针异常 loginDetailsS​​ervice.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任谁 2024-12-11 09:10:50

当您“将代码作为 junit 测试运行”时,您仍然需要执行 JUnit 所做的操作来引导您的应用程序上下文和依赖项注入:

public static void main(String[] args) {
    org.junit.runner.JUnitCore.run(LoginDetailsTest.class);
}

You still need to do what JUnit does when you "run code as junit test" to bootstrap your application context and dependency injection:

public static void main(String[] args) {
    org.junit.runner.JUnitCore.run(LoginDetailsTest.class);
}
紫罗兰の梦幻 2024-12-11 09:10:50

main 是另一回事。因为通过 new LoginDetailsTest() 实例化该类,它不是由 Spring 构建的 - 不可能有依赖注入。

您需要做的是:

  • 为导入 Home-ctx.xmlmain 方法创建一个新的应用程序上下文 appctx.xml 并声明一个new bean
  • main 方法中获取 bean 的实例并调用testLoginDetails() 像这样:

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("appctx.xml");  
        LoginDetailsTest loginDetailsTest = (LoginDetailsTest) context.getBean("loginDetailsTest");
        登录详细信息测试.testLoginDetails();
    }
    

一般来说,您应该将 JUnit 测试与 main 分开方法和业务逻辑。

The mainis a different thing. Because by instantiating the class by new LoginDetailsTest() it is not build by Spring - there can be no dependency injection.

What you need to do is:

  • make a new application context appctx.xml for your main method that imports Home-ctx.xml and declare a new bean <bean id="loginDetailsTest" class="LoginDetailsTest"/>
  • in your main method get an instance of the bean and call testLoginDetails() like this:

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("appctx.xml");  
        LoginDetailsTest loginDetailsTest = (LoginDetailsTest) context.getBean("loginDetailsTest");
        loginDetailsTest.testLoginDetails();
    }
    

In general you should separate the JUnit test, main method and business logic.

殤城〤 2024-12-11 09:10:50

首先,您的测试工作的原因是(SpringJUnit4ClassRunner),它做了很多事情,但为了简单起见,它引导 spring 容器并注入您在上下文文件(Home-ctx.xml)中定义的所有依赖项,包括您注入到测试用例中的一个。有关更多详细信息,请查看这些类

  1. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/TestContextManager.html
  2. http://static.springsource .org/spring/docs/2.5.x/api/org/springframework/test/context/support/DependencyInjectionTestExecutionListener.html

要解决 main 方法的问题,您必须加载spring 上下文你自己并注入依赖关系像这样

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/com/home/app/Home-ctx.xml");

 new LoginDetailsTest().loginDetailsService = (LoginDetailsService) ctx.getBean(LoginDetailsService.class); 

 //now your main method should work
 new LoginDetailsTest().testLoginDetails();

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

  1. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/TestContextManager.html
  2. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/support/DependencyInjectionTestExecutionListener.html

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

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/com/home/app/Home-ctx.xml");

 new LoginDetailsTest().loginDetailsService = (LoginDetailsService) ctx.getBean(LoginDetailsService.class); 

 //now your main method should work
 new LoginDetailsTest().testLoginDetails();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文