Tom*_*icz 21
你有没有学过Spring参考文档中的测试章节?这是一个你应该开始的例子:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {
@Resource
private FooService fooService;
// class body...
}
Run Code Online (Sandbox Code Playgroud)
如果你com.example.MyTest在/src/test/java,你将需要/src/test/resources/com/example/MyTest-context.xml- 但例外将告诉你的方式.
aba*_*ogh 12
这是一种可能性:
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "/applicationContext.xml"
// in the root of the classpath
@ContextConfiguration({"/applicationContext.xml"})
public class MyTest {
// class body...
}
Run Code Online (Sandbox Code Playgroud)
通常,为测试基础架构提供test-applicationContext是个好主意.
您应该SpringJUnit4ClassRunner在测试类上使用,并在包含该bean的测试类中的字段上使用@Resource(或@Autowired).您应该考虑使用特殊的测试上下文Spring配置,它使用存根,以便您的测试是真正的单元测试,而不是依赖于整个应用程序上下文.