SpringJUnit4ClassRunner initialize beans for each test?

扶醉桌前 提交于 2019-12-05 22:17:09

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.

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.

Take a look at the 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!