从JUnit测试中访问spring上下文

时间:2016-06-19 17:28:01

标签: spring testing junit

我尝试运行spring框架的测试。我只使用xml上下文。在test(JUnit)类中,我指定了

mm n = new mm(); 
mm.MessageHandler messageHandler = n.new MessageHandler();

但我不知道如何在@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"springContext.xml"}) public class MyTest extends TestCase { @Test public void test() { } } 方法中访问spring上下文?我不能使用@Test注释,因为我没有使用注释驱动的上下文

1 个答案:

答案 0 :(得分:2)

测试类就像一个普通的bean。到目前为止,您可以在测试类中使用这两个:

  • autowire ApplicationContext
  • 实施ContextAware接口

请参阅How to inject ApplicationContext itself