简体   繁体   English

SpringJUnit4ClassRunner是否为每个测试初始化​​bean?

[英]SpringJUnit4ClassRunner initialize beans for each test?

The following test illustrates that this test bean is initialized twice by Spring. 以下测试说明该测试bean由Spring初始化了两次。 I'm hoping someone can tell me why this is so, since it should only be once. 我希望有人可以告诉我为什么会这样,因为它应该只有一次。 Here's the test: 这是测试:

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

It's not a Spring convention. 这不是Spring约定。 You should be following JUnit conventions, ie 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中更加明显,在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. Test注释告诉JUnit,可以将其附加到的public void方法作为测试用例运行。 To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. 为了运行该方法,JUnit首先构造一个新的类实例,然后调用带注释的方法。 Any exceptions thrown by the test will be reported by JUnit as a failure. 测试抛出的任何异常将由JUnit报告为失败。 If no exceptions are thrown, the test is assumed to have succeeded. 如果未引发任何异常,则假定测试成功。

Your use case is a bit puzzling since your test isn't actually doing anything and there is no bean, which you reference. 您的用例有点令人费解,因为您的测试实际上并没有做任何事情,并且您没有引用任何bean。 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. 默认情况下,Spring Bean是使用默认的scope =“ singleton”属性声明的,因此,如果您实际上声明了Bean,它将是已缓存的单例。 However, this has nothing to do with method execution. 但是,这与方法执行无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM