Search code examples
spring-bootunit-testingsonarqubesonarcloud

SonarCloud raising a "Blocker" on Springboot's contextLoads unit test


When building Springboot's application and providing Sonar Report a code smell tagged as "Blocker" is raised on Springboot default unit test that evaluates the context load:

    package nz.co.datacom.oi.processor;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class ProcessorApplicationTest {
    @Test
    public void contextLoads(){}
}

How to solve that issue?


Solution

  • I did a research and got that quick solution to have the test running and satisfy Sonar:

    package nz.co.datacom.oi.processor;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ActiveProfiles("test")
    public class ProcessorApplicationTest {
        @Test(expected = Test.None.class)
        public void contextLoads(){}
    }
    

    I simply included the @Test(expected = Test.None.class)