【发布时间】:2019-06-28 20:02:33
【问题描述】:
为了在测试类中获取要测试的类的实例,最佳实践是什么?
用
ClassToBeTested classToBeTested = new ClassToBeTested();
在测试类中
或
从 Spring 上下文中获取它,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {
@Autowired
MyService service;
...
}
【问题讨论】:
-
它有依赖关系吗?如果是这样,第二种情况将为您提供它们。如果没有,或者您愿意自己提供它们,两者都可以。
-
感谢您的快速接受,很高兴您发现答案对您有帮助。
标签: java spring unit-testing junit dependency-injection