问题描述:
spock框架版本
<?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>com.jichu.spock</groupId>
<artifactId>test-spock</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spock.version>2.1-M2-groovy-3.0</spock.version>
<spock-genesis.version>0.6.0</spock-genesis.version>
<groovy.version>3.0.9</groovy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nagternal</groupId>
<artifactId>spock-genesis</artifactId>
<version>${spock-genesis.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
<exclusion>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>-->
<!--<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
</dependency>-->
<!-- spock模拟类的时候需要这个库 -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.6</version>
</dependency>
<!-- 模拟没有默认构造函数的对象时(没有无参构造函数,且多于一个有参构造函数),需要这个库 -->
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.2</version>
<scope>test</scope>
</dependency>
<!-- 提供多种结果匹配的方法 -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.13.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>addTestSources</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- mvn测试插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
idea版本:
问题:
运行groovy测试方法时,第一次运行可以成功,第二次运行就会失败。报异常:
"C:\Program Files\Java\jdk1.8.0_301\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Tools\JetBrains\IntelliJ IDEA 2021.2\lib\idea_rt.jar=58007:D:\Tools\JetBrains\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Tools\JetBrains\IntelliJ IDEA 2021.2\lib\idea_rt.jar;D:\Tools\JetBrains\IntelliJ IDEA 2021.2\plugins\junit\lib\junit5-rt.jar;D:\Tools\JetBrains\IntelliJ IDEA 2021.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_301\jre\lib\rt.jar;E:\my-own-workspace\test-spock\target\test-classes;C:\Users\HL\.m2\repository\org\spockframework\spock-core\2.1-M2-groovy-3.0\spock-core-2.1-M2-groovy-3.0.jar;C:\Users\HL\.m2\repository\org\codehaus\groovy\groovy\3.0.9\groovy-3.0.9.jar;C:\Users\HL\.m2\repository\org\junit\platform\junit-platform-engine\1.8.1\junit-platform-engine-1.8.1.jar;C:\Users\HL\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\HL\.m2\repository\org\junit\platform\junit-platform-commons\1.8.1\junit-platform-commons-1.8.1.jar;C:\Users\HL\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Users\HL\.m2\repository\com\nagternal\spock-genesis\0.6.0\spock-genesis-0.6.0.jar;C:\Users\HL\.m2\repository\com\github\mifmif\generex\1.0.1\generex-1.0.1.jar;C:\Users\HL\.m2\repository\dk\brics\automaton\automaton\1.11-8\automaton-1.11-8.jar;C:\Users\HL\.m2\repository\net\bytebuddy\byte-buddy\1.12.6\byte-buddy-1.12.6.jar;C:\Users\HL\.m2\repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;C:\Users\HL\.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 "com.jichu.spock.SpockDataDrivenT,maximum of #a and #b is #c"
!!! JUnit version 3.8 or later expected:
java.lang.ClassNotFoundException: junit.framework.ComparisonFailure
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:214)
at com.intellij.rt.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:198)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:46)
Process finished with exit code -3
这应该算是一个idea工具的bug。解决方案:
打开工程的 project-structure
去掉 Tests 排除文件夹 target
应用后,项目编译产生的target目录会变成这样:
再次运行测试方法会再次报异常。之后就可以正常执行。