0

I have the following test class:

@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();
   }
}

Problem is AnotherTestClass autowire fields are all null (the spring context didnt load). here is 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() {

       ...
  }

Thing is both tests in AnotherTestClass runs perfectly when i test. How can i make the autowire fields to get injected?

Thanks

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Urbanleg
  • 6,252
  • 16
  • 76
  • 139
  • This `new AnotherTestClass()` is not a Spring managed bean. – Sotirios Delimanolis Nov 11 '14 at 16:29
  • This is not a duplicate since this question was raised in the context of Junit tests and not a simple spring configuration. – Urbanleg Nov 11 '14 at 16:31
  • Is the question _How can i make the autowire fields to get injected?_. The answer would be to make it a Spring bean. As for the `@Test`s, that's a whole different question. The test class is handled by JUnit and the given `Runner`. Invoking a `@Test` annotated method is not the same as having JUnit invoke a `@Test` annotated method. – Sotirios Delimanolis Nov 11 '14 at 16:33
  • In other words, its not possible to invoke a test of one class from another. I see. Thanks – Urbanleg Nov 11 '14 at 16:34
  • There might be a JUnit way to do it. But I don't think simply calling the method directly will achieve what you seem to want. If you'd like to edit your question to address that, I'll re-open. Currently, I feel the question is about why the fields are `null`. – Sotirios Delimanolis Nov 11 '14 at 16:35
  • OK, I will research for the right way, Thanks! – Urbanleg Nov 11 '14 at 16:36
  • Yup, duplicate at this point. `AnotherTestClass` isn't managed, and it would be rather weird to inject a test case into another. – chrylis -cautiouslyoptimistic- Nov 11 '14 at 16:48

0 Answers0