【发布时间】:2013-10-10 12:40:00
【问题描述】:
我想测试一个通过@Autowired 注解注入资源的类。
class TestedClass{
@Autowired
private MyResource resource
...
}
如何在不修改测试类的情况下使这种注入工作?注入在测试类中工作正常,但在测试类中不行:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= "classpath*:/bean-definition.xml")
public class TestedClassTest {
TestedClass instance;
//This works
@Autowired
private MyResource resource
@Before
public void setUp() throws Exception {
instance = new TestedClass();
}
...
}
那么问题可能是如何将上下文传递给被测试的类?
【问题讨论】:
标签: java spring junit dependency-injection