构建 Springboot 的应用程序并提供 Sonar Report 时,在评估上下文负载的 Springboot 默认单元测试中会引发标记为“Blocker”的代码味道:
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(){}
}
如何解决这个问题?
我做了一项研究,得到了快速的解决方案来运行测试并满足声纳的要求:
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(){}
}
我只是简单地包括了
@Test(expected = Test.None.class)
对于 JUnit5,这可以工作,尽管它不是一个好看的解决方案:
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
});
}
}