What is the use of contextLoads method in Spring Boot Junit Testcases?

Thiagarajan Ramanathan picture Thiagarajan Ramanathan · Apr 17, 2018 · Viewed 9.5k times · Source

This method is empty in all my JUnit test cases. What is the use of this method?

Sonarqube is complaining
"Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation."

I can bypass this by adding some comment but I just want to know why it is necessary.

Answer

ozinal picture ozinal · Jan 31, 2019

Use different runner,

if you are using SpringRunner.class, use an alternative one such as MockitoJUnitRunner.class or MockitoJunitRunner.class rather then SpringRunner.class

@Runwith(SpringRunner.class)
 @Runwith(JUnit4ClassRunner.class)
 @Runwith(MockitoJUnit4Runner.class)
 @Runwith(MockitoJUnitRunner.class)