简体   繁体   中英

How to unit test spring bean definitions?

I have defined several spring beans via @Bean .

@Bean
void SomeClass someClass() {
return new SomeClass();
}

But these are not covered by unit tests as I use @Mock to mock the classes, and beans are not instantiated in unit tests.

Now test coverage is failing because spring bean definition lines are not covered, is there any way to fix it?

We use Jacoco coverage.

Note: I am not using @Configuration, rather passing the class to Application context

@Configuration files are normally excluded from code-coverage tests by rule. Running the application with @SpringBootTest (if you're using Boot) may also cause these to be marked as executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM