0

我正在对一些用 jsr303 约束注释的类进行单元测试,但是我找不到合适的 maven 依赖项javax.validation.Validation来创建ValidatorFactory在我的测试中使用的。奇怪的是我只在Validation课堂上遇到了这个问题。约束和验证工厂本身解决得很好。

NetBeans (7.3) 为我迄今为止尝试的每个依赖项(hibernate-validator 和 validation-api)都提供了“项目类路径中缺少 Java EE API”消息。这是一些 pom(包括两个依赖项):

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.3.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.3.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0.1</version>
    </dependency>

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

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
</dependencies>
4

2 回答 2

1

原来我必须在validation-api之前声明测试依赖项javaee-api,否则 Maven 会尝试将其用于测试,这是不可能的。在之后声明它不会覆盖更广泛的范围依赖性。

于 2013-05-09T22:36:41.260 回答
0

让我们尝试将validation-api 的groupID 更改为javax.validation

参考

于 2013-05-09T14:17:36.113 回答