【问题标题】:Maven does not pick up tests in spring boot app [duplicate]Maven 不会在 Spring Boot 应用程序中进行测试 [重复]
【发布时间】:2018-07-28 20:56:18
【问题描述】:

在 Spring Boot 应用程序中,我有一个这样的测试类

class UserInfoControllerTest {

    @Mock
    private UserService userService;
    @Mock
    private Principal principal;
    private UserInfoController subject;

    @BeforeEach
    void setUp() {
        //before each code
    }

    @Test
    void shouldReturnXYZ() {
       //test code
    }

    //more tests
}

在例如,选择Run -> Run with coverage选项时,通过Intellij Idea获取这些类中的测试。

但是当我运行mvn testmvn clean test 时,这些测试都没有被选中。我该如何解决这个问题?

编辑:这里是pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.example</groupId>
<artifactId>foo</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<name>Foo</name>
<description>Bar</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>

<dependencies>

    <!-- spring boot and dependencies-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>



    <!-- For testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我从上述文件中删除了与测试无关的依赖项,例如lombokgson 等,以保持问题较小。

【问题讨论】:

  • 你能发布文件夹结构吗?
  • @user6690200 我已将test -&gt; java 目录标记为测试源根目录。但我不认为这是一个问题,因为 maven 应该与 IDE 无关。
  • 你在哪里找到你的测试? src/test/java/ ?顺便说一句:您为什么将测试包设为私有的充分理由?公开吗?
  • @khmarbaise 测试在src/test/java 中。我将测试类包设为私有,因为 IntelliJ 抱怨这些类不必要地公开。如果我公开课程并没有任何区别。
  • 请添加您的 POM。

标签: java maven spring-boot


【解决方案1】:

这是使用 Junit5 和 Surefire 的“问题”,您需要指定 Surefire 的提供程序才能运行 Junit5 测试,

<plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                </dependencies>
</plugin>

它可以在 IntelliJ 中工作,因为 IntelliJ 目前已经原生支持 Junit5,与 Eclipse 相同。

【讨论】:

  • 谢谢,这有帮助。但这并不完全正确。我发现了另一个重复的问题,尽管如果不是这个答案,我永远不会发现。我将把它作为重复项关闭,以便任何搜索此内容的人也能找到另一个。
  • 另外值得一提的是你应该导入org.junit.jupiter.api.Test而不是`org.junit.Test`
猜你喜欢
  • 2016-07-01
  • 2017-02-09
  • 2019-04-09
  • 2019-03-17
  • 2016-10-20
  • 2023-03-10
  • 1970-01-01
  • 2016-12-17
  • 2017-05-17
相关资源
最近更新 更多