Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij tests pass, mvn test fails

Main problem

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.

Any help is greatly appreaciated!

Olav

Example of 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());
    }
    //...
}

Application class

@SpringBootApplication
public class Application {

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

Output in command line (one for every test using a service)

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 line 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 file

<?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>
like image 962
olaven Avatar asked Mar 09 '19 21:03

olaven


People also ask

Why is IntelliJ not running tests?

Check in Project settings -> Modules that you test package is marked as Tests. Right click on the test class name either in the code window or in the project panel, and select Run <classname>. If you don't see the run menu in the popup then you haven't selected a test or you don't have junit plugin installed.

How do I skip failed test cases in Maven?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

How do I run an IntelliJ test in Maven?

Run testsOpen the Maven tool window. Under the Lifecycle node select test. Note that goals specified in the Maven surefire plugin will be activated at this phase and all tests in a project or in a module will be run.

How do I run a test in mvn?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.


1 Answers

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>
like image 192
amseager Avatar answered Oct 08 '22 12:10

amseager