SonarCloud 在 Springboot 的 contextLoads 单元测试中引发“Blocker”

问题描述 投票:0回答:2

构建 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-boot unit-testing sonarqube sonarcloud
2个回答
1
投票

我做了一项研究,得到了快速的解决方案来运行测试并满足声纳的要求:

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)


0
投票

对于 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
        });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.