我的 Junit 测试使用 DBUnit,并且从 Eclipse 运行时运行良好。然而,使用 Maven 运行相同的测试会失败:
integrity constraint violation: foreign key no action
我已经尝试连续几次从 eclipse GUI 运行该测试和整套测试(“作为 JUnit 测试运行”),并且它们从未失败 - 但从 Maven 中它们确实失败了。
我在每次测试中都使用@DatabaseSetup,但这足以真正重置数据库吗?我还认为 Maven 可能会并行运行测试,因此我尝试在 pom.xml 中将 Surefire 插件的 forkMode 设置为“always”,但它没有改变任何内容。
最佳答案
在 Maven 测试期间,我遇到了关于 JVM 中使用的编码的类似问题。
我在 pom.xml 中添加了以下内容:
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<junitArtifactName>junit:junit</junitArtifactName>
<encoding>UTF-8</encoding>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<argLine>-Xms256m -Xmx512m -XX:MaxPermSize=128m -ea
-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
另请参阅此处 http://carlobertoldi.wordpress.com/2012/03/12/maven-unit-tests-and-those-funny-characters/
这解决了我的问题
关于Junit (DBUnit) 测试在 Maven 中失败,但在 Eclipse 中却失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15405882/