Intellij 测试通过,mvn 测试失败

Posted

技术标签:

【中文标题】Intellij 测试通过,mvn 测试失败【英文标题】:Intellij tests pass, mvn test fails 【发布时间】:2019-07-31 14:20:12 【问题描述】:

主要问题

我正在开发一个 spring-boot 应用程序。 我有测试,@autowiredserivces。当我在 IntelliJ 中运行它们时,所有测试都通过了。但是,当我从命令行运行mvn test时,会抛出空指针异常。

非常感谢任何帮助!

奥拉夫

测试示例

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class,
        webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class CategoryServiceTest extends ServiceTestBase 

    @Autowired
    private CategoryService categoryService;


    @Test
    public void testNoCategory() 
        List<Category> categories = categoryService.getAllCategories(false);
        assertEquals(0, categories.size());
    
    //...

应用类

@SpringBootApplication
public class Application 

    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

命令行输出(每个使用服务的测试一个)

org.olaven.quizgame.services.QuizServiceTest.testQuizWithoutSubCategoryFails()  Time elapsed: 0 sec  <<< FAILURE!
java.lang.NullPointerException
    at org.olaven.quizgame.services.QuizServiceTest.testQuizWithoutSubCategoryFails(QuizServiceTest.java:219)

QuizServiceTest.java 行 217-220

Long categoryId = categoryService.createCategory(getRandomString(10));
Long subCategoryId = categoryService.createSubCategory(categoryId, getRandomString(10));
Long quizId = quizService.createQuiz(subCategoryId, getRandomString(10), "1", "2", "3", "4", 0);

POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>olaven</groupId>
    <artifactId>quizgame</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <version.springboot>2.1.3.RELEASE</version.springboot>
        <version.junit>5.0.2</version.junit>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>$version.springboot</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>$version.springboot</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>$version.springboot</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>$version.springboot</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>$version.springboot</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.el</groupId>
                    <artifactId>javax.el-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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




        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.198</version>
        </dependency>
    </dependencies>


    <build>
        <finalName>quizgame</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>$version.springboot</version>
                <configuration>
                    <finalName>quizgame</finalName>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
</project>

【问题讨论】:

您是否按照here 的概述定义了test 参数? 我尝试运行mvn surefire:test -Dtest=CategoryServiceTestmvn -Dtest=QuizServiceTest test。感谢您的建议,但不幸的是,两者都没有奏效。 明白了。您可以粘贴您的 pom.xml 作为您问题的一部分吗? 现在更新:-) 【参考方案1】:

尝试将更新的maven-surefire-plugin 版本添加到您的 pom.xml 中:

<build>
     <plugins>
         ....
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>2.22.0</version>
         </plugin>
         ....
     </plugins>
 </build>

【讨论】:

您能否为您的答案添加一个简短的解释? @Gautam Junit 4 测试自 2.22 版本起由 maven surefire 插件支持。见junit.org/junit5/docs/current/user-guide/… 和issues.apache.org/jira/browse/SUREFIRE-1330 谢谢,这拯救了我的一天!

以上是关于Intellij 测试通过,mvn 测试失败的主要内容,如果未能解决你的问题,请参考以下文章

在测试阶段,Spring Security OAuth2 的 mvn 安装失败

仅当按下重新运行失败的测试时才使用 gradle 和 intellij 找到测试

Jenkins-如果测试失败,即使构建通过,也要查看任何失败图标

Eclipse编译运行没问题,但执行mvn clean install跑单元测试失败的原因解析

Maven 生成空的肯定报告

Kotlin 测试从命令行失败并出现 ClassNotFoundException 但在 IntelliJ 中工作