2013-03-14 95 views
2

我的Junit测试使用DBUnit,它们在从Eclipse运行时运行正常。但是,运行与Maven相同的测试失败,一个测试:Junit(DBUnit)测试失败与Maven但不是在Eclipse中?

integrity constraint violation: foreign key no action 

我已经尝试运行测试和一整套从蚀GUI测试(“运行作为JUnit测试”)多次连续和他们永远不会失败 - 但从Maven他们确实失败。

我对每个测试都使用@DatabaseSetup,但这足以真正重置数据库吗?我也认为Maven可能并行运行测试,所以我尝试在我的pom.xml中将forkMode设置为“始终”用于Surefire插件,但它没有改变任何东西。

+1

你试图[运行与Maven单个测试(HTTP: //maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html)? – gontard 2013-03-14 10:35:49

+0

好点。现在我已经尝试过了,单独运行失败测试的测试类,但运行所有测试失败,如前所述。 – wannabeartist 2013-03-14 10:48:13

+0

但是从eclipse GUI运行“整套测试”有效吗?这很奇怪。你确定,这是同一套测试? – gontard 2013-03-14 10:53:40

回答

1

您正在使用来自spring-test DBUnit的注释@DatabaseSetUp进行数据库初始化。您也应该使用注释@DatabaseTearDown

spring-test DBUnit文档:

The @DatabaseTearDown annotation can be used to reset database tables once a test has completed. As with @DatabaseSetup the annotation can be applied at the method or class level. When using @DatabaseTearDown use the value and type attributes in the same way as @DatabaseSetup.

+0

有没有办法在每种情况下创建全新的模式? @DatabaseTearDown无法重置ID生成器,在我的一些测试中,我检查预期数据集中的ID。 – 2014-07-15 09:58:07

+0

@ stanislav.chetvertkov我不知道。你可以问另一个问题。 – gontard 2014-07-15 10:02:22

+0

可能我会 – 2014-07-15 10:06:10

3

我有类似的问题指的在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> 

参见herer http://carlobertoldi.wordpress.com/2012/03/12/maven-unit-tests-and-those-funny-characters/

这解决了我的问题

+0

这个答案与问题有关吗? – gontard 2014-07-15 10:02:49

相关问题