1

I have a logic in my application class in a Spring Boot app, but I don't know how to do an unit and an integration test to cover it.

Here is the code.

 @SpringBootApplication public class MlgApplication { public static void main(String[] args) throws IOException { ConfigurableApplicationContext run = SpringApplication.run(MlgApplication.class, args); ListBean ListBean = run.getBean(ListBean.class); ListBean.createList(); } } 

It's a command line application that runs with 'java -jar mlg.jar'

2 Answers 2

1

If you are using Spring initializr, this test will be created for you. You may call it an integration test because it will try to start your application context (thus integrating all classes inisde it). It goes something like this:

@RunWith(SpringRunner.class) @SpringBootTest public class BootApplicationTests { @Test public void contextLoads() { // some more optional integration assertions here // like asserting number of beans, are they null, etc... } } 

And for your specific domain logic, you can try to assert if the list is created but I would put that in a separate class as a unit test.

Sign up to request clarification or add additional context in comments.

Comments

0

I managed to do it this way, using mockito-core 3.8 and mockito-inline BUT was not able to get Jacoco coverage doing it this way:

@SpringBootTest @ActiveProfiles("test") public class AutowireTest { private static final String ARG = ""; private static final String[] ARGS = new String[]{ARG}; @Autowired ConfigurableApplicationContext context; @Test //Junit5 public void main() { try (MockedStatic<Application> appStatic = Mockito.mockStatic(Application.class); MockedStatic<SpringApplication> springStatic = Mockito.mockStatic( SpringApplication.class)) { appStatic.when(() -> Application.main(ARGS)) .thenCallRealMethod(); springStatic.when(() -> SpringApplication.run(Application.class, ARGS)) .thenReturn(context); // when Application.main(ARGS); //then appStatic.verify(times(1), () -> Application.main(ARGS)); springStatic.verify(times(1), () -> SpringApplication.run(Application.class, ARGS)); } } } 

So, I am asking why here: How to Unit test Spring-Boot Application main() method to get Jacoco test coverage

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.