Skip to content

Commit

Permalink
Merge pull request #1014 from hcoles/bug/missing-plugin-reporting
Browse files Browse the repository at this point in the history
check for junit4 on classpath before loading plugin
  • Loading branch information
hcoles authored Apr 29, 2022
2 parents de21d09 + b5a7aab commit 54de329
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pitest-maven-verification/src/test/java/org/pitest/PitMojoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,15 @@ public void shouldNotNullPointerWhenEnumInitializerNotCalled() throws IOExceptio
assertThat(actual).isNotEmpty();
}

@Test
public void shouldFailCleanlyWhenTestPluginMissing() throws IOException, VerificationException {
File testDir = prepare("/pit-missing-test-plugin");
verifier.executeGoal("test");
verifier.executeGoal("org.pitest:pitest-maven:mutationCoverage");

String actual = readResults(testDir);
assertThat(actual).isNotEmpty();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>junit-categories-check</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>missing-test-plugin</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pit.version}</version>
<configuration>
<exportLineCoverage>true</exportLineCoverage>
<outputFormats><value>XML</value></outputFormats>
<timestampedReports>false</timestampedReports>
<targetTests><param>com.example.*</param></targetTests>
<targetClasses><param>com.example*</param></targetClasses>
<verbose>true</verbose>
<features>+CLASSLIMIT(limit[6])</features>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example;

public class Covered {

public static int someCode(int i) {
if ( i == 0 ) {
return 1;
}

return 1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

public class NotCovered {

public static int someCode(int i) {
if ( i == 0 ) {
return 1;
}

if ( i == 2 ) {
return 42;
}

return i;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ATest {

@Test
public void aTest() {
assertEquals(1, NotCovered.someCode(0));
}

@Test
public void anotherTest() {
assertEquals(42, NotCovered.someCode(2));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

import java.util.Arrays;
import java.util.Collection;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AnotherTest {

@Test
public void aTest() {
assertEquals(1, Covered.someCode(0));
}

@Test
public void anotherTest() {
assertEquals(1, Covered.someCode(1));
}

}
17 changes: 16 additions & 1 deletion pitest/src/main/java/org/pitest/junit/JUnitTestPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,22 @@ public String description() {
public Configuration createTestFrameworkConfiguration(TestGroupConfig config,
ClassByteArraySource source, Collection<String> excludedRunners, Collection<String> includedTestMethods) {
Objects.requireNonNull(config);
return new JUnitCompatibleConfiguration(config, excludedRunners, includedTestMethods);

if (junit4IsPresent()) {
return new JUnitCompatibleConfiguration(config, excludedRunners, includedTestMethods);
}
return new NullConfiguration();
}

// the plugin performs junit version checks later, but these don't get to run
// if junit is completely absent and cannot be loaded.
private boolean junit4IsPresent() {
try {
Class.forName("org.junit.runner.manipulation.Filter");
return true;
} catch (ClassNotFoundException ex) {
return false;
}
}

@Override
Expand Down
26 changes: 26 additions & 0 deletions pitest/src/main/java/org/pitest/junit/NullConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.pitest.junit;

import org.pitest.help.PitHelpError;
import org.pitest.testapi.Configuration;
import org.pitest.testapi.TestSuiteFinder;
import org.pitest.testapi.TestUnitFinder;

import java.util.Collections;
import java.util.Optional;

public class NullConfiguration implements Configuration {
@Override
public TestUnitFinder testUnitFinder() {
return c -> Collections.emptyList();
}

@Override
public TestSuiteFinder testSuiteFinder() {
return c -> Collections.emptyList();
}

@Override
public Optional<PitHelpError> verifyEnvironment() {
return Optional.empty();
}
}
25 changes: 25 additions & 0 deletions pitest/src/test/java/org/pitest/junit/NullConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.pitest.junit;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class NullConfigurationTest {

NullConfiguration underTest = new NullConfiguration();

@Test
public void findsNoTests() {
assertThat(underTest.testUnitFinder().findTestUnits(this.getClass())).isEmpty();
}

@Test
public void findsNoSuites() {
assertThat(underTest.testSuiteFinder().apply(this.getClass())).isEmpty();
}

@Test
public void passesVerification() {
assertThat(underTest.verifyEnvironment()).isEmpty();
}
}

0 comments on commit 54de329

Please sign in to comment.