Intellij测试通过,MVN测试失败 [英] Intellij tests pass, mvn test fails

查看:56
本文介绍了Intellij测试通过,MVN测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Spring Boot应用程序. 我已经测试了@autowired serivces.当我在IntelliJ中运行它们时,所有测试均通过.但是,当我从命令行运行mvn test时,会抛出nullpointer-exceptions.

I am working on a spring-boot application. I have test, with @autowiredserivces. All tests pass when I run them in IntelliJ. However, when I run mvn testfrom command line, nullpointer-exceptions are thrown.

非常感谢您的帮助!

奥拉夫

@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>

推荐答案

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

Try to add newer maven-surefire-plugin version into your pom.xml:

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

这篇关于Intellij测试通过,MVN测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆