Skip to content

Commit

Permalink
[#325] Spock 2 tests support
Browse files Browse the repository at this point in the history
Checking in the lib module.
  • Loading branch information
szpak committed Aug 22, 2022
1 parent a5f6031 commit cb740ac
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 11 deletions.
6 changes: 6 additions & 0 deletions functional-tests/infinitest-runner-spock-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.infinitest</groupId>
<artifactId>infinitest-lib</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.infinitest.parser

import javassist.ClassPool
import javassist.NotFoundException
import org.infinitest.testdata.HierarchicSpockSpec
import org.infinitest.testdata.ParameterizedSpockSpec
import org.infinitest.testdata.SimpleSpockSpec
import spock.lang.Specification

class JavaAssistClassSpockSpec extends Specification {

private ClassPool classPool

void setup() {
classPool = new ClassPool()
classPool.appendSystemPath()
}

void "should detect Spock 2 test (#clazz.simpleName)"() {
expect:
classFor(clazz).isATest()
where:
clazz << [SimpleSpockSpec, HierarchicSpockSpec, ParameterizedSpockSpec]
}
private JavaAssistClass classFor(Class<?> testClass) {
try {
return new JavaAssistClass(classPool.get(testClass.getName()));
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.infinitest.testdata

import spock.lang.Specification

abstract class AbstractSpockSpec extends Specification {

void "should pass from abstract class"() {
expect:
1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.infinitest.testdata

class HierarchicSpockSpec extends AbstractSpockSpec {

void "should pass in hierarchy"() {
expect:
1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.infinitest.testrunner.junit5.testdata
package org.infinitest.testdata

import spock.lang.Specification

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.infinitest.testrunner.junit5.testdata
package org.infinitest.testdata

import spock.lang.Specification

class SimpleSpockSpec extends Specification {

void "should be executed and pass"() {
expect:
GroovySystem.getShortVersion() == "4.0"
1
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.infinitest.testrunner.junit5

import org.infinitest.testdata.HierarchicSpockSpec
import org.infinitest.testrunner.DefaultRunner
import org.infinitest.testrunner.TestResults
import org.infinitest.testrunner.junit5.testdata.ParameterizedSpockSpec
import org.infinitest.testrunner.junit5.testdata.SimpleSpockSpec
import org.infinitest.testdata.ParameterizedSpockSpec
import org.infinitest.testdata.SimpleSpockSpec
import spock.lang.Specification;

class RunnerJunit5SpockSpec extends Specification {
Expand All @@ -12,7 +13,7 @@ class RunnerJunit5SpockSpec extends Specification {
expect:
Junit5Runner.isJUnit5Test(clazz)
where:
clazz << [SimpleSpockSpec, ParameterizedSpockSpec]
clazz << [SimpleSpockSpec, HierarchicSpockSpec, ParameterizedSpockSpec]
}
void "should execute Spock 2 test (#clazz.simpleName)"() {
Expand All @@ -25,6 +26,7 @@ class RunnerJunit5SpockSpec extends Specification {
where:
clazz || expectedMethodStatsSize
SimpleSpockSpec || 1
HierarchicSpockSpec || 2
ParameterizedSpockSpec || 3
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public class JavaAssistClass extends AbstractJavaClass {

public JavaAssistClass(CtClass classReference) {
imports = findImports(classReference);
isATest = !isAbstract(classReference) &&
hasTests(classReference) &&
(isJUnit5TestClass(imports) || canInstantiate(classReference));
isATest = !isAbstract(classReference) && hasSupportedTests(classReference);
className = classReference.getName();
}

Expand Down Expand Up @@ -174,10 +172,22 @@ public String getName() {
return className;
}

private boolean isAbstract(CtClass classReference) {
private static boolean isAbstract(CtClass classReference) {
return classReference.isInterface() || Modifier.isAbstract(classReference.getModifiers());
}

@SuppressWarnings("RedundantIfStatement")
private boolean hasSupportedTests(CtClass clazz) {
if (hasJUnitOrTestNGTests(clazz) &&
(isJUnit5TestClass(imports) || canInstantiate(clazz))) {
return true;
}
if (isSpockTestClass(clazz)) {
return true;
}
return false;
}

@Override
public boolean isATest() {
return isATest;
Expand Down Expand Up @@ -226,7 +236,7 @@ public String toString() {
return getName();
}

private boolean hasTests(CtClass classReference) {
private boolean hasJUnitOrTestNGTests(CtClass classReference) {
return hasJUnitTestMethods(classReference) //
|| usesCustomRunner(classReference) //
|| hasTestNGTests(classReference);
Expand Down Expand Up @@ -396,6 +406,10 @@ private boolean isJUnit4TestMethod(CtMethod ctMethod) {
return false;
}

private boolean isSpockTestClass(CtClass clazz) {
return new SpockIsTestClassChecker(clazz).isATest();
}

public void setClassFile(File classFile) {
this.classFile = classFile;
}
Expand All @@ -409,4 +423,37 @@ public boolean locatedInClassFile() {
public File getClassFile() {
return classFile;
}

private static class SpockIsTestClassChecker {

private static final String SPOCK_BASE_CLASS_NAME = "spock.lang.Specification";
private final boolean isATest;

public SpockIsTestClassChecker(CtClass clazz) {
isATest = !isAbstract(clazz) && hasGivenClassSpockSpecificationAsSuperclass(clazz);
}

private boolean hasGivenClassSpockSpecificationAsSuperclass(CtClass clazz) {
try {
return isGivenClassSpockSpecification(clazz.getSuperclass());
} catch (NotFoundException e) {
return false; //TODO: Log warning?
}
}

private boolean isGivenClassSpockSpecification(CtClass currentSuperclass) throws NotFoundException {
if (currentSuperclass == null || "java.lang.Object".equals(currentSuperclass.getName())) {
return false;
}
if (SPOCK_BASE_CLASS_NAME.equals(currentSuperclass.getName())) {
return true;
}
return isGivenClassSpockSpecification(currentSuperclass.getSuperclass());
}

boolean isATest() {
return isATest;
}
}

}

0 comments on commit cb740ac

Please sign in to comment.