单元测试不是 运行 使用 maven

Unit test not running using maven

我无法使用 Maven 运行 我的单元测试。我尝试使用 mvn clean install 和 mvn test 运行 测试,但没有提供预期的结果。

我已经包含了以下依赖项:

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-streams-test-utils</artifactId>
        <scope>test</scope>
</dependency>

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <!--include manifest in repository jar-->
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

测试 class 包含以下术语和注释:

@ExtendWith(SpringExtension.class)
@SpringBootTest(
    webEnvironment = WebEnvironment.NONE,
    classes = {TruckIdLookupService.class, TimescaleConfiguration.class})
public class TestClassName {

方法名如下注解和名称:

@Test
void testMethodName() throws Exception {

我能够在 Intellij 中获得预期的结果,但是 Maven 没有 运行测试。

[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ truckmsg-processor ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

是否有可能是您的系统未正确注入正确的class?

在一些项目中检查我的代码,我注意到我有以下符号:

@RunWith(SpringJUnit4ClassRunner.class)

而且 here 你可以看到它的作用。

@SpringBootTest(classes = App.class)
@WebAppConfiguration
@Transactional
public class BulkTests {
@Test
     public void test1() {
     }
 }

更改 junit Jupiter 依赖项有帮助:

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <scope>test</scope>
</dependency>

添加此依赖项有帮助:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>