org.jetbrains:手动运行Maven测试时,注释不起作用

时间:2017-05-10 19:12:50

标签: java maven intellij-idea

我目前正在IntelliJ IDE中开发Java 8应用程序。我遇到了JetBrains annotations,我开始像这样使用它们:

public class ClassA {
    public ClassA(@NotNull ClassB classB, @NotNull ClassC classC) {
        ...
    }
}

和测试文件如下:

public class ClassATest {
    @Test(expected = IllegalArgumentException.class)
    public void testConstructorNull1() {
        new ClassA(null, null);
    }
}

只要我从IDE注释运行应用程序/测试工作,但是当我运行例如来自命令行的mvn tests,在测试NullPointerException参数时,我得到了很多IllegalArgumentException而不是预期的null。似乎注释被忽略了。

我认为问题出现在pom.xml的错误配置中,如下所示:

<?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>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>

    <name>...</name>
    <description>...</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.7.22</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4</artifactId>
            <version>4.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>

        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>15.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>...</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

你知道我做错了什么吗?谢谢。

平台:GNU / Linux(Kubuntu 16.04)
Java:OpenJDK 1.8.0_121
Maven:3.3.9

1 个答案:

答案 0 :(得分:1)

如果添加maven插件,它将起作用:

<pluginRepositories>
    <pluginRepository>
        <id>repository.jetbrains.com</id>
        <name>repository.jetbrains.com-all</name>
        <url>http://repository.jetbrains.com/all</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin>
            <groupId>com.intellij</groupId>
            <artifactId>notnull-instrumenter-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <goals>
                        <goal>instrument</goal>
                        <goal>tests-instrument</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>