Hope this answers your question
Here webApplicationContext is initialised whenever we try to execute a test case as @Before get initiated which calls setUp() of AbstractTest class which has initialisation logic of webApplicationContext
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)
@WebAppConfiguration
public abstract class AbstractTest {
protected MockMvc mvc;
@Autowired
WebApplicationContext webApplicationContext;
protected void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
protected String mapToJson(Object obj) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
protected <T> T mapFromJson(String json, Class<T> clazz)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, clazz);
}
}
public class UserControllerTest extends AbstractTest {
@Override
@Before
public void setUp() {
super.setUp();
}
@Test
public void testGet() throws Exception {
String uri = “/url”;
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri)
.accept(MediaType.APPLICATION_JSON_VALUE)).andReturn();
int status = mvcResult.getResponse().getStatus();
assertEquals(200, status);
}
}
Main Spring boot class
@SpringBootApplication
public class Main extends SpringBootServletInitializer{
private Logger LOGGER = (Logger) LoggerFactory.getLogger(FactsMain.class);
@Value("${facts.trustCertPath}")
private String trustCertPath;
public static void main(String args[]) {
SpringApplication.run(Main.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(FactsMain.class);
}
@PostConstruct
public void setSSLPath() {
LOGGER.info("trustCertPath - " + trustCertPath);
System.setProperty("javax.net.ssl.trustStore", trustCertPath);
}
}