diff --git a/README.md b/README.md index 6367d6dc6c..93f20eefe3 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,12 @@ You can set up `mvn spotless:apply` to run automatically (in `validate` phase) f ``` +## Additional Checks + +You can set the `ban-junit4-imports.skip` property to `false` if your tests are written with JUnit 5 (Jupiter) and you want to ensure that no JUnit 4 imports are used in your plugin. +This will prevent imports of JUnit 4 classes in your code to ensure newly added or improved tests do not reintroduce JUnit 4 (e.g., out of habit or from IDE import autocompletion). +Note that this will only address imports of JUnit 4 in your code, and not uses from dependencies. + ## Running Benchmarks To run JMH benchmarks from JUnit tests, activate the `benchmark` diff --git a/pom.xml b/pom.xml index fb7d5744fc..3fb4a08e30 100644 --- a/pom.xml +++ b/pom.xml @@ -151,6 +151,9 @@ true + + true + true @@ -533,6 +536,11 @@ extra-enforcer-rules 1.9.0 + + de.skuzzle.enforcer + restrict-imports-enforcer-rule + 2.6.1 + @@ -658,6 +666,23 @@ + + check-junit-imports + + enforce + + process-sources + + + + Use JUnit 5 (org.junit.jupiter.*) + org.junit.** + org.junit.jupiter.** + + + ${ban-junit4-imports.skip} + + diff --git a/src/it/ban-junit4-fail/invoker.properties b/src/it/ban-junit4-fail/invoker.properties new file mode 100644 index 0000000000..abad60aa99 --- /dev/null +++ b/src/it/ban-junit4-fail/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals=-Dstyle.color=always -ntp verify +invoker.buildResult=failure diff --git a/src/it/ban-junit4-fail/pom.xml b/src/it/ban-junit4-fail/pom.xml new file mode 100644 index 0000000000..a10136853c --- /dev/null +++ b/src/it/ban-junit4-fail/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + @project.version@ + + + org.jenkins-ci.plugins.its + ban-junit4-fail-it + 1.0-SNAPSHOT + hpi + + 2.479.3 + false + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + diff --git a/src/it/ban-junit4-fail/postbuild.groovy b/src/it/ban-junit4-fail/postbuild.groovy new file mode 100644 index 0000000000..b64b731b8d --- /dev/null +++ b/src/it/ban-junit4-fail/postbuild.groovy @@ -0,0 +1,12 @@ +// Build failed +def hpi = new File(basedir, 'target/ban-junit4-fail-it.jar') +assert !hpi.exists() + +// Because of banned imports +def log = new File(basedir, 'build.log') +assert log.text.contains('Reason: Use JUnit 5 (org.junit.jupiter.*)') +assert log.text.contains('org.junit.Test') +assert log.text.contains('(Line: 3, Matched by: org.junit.**)') +assert log.text.contains('static org.junit.Assert.assertTrue') +assert log.text.contains('(Line: 5, Matched by: org.junit.**)') +return true diff --git a/src/it/ban-junit4-fail/src/main/resources/index.jelly b/src/it/ban-junit4-fail/src/main/resources/index.jelly new file mode 100644 index 0000000000..2f655e510a --- /dev/null +++ b/src/it/ban-junit4-fail/src/main/resources/index.jelly @@ -0,0 +1,2 @@ + +
diff --git a/src/it/ban-junit4-fail/src/test/java/banjunit4/BanJUnit4FailTest.java b/src/it/ban-junit4-fail/src/test/java/banjunit4/BanJUnit4FailTest.java new file mode 100644 index 0000000000..c9be439068 --- /dev/null +++ b/src/it/ban-junit4-fail/src/test/java/banjunit4/BanJUnit4FailTest.java @@ -0,0 +1,12 @@ +package banjunit4; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class BanJUnit4FailTest { + @Test + public void thisFails() { + assertTrue(true); + } +} diff --git a/src/it/ban-junit4-pass/invoker.properties b/src/it/ban-junit4-pass/invoker.properties new file mode 100644 index 0000000000..025c368b6d --- /dev/null +++ b/src/it/ban-junit4-pass/invoker.properties @@ -0,0 +1 @@ +invoker.goals=-Dstyle.color=always -ntp verify diff --git a/src/it/ban-junit4-pass/pom.xml b/src/it/ban-junit4-pass/pom.xml new file mode 100644 index 0000000000..d25632cee2 --- /dev/null +++ b/src/it/ban-junit4-pass/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + @project.version@ + + + org.jenkins-ci.plugins.its + ban-junit4-pass-it + 1.0-SNAPSHOT + hpi + + 2.479.3 + false + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + diff --git a/src/it/ban-junit4-pass/postbuild.groovy b/src/it/ban-junit4-pass/postbuild.groovy new file mode 100644 index 0000000000..57405da686 --- /dev/null +++ b/src/it/ban-junit4-pass/postbuild.groovy @@ -0,0 +1,10 @@ +// Build passes +def hpi = new File(basedir, 'target/ban-junit4-pass-it.jar') +assert hpi.exists() + +def log = new File(basedir, 'build.log') +assert !log.text.contains('org.junit.Test') +assert !log.text.contains('(Line: 3, Matched by: org.junit.**)') +assert !log.text.contains('static org.junit.Assert.assertTrue') +assert !log.text.contains('(Line: 5, Matched by: org.junit.**)') +return true diff --git a/src/it/ban-junit4-pass/src/main/resources/index.jelly b/src/it/ban-junit4-pass/src/main/resources/index.jelly new file mode 100644 index 0000000000..2f655e510a --- /dev/null +++ b/src/it/ban-junit4-pass/src/main/resources/index.jelly @@ -0,0 +1,2 @@ + +
diff --git a/src/it/ban-junit4-pass/src/test/java/banjunit4/BanJUnit4PassTest.java b/src/it/ban-junit4-pass/src/test/java/banjunit4/BanJUnit4PassTest.java new file mode 100644 index 0000000000..1c828d5645 --- /dev/null +++ b/src/it/ban-junit4-pass/src/test/java/banjunit4/BanJUnit4PassTest.java @@ -0,0 +1,12 @@ +package banjunit4; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class BanJUnit4PassTest { + @Test + public void thisFails() { + assertTrue(true); + } +}