0

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?

rod.dinis
  • 1,233
  • 2
  • 11
  • 20

2 Answers2

1

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)

rod.dinis
  • 1,233
  • 2
  • 11
  • 20
0

For JUnit5 this would work, even though it is not a nice looking solution:

package org.example;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@SpringBootTest
class ExampleApplicationIT {

    @Test
    void contextLoads() {
        assertDoesNotThrow(() -> {
            // if context was successfully loaded, this block will be executed
        });
    }
}
Mikka
  • 21
  • 3