Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for junit4 on classpath before loading plugin #1014

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test the error message or something else here?

Copy link
Owner Author

@hcoles hcoles Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't one at the moment - everything runs without finding tests as there are no applicable test plugins. Just pulling testng support out into an external plugin before fixing that.

}


}
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();
}
}