java SpringJUnit4ClassRunner 为每个测试初始化 bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1564238/
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
SpringJUnit4ClassRunner initialize beans for each test?
提问by harschware
The following test illustrates that this test bean is initialized twice by Spring. I'm hoping someone can tell me why this is so, since it should only be once. Here's the test:
下面的测试说明这个测试 bean 被 Spring 初始化了两次。我希望有人能告诉我为什么会这样,因为它应该只有一次。这是测试:
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {} )
public class TestAfterPropsSet implements InitializingBean {
private static final Logger logger = Logger.getLogger(TestAfterPropsSet.class);
@Test
public void test1() {
logger.debug("Test1");
}
@Test
public void test2() {
logger.debug("Test2");
}
public void afterPropertiesSet() throws Exception {
logger.debug("Bean Initialized");
}
} // end class
Here's the bean file:
这是bean文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
and here's the output:
这是输出:
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 26] DEBUG - Bean Initialized
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 17] DEBUG - Test1
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 26] DEBUG - Bean Initialized
2009-10-13 21:20:04,393 [TestAfterPropsSet.java 22] DEBUG - Test2
回答by Droo
It's not a Spring convention. You should be following JUnit conventions, i.e. suite-wide initialization or deconstruction should be done in @BeforeClass and @AfterClass accordingly, or you can use @Autowire and let Spring handle the object's scope.
这不是春季会议。您应该遵循 JUnit 约定,即套件范围的初始化或解构应该在 @BeforeClass 和 @AfterClass 中相应地完成,或者您可以使用 @Autowire 并让 Spring 处理对象的范围。
A new suite will be constructed for each test. This is more apparent in JUnit3 where you had to create a new suite using a specified test name.
将为每个测试构建一个新套件。这在 JUnit3 中更为明显,您必须使用指定的测试名称创建新套件。
Take a look at the JavaDoc:
看看JavaDoc:
The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method.Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.
Test 注释告诉 JUnit,它所附加的 public void 方法可以作为测试用例运行。为了运行该方法,JUnit 首先构造该类的一个新实例,然后调用带注释的方法。测试抛出的任何异常都会被 JUnit 报告为失败。如果没有抛出异常,则假定测试成功。
Your use case is a bit puzzling since your test isn't actually doing anything and there is no bean, which you reference. By default, Spring beans are declared with the default scope="singleton" attribute, so had you actually declared a bean, it would have been a cached singleton. However, this has nothing to do with method execution.
您的用例有点令人费解,因为您的测试实际上没有做任何事情,并且没有您引用的 bean。默认情况下,Spring bean 是用默认的 scope="singleton" 属性声明的,所以如果你真的声明了一个 bean,它就会是一个缓存的单例。但是,这与方法执行无关。

