0

I am using nested classes for writing unit test cases in spring boot. My test case file is :

@RunWith(NestedRunner.class)
@SpringBootTest
@TestInstance
@AutoConfigureMockMvc
public class OuterTestCase {

        public static class InnerTestCases {

             @Autowired
             private RestTemplate restTemplate;
             @Autowired
             private MockMvc mockMvc;

             @Test
             public void testcase(){
              //do Something
             }
        }
}

But I am getting NPE when tried using RestTemplate inside testCase method.

I am unable to initialize beans inside the inner class.

  • Does this answer your question? [@Autowired not working in inner class](https://stackoverflow.com/questions/28720329/autowired-not-working-in-inner-class) – Randy Casburn Dec 19 '20 at 15:42
  • Basically have to annotate the inner static class as some sort of bean - perhaps @Component. Otherwise you have to manage the dependencies yourself. – Randy Casburn Dec 19 '20 at 15:45
  • adding component bean also doesn't work. @RandyCasburn And also I checked the above link but that too didn't answered my question. – Master Developer Dec 19 '20 at 16:49

1 Answers1

0

A static class gets initialized during early stages of class loading. Unless the Autowired class has been initialized using a static class you will encounter above error.

vishnu g
  • 99
  • 4