单元测试未使用Maven运行

时间:2019-05-17 17:16:08

标签: java maven junit5

我无法使用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>

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

@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

4 个答案:

答案 0 :(得分:1)

更改junit的Jupiter依赖性有助于

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

答案 1 :(得分:0)

您的系统是否可能没有正确注入正确的类?

在某些项目中检查我的代码,我注意到我有以下表示法:

@RunWith(SpringJUnit4ClassRunner.class)

然后here可以看到它的作用。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

添加此依赖项有帮助:

<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>
相关问题