java @Autowired 与 JUnit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26462926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:58:25  来源:igfitidea点击:

@Autowired with JUnit tests

javaspringjunitautowired

提问by bigster

I′ve using JUnit but some tests have some issues, these tests have @Autowired annotation inside the Spring beans and when i reference them the beans that are @Autowired were always NULL.

我一直在使用 JUnit 但有些测试有一些问题,这些测试在 Spring bean 中有 @Autowired 注释,当我引用它们时,@Autowired 的 bean 总是 NULL。

Here is the example code:

这是示例代码:

     public class Test {

                 protected ApplicationContext ac;

                 @Before
                 public void setUp() {
                     ac = new FileSystemXmlApplicationContext("classpath:config/applicationContext.xml"
                     "classpath:config/test-datasources.xml");
                 }

                 @Test
                 public void testRun() throws Exception {
                        IManager manager =  (IManager)this.ac.getBean("manager");
                        manager.doSomething();
                 }
    }

    @Service
    public class Manager implements IManager {

            public boolean doSomething() throws Exception {
                 ParametersJCSCache parametersJCSCache = new ParametersJCSCache();
                 String paramValue = parametersJCSCache.getParameter("XPTO");
                 ...
            }
    }

    public class ParametersJCSCache extends SpringBeanAutowiringSupport {

          @Autowired
          private IParameterManager parameterManager;  //THIS IS NULL
    }

When invoking the Manager object the Spring generates the proxy but when accessing the @Autowired parameterManager the object is null, and with this issue i can′t test this method.

当调用 Manager 对象时,Spring 会生成代理,但是在访问 @Autowired parameterManager 时,对象为空,因此我无法测试此方法。

Any idea what is causing this? Why does the object not get injected? It works well in the context of a web application, but in the test context is always NULL.

知道是什么原因造成的吗?为什么对象没有被注入?它在 Web 应用程序的上下文中运行良好,但在测试上下文中始终为 NULL。

采纳答案by bigster

I've finally found a solution to my problem. Find this post SpringBeanAutowiringSupport does not inject beans in jUnit testsand do something like this the JUnit tests works.

我终于找到了解决我的问题的方法。找到这篇文章SpringBeanAutowiringSupport 不会在 jUnit 测试中注入 bean并执行类似 JUnit 测试的操作。

回答by Niels Bech Nielsen

Try running your unit tests with a spring runner instead.

尝试使用 spring runner 运行单元测试。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/applicationContext.xml",
                 "classpath:config/test-datasources.xml"})
public class MyTest {

   @Autowired private IManager manager;

   @Test public void someTest() {
     assertNotNull(manager);
   }
}

It will load the config only once and when needed, and it will autowire your junit class

它只会在需要时加载一次配置,并且会自动装配您的 junit 类

回答by Alan Hay

Looks to me like ParametersJCSCache is not a Spring managed bean. You need to make this a Spring bean and autowire it into the Manager

在我看来,ParametersJCSCache 不是 Spring 托管的 bean。您需要将其设为 Spring bean 并将其自动装配到 Manager 中

@Service
        public class Manager implements IManager {

                public boolean doSomething() throws Exception {
                     ParametersJCSCache parametersJCSCache = new ParametersJCSCache(); <-- You create a new (non Spring-managed) instance
                     String paramValue = parametersJCSCache.getParameter("XPTO");
                     ...
                }
        }

Change to:

改成:

@Service
public class Manager implements IManager {

      @Autowired
      private ParametersJCSCache  parametersJCSCache; 

        public boolean doSomething() throws Exception {
             String paramValue = parametersJCSCache.getParameter("XPTO");
             ...
        }
}

@Component
public class ParametersJCSCache extends JCSCache {

      @Autowired
      private IParameterManager parameterManager;  //THIS IS NULL
}