Skip to content

Commit

Permalink
Move to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Nov 4, 2023
1 parent 8ec5921 commit 98ad2c7
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 76 deletions.
19 changes: 15 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@
<version>${mavenFilteringVersion}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -162,6 +163,16 @@
<version>3.24.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<reporting>
Expand Down
6 changes: 3 additions & 3 deletions src/it/filter-main-annotation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
</dependency>
</dependencies>
</project>
11 changes: 6 additions & 5 deletions src/it/filter-main-annotation/src/main/java-templates/pkg/A.java
Original file line number Diff line number Diff line change
@@ -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@";
}
public static final String FILTER_A="${someprop}";
public static final String FILTER_B="@someprop@";
}
Original file line number Diff line number Diff line change
@@ -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@";
}
Original file line number Diff line number Diff line change
@@ -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 {
}
}
Original file line number Diff line number Diff line change
@@ -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}>";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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
{
@Test
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));
}
Expand All @@ -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());
}
}
7 changes: 4 additions & 3 deletions src/it/filter-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 3 additions & 3 deletions src/it/filter-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
6 changes: 3 additions & 3 deletions src/it/filter-tests/src/test/java/some/testpkg/TheClass.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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
{
@Test
public void testFoo()
{
Assert.assertEquals( "%1.0-specialtestversion%", net.batmat.maven.SomeTestClass.VERSION );
assertEquals( "%1.0-specialtestversion%", net.batmat.maven.SomeTestClass.VERSION );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <[email protected]>
* @since 2015-11-17
*/
@RunWith( MockitoJUnitRunner.class )
public class AbstractFilterSourcesMojoTest
@ExtendWith(MockitoExtension.class)
class AbstractFilterSourcesMojoTest
{
@Spy
private MavenProject project = MavenProjectStub.createProjectForITExample( "sample-simple" );
Expand All @@ -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();
Expand All @@ -90,7 +87,7 @@ public void testGetOutputDirectory()
}

@Test
public void testGetSourceDirectory()
void testGetSourceDirectory()
{
// when
File file = mojo.getSourceDirectory();
Expand All @@ -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 ) );
Expand Down Expand Up @@ -148,4 +144,4 @@ private static File resolve( File file, String... paths )
}
return new File( sb.toString() );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<File[]> 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
Expand All @@ -67,4 +63,8 @@ protected void addSourceFolderToProject( MavenProject mavenProject )
filterSourcesMojo.execute();
verifyNoInteractions( mock );
}

public void initFailingFilterSourcesMojoTest(File failingParam) {
this.failingParam = failingParam;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() );
}
Expand Down

0 comments on commit 98ad2c7

Please sign in to comment.