Initialize beans in inner Static test class in springBoot

381 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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.