Posts

Showing posts from July, 2023

How to Test Application Context in Spring Boot

 Usually, we won't bother about the Junit test for the application context and bean instantiations, blindly trust about the stability of the spring boot framework. And spring boot generates automatically a test class with a method contextLoads() @Test void contextLoads () { } To check the beans are launching properly or not we can frame springboot application like below  @SpringBootApplication public class PracticeApplication { public static void main (String[] args) { ConfigurableApplicationContext ctx=SpringApplication. run (PracticeApplication. class , args); printBeanNames (ctx); } Lets deal with the context object  private static void printBeanNames (ApplicationContext ctx) { String[] beanDefinitionNames = ctx.getBeanDefinitionNames(); for (String bean:beanDefinitionNames) System. out .println( "BeanNAme is " +bean); int beanDefinitionCount = ctx.getBeanDefinitionCount(); System. out .println( "beanDefinitionCount...