首页 > 解决方案 > SonarCloud 在 Springboot 的 contextLoads 单元测试中引发“阻止程序”

问题描述

在构建 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(){}
}

如何解决这个问题?

标签: spring-bootunit-testingsonarqubesonarcloud

解决方案


我进行了一项研究,并获得了快速运行测试并满足 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(){}
}

我只是包括@Test(expected = Test.None.class)


推荐阅读