From 98ad2c77d69528a2dda72d0a3e217ac54e96ef33 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Fri, 27 Oct 2023 23:46:45 +0200 Subject: [PATCH] Move to JUnit5 --- pom.xml | 19 ++++++++++--- src/it/filter-main-annotation/pom.xml | 6 ++-- .../src/main/java-templates/pkg/A.java | 11 ++++---- .../src/main/java-templates/pkg/B.java | 6 ++-- .../src/main/java-templates/pkg/C.java | 7 +++-- .../main/java-templates/pkg/SomeClass.java | 7 +++-- .../test/java/net/batmat/maven/AppTest.java | 18 ++++++------ src/it/filter-main/pom.xml | 7 +++-- .../test/java/net/batmat/maven/AppTest.java | 9 +++--- src/it/filter-tests/pom.xml | 6 ++-- .../src/test/java/some/testpkg/TheClass.java | 6 ++-- .../AbstractFilterSourcesMojoTest.java | 28 ++++++++----------- .../FailingFilterSourcesMojoTest.java | 26 ++++++++--------- .../templating/OkFilterSourcesMojoTest.java | 9 +++--- 14 files changed, 89 insertions(+), 76 deletions(-) diff --git a/pom.xml b/pom.xml index a976a81..256b9c2 100644 --- a/pom.xml +++ b/pom.xml @@ -146,14 +146,15 @@ ${mavenFilteringVersion} - junit - junit + org.mockito + mockito-core + 5.4.0 test org.mockito - mockito-core - 5.4.0 + mockito-junit-jupiter + 4.11.0 test @@ -162,6 +163,16 @@ 3.24.2 test + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-params + test + diff --git a/src/it/filter-main-annotation/pom.xml b/src/it/filter-main-annotation/pom.xml index b6079c3..52f5032 100644 --- a/src/it/filter-main-annotation/pom.xml +++ b/src/it/filter-main-annotation/pom.xml @@ -35,9 +35,9 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-api + 5.10.0 diff --git a/src/it/filter-main-annotation/src/main/java-templates/pkg/A.java b/src/it/filter-main-annotation/src/main/java-templates/pkg/A.java index 323e4de..0c0a2a1 100644 --- a/src/it/filter-main-annotation/src/main/java-templates/pkg/A.java +++ b/src/it/filter-main-annotation/src/main/java-templates/pkg/A.java @@ -1,8 +1,9 @@ package pkg; -import org.junit.Ignore; -@Ignore("${someprop}") +import org.junit.jupiter.api.Disabled; + +@Disabled("${someprop}") public class A { - public static final String FILTER_A="${someprop}"; - public static final String FILTER_B="@someprop@"; -} \ No newline at end of file + public static final String FILTER_A="${someprop}"; + public static final String FILTER_B="@someprop@"; +} diff --git a/src/it/filter-main-annotation/src/main/java-templates/pkg/B.java b/src/it/filter-main-annotation/src/main/java-templates/pkg/B.java index 27e87a3..181ab72 100644 --- a/src/it/filter-main-annotation/src/main/java-templates/pkg/B.java +++ b/src/it/filter-main-annotation/src/main/java-templates/pkg/B.java @@ -1,8 +1,8 @@ package pkg; -import org.junit.Ignore; +import org.junit.jupiter.api.Disabled; -@Ignore( "@someprop@") +@Disabled( "@someprop@") public class B { - public static final String FILTER_A="${someprop}"; + public static final String FILTER_A="${someprop}"; public static final String FILTER_B="@someprop@"; } diff --git a/src/it/filter-main-annotation/src/main/java-templates/pkg/C.java b/src/it/filter-main-annotation/src/main/java-templates/pkg/C.java index b77edd9..df1470d 100644 --- a/src/it/filter-main-annotation/src/main/java-templates/pkg/C.java +++ b/src/it/filter-main-annotation/src/main/java-templates/pkg/C.java @@ -1,11 +1,12 @@ package pkg; -import org.junit.Ignore; -@Ignore( +import org.junit.jupiter.api.Disabled; + +@Disabled( "${someprop}" + "@someprop@" + "${someprop}@someprop@ @someprop@${someprop}" ) public class C { -} \ No newline at end of file +} diff --git a/src/it/filter-main-annotation/src/main/java-templates/pkg/SomeClass.java b/src/it/filter-main-annotation/src/main/java-templates/pkg/SomeClass.java index 6d5270a..7f4a146 100644 --- a/src/it/filter-main-annotation/src/main/java-templates/pkg/SomeClass.java +++ b/src/it/filter-main-annotation/src/main/java-templates/pkg/SomeClass.java @@ -1,13 +1,14 @@ package pkg; -import org.junit.Ignore; +import org.junit.jupiter.api.Disabled; + /* some stupid doc * basedir=${basedir} - * + * * some new code * et bim */ -@Ignore("${someprop}") +@Disabled("${someprop}") public class SomeClass { public static final String VERSION = "<${project.version}>"; diff --git a/src/it/filter-main-annotation/src/test/java/net/batmat/maven/AppTest.java b/src/it/filter-main-annotation/src/test/java/net/batmat/maven/AppTest.java index e0fde43..44fe1cb 100644 --- a/src/it/filter-main-annotation/src/test/java/net/batmat/maven/AppTest.java +++ b/src/it/filter-main-annotation/src/test/java/net/batmat/maven/AppTest.java @@ -1,9 +1,9 @@ package net.batmat.maven; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; public class AppTest { @@ -11,13 +11,13 @@ public class AppTest public void testApp() { assertEquals("<1.0-specialversion>", pkg.SomeClass.VERSION); - assertEquals("coquinou", pkg.SomeClass.class.getAnnotation(Ignore.class).value()); + assertEquals("coquinou", pkg.SomeClass.class.getAnnotation(Disabled.class).value()); } @Test public void testA() throws Exception { - assertEquals("coquinou", pkg.A.class.getAnnotation(Ignore.class).value()); + assertEquals("coquinou", pkg.A.class.getAnnotation(Disabled.class).value()); assertEquals("coquinou", pkg.A.class.getDeclaredField("FILTER_A").get(null)); assertEquals("coquinou", pkg.A.class.getDeclaredField("FILTER_B").get(null)); } @@ -29,16 +29,16 @@ public void testB() throws Exception assertEquals("coquinou", pkg.B.class.getDeclaredField("FILTER_B").get(null)); } - @Ignore("See MOJO-2012, not sure it will ever work. To be continued...") + @Disabled("See MOJO-2012, not sure it will ever work. To be continued...") @Test public void testB_TwoArobases() throws Exception { - assertEquals("coquinou", pkg.B.class.getAnnotation(Ignore.class).value()); + assertEquals("coquinou", pkg.B.class.getAnnotation(Disabled.class).value()); } - + @Test public void testC() throws Exception { - assertEquals("coquinoucoquinoucoquinoucoquinou coquinoucoquinou", pkg.C.class.getAnnotation(Ignore.class).value()); + assertEquals("coquinoucoquinoucoquinoucoquinou coquinoucoquinou", pkg.C.class.getAnnotation(Disabled.class).value()); } } diff --git a/src/it/filter-main/pom.xml b/src/it/filter-main/pom.xml index 4f08794..889152a 100644 --- a/src/it/filter-main/pom.xml +++ b/src/it/filter-main/pom.xml @@ -32,9 +32,10 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-api + 5.10.0 + test diff --git a/src/it/filter-main/src/test/java/net/batmat/maven/AppTest.java b/src/it/filter-main/src/test/java/net/batmat/maven/AppTest.java index c08d996..242ab7c 100644 --- a/src/it/filter-main/src/test/java/net/batmat/maven/AppTest.java +++ b/src/it/filter-main/src/test/java/net/batmat/maven/AppTest.java @@ -1,12 +1,13 @@ package net.batmat.maven; -import org.junit.Assert; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; -public class AppTest +public class AppTest { - @org.junit.Test + @Test public void testApp() { - Assert.assertEquals( "<1.0-specialversion>", pkg.SomeClass.VERSION); + assertEquals( "<1.0-specialversion>", pkg.SomeClass.VERSION); } } diff --git a/src/it/filter-tests/pom.xml b/src/it/filter-tests/pom.xml index dbff532..a91dbe8 100644 --- a/src/it/filter-tests/pom.xml +++ b/src/it/filter-tests/pom.xml @@ -7,9 +7,9 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-api + 5.10.0 test diff --git a/src/it/filter-tests/src/test/java/some/testpkg/TheClass.java b/src/it/filter-tests/src/test/java/some/testpkg/TheClass.java index ef668c4..75eadde 100644 --- a/src/it/filter-tests/src/test/java/some/testpkg/TheClass.java +++ b/src/it/filter-tests/src/test/java/some/testpkg/TheClass.java @@ -1,7 +1,7 @@ package some.testpkg; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; @SuppressWarnings( net.batmat.maven.SomeTestClass.VERSION ) public class TheClass @@ -9,6 +9,6 @@ public class TheClass @Test public void testFoo() { - Assert.assertEquals( "%1.0-specialtestversion%", net.batmat.maven.SomeTestClass.VERSION ); + assertEquals( "%1.0-specialtestversion%", net.batmat.maven.SomeTestClass.VERSION ); } } diff --git a/src/test/java/org/codehaus/mojo/templating/AbstractFilterSourcesMojoTest.java b/src/test/java/org/codehaus/mojo/templating/AbstractFilterSourcesMojoTest.java index 5feba27..c3c7834 100644 --- a/src/test/java/org/codehaus/mojo/templating/AbstractFilterSourcesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/templating/AbstractFilterSourcesMojoTest.java @@ -26,7 +26,6 @@ import static org.mockito.Mockito.verify; import java.io.File; -import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.maven.execution.MavenSession; @@ -35,22 +34,22 @@ import org.apache.maven.shared.filtering.MavenFilteringException; import org.apache.maven.shared.filtering.MavenResourcesExecution; import org.apache.maven.shared.filtering.MavenResourcesFiltering; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.invocation.InvocationOnMock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.Answer; /** * @author Krzysztof SuszyƄski * @since 2015-11-17 */ -@RunWith( MockitoJUnitRunner.class ) -public class AbstractFilterSourcesMojoTest +@ExtendWith(MockitoExtension.class) +class AbstractFilterSourcesMojoTest { @Spy private MavenProject project = MavenProjectStub.createProjectForITExample( "sample-simple" ); @@ -70,16 +69,14 @@ public class AbstractFilterSourcesMojoTest @InjectMocks private AbstractFilterSourcesMojo mojo = new FilterSourcesMojo(); - @Before - public void before() - throws IOException - { + @BeforeEach + void before() { File target = resolve( project.getBasedir(), outputDirectory.getPath() ); FileUtils.deleteQuietly( target ); } @Test - public void testGetOutputDirectory() + void testGetOutputDirectory() { // when File file = mojo.getOutputDirectory(); @@ -90,7 +87,7 @@ public void testGetOutputDirectory() } @Test - public void testGetSourceDirectory() + void testGetSourceDirectory() { // when File file = mojo.getSourceDirectory(); @@ -104,8 +101,7 @@ public void testGetSourceDirectory() } @Test - public void testExecute() - throws MojoExecutionException, MavenFilteringException + void testExecute() throws MojoExecutionException, MavenFilteringException { // given doAnswer( new MockCopyAnswer() ).when( mavenResourcesFiltering ).filterResources( any( MavenResourcesExecution.class ) ); @@ -148,4 +144,4 @@ private static File resolve( File file, String... paths ) } return new File( sb.toString() ); } -} \ No newline at end of file +} diff --git a/src/test/java/org/codehaus/mojo/templating/FailingFilterSourcesMojoTest.java b/src/test/java/org/codehaus/mojo/templating/FailingFilterSourcesMojoTest.java index 4e4d9cf..2ec0cce 100644 --- a/src/test/java/org/codehaus/mojo/templating/FailingFilterSourcesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/templating/FailingFilterSourcesMojoTest.java @@ -25,32 +25,28 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; -import org.apache.maven.shared.filtering.MavenFilteringException; import org.apache.maven.shared.filtering.MavenResourcesFiltering; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; -import org.junit.runners.Parameterized.Parameters; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; -@RunWith( value = Parameterized.class ) // Let's play with Parameterized, I've been wanted to do that for quite a long time :-). public class FailingFilterSourcesMojoTest { - @Parameter( 0 ) public File failingParam; - @Parameters public static Collection getFailingParameters() { return Arrays.asList( new File[] { null }, new File[] { new File( "/non/existing/path/yodleyyyyeee" ) } ); } - @Test - public void testBadDirectoryDoesNotAddSourceFolder() - throws MojoExecutionException { + @MethodSource("getFailingParameters") + @ParameterizedTest + void testBadDirectoryDoesNotAddSourceFolder(File failingParam) + throws MojoExecutionException { + initFailingFilterSourcesMojoTest( failingParam ); FilterSourcesMojo filterSourcesMojo = new FilterSourcesMojo() { @Override @@ -67,4 +63,8 @@ protected void addSourceFolderToProject( MavenProject mavenProject ) filterSourcesMojo.execute(); verifyNoInteractions( mock ); } + + public void initFailingFilterSourcesMojoTest(File failingParam) { + this.failingParam = failingParam; + } } diff --git a/src/test/java/org/codehaus/mojo/templating/OkFilterSourcesMojoTest.java b/src/test/java/org/codehaus/mojo/templating/OkFilterSourcesMojoTest.java index a6b0f12..ddec069 100644 --- a/src/test/java/org/codehaus/mojo/templating/OkFilterSourcesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/templating/OkFilterSourcesMojoTest.java @@ -24,18 +24,19 @@ import java.io.File; import org.apache.maven.project.MavenProject; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -public class OkFilterSourcesMojoTest +class OkFilterSourcesMojoTest { @Test - public void testExistingDirectoryDoesNotAddSourceFolder() + void testExistingDirectoryDoesNotAddSourceFolder() { final StringBuilder placeholder = new StringBuilder(); FilterSourcesMojo filterSourcesMojo = new FilterSourcesMojo() @@ -53,7 +54,7 @@ protected void addSourceFolderToProject( MavenProject mavenProject ) Mockito.doThrow( IllegalArgumentException.class ).when( mock ).addTestCompileSourceRoot( anyString() ); filterSourcesMojo.addSourceFolderToProject( mock ); - + assertEquals( "called", placeholder.toString() ); assertThat( placeholder.toString() ).isEqualTo( "called" ); verify( mock, never() ).addTestCompileSourceRoot( anyString() ); }