Junit - 未注入弹簧自动装配的字段

时间:2014-11-11 16:28:36

标签: java spring spring-mvc junit

我有以下测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/application-context.xml" })
public class TestSanity {

   @Test
   public void testSanity() {

      new AnotherTestClass().simpleTest();
   }
}

问题是AnotherTestClass autowire字段都是null(spring上下文没有加载)。这是AnotherTestClass:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/application-context.xml" })
public class AnotherTestClass {

  @Autowired
  private PlayerService playerService;
  @Autowired
  private IPlayerSessionService playerSessionService;
  @Autowired
  private PlayerRepository playerRepository;


  @Test
  public void simpleTest() {

       ...
  }

  @Test
  public void complexAndLongTest() {

       ...
  }

事情是我测试时,AnotherTestClass中的两个测试都运行得很好。 如何让autowire字段被注入?

由于

0 个答案:

没有答案