Skip to content

Commit 6c56761

Browse files
Migrate tests to JUnit5 (maven)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 0312530 commit 6c56761

File tree

6 files changed

+43
-61
lines changed

6 files changed

+43
-61
lines changed

maven/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Copyright (c) 2013 Jeremy Long. All Rights Reserved.
116116
</dependency>
117117
<dependency>
118118
<groupId>org.mockito</groupId>
119-
<artifactId>mockito-core</artifactId>
119+
<artifactId>mockito-junit-jupiter</artifactId>
120120
<scope>test</scope>
121121
</dependency>
122122
<dependency>

maven/src/test/java/org/owasp/dependencycheck/maven/ArtifactScopeExcludedTest.java

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
*/
1818
package org.owasp.dependencycheck.maven;
1919

20-
import org.junit.Test;
21-
import org.junit.runner.RunWith;
22-
import org.junit.runners.Parameterized;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.MethodSource;
2322
import org.owasp.dependencycheck.utils.Filter;
2423

2524
import java.util.Arrays;
@@ -38,18 +37,9 @@
3837
import static org.hamcrest.core.IsEqual.equalTo;
3938
import static org.owasp.dependencycheck.maven.ArtifactScopeExcludedTest.ArtifactScopeExcludedTestBuilder.pluginDefaults;
4039

41-
@RunWith(Parameterized.class)
42-
public class ArtifactScopeExcludedTest {
40+
class ArtifactScopeExcludedTest {
4341

44-
private final boolean skipTestScope;
45-
private final boolean skipProvidedScope;
46-
private final boolean skipSystemScope;
47-
private final boolean skipRuntimeScope;
48-
private final String testString;
49-
private final boolean expectedResult;
50-
51-
@Parameterized.Parameters(name = "{0}")
52-
public static Collection<Object[]> getParameters() {
42+
static Collection<Object[]> getParameters() {
5343
return Arrays.asList(new Object[][]{
5444
{pluginDefaults().withTestString(SCOPE_COMPILE).withExpectedResult(false)},
5545
{pluginDefaults().withTestString(SCOPE_COMPILE_PLUS_RUNTIME).withExpectedResult(false)},
@@ -66,19 +56,12 @@ public static Collection<Object[]> getParameters() {
6656
});
6757
}
6858

69-
public ArtifactScopeExcludedTest(final ArtifactScopeExcludedTestBuilder builder) {
70-
this.skipTestScope = builder.skipTestScope;
71-
this.skipProvidedScope = builder.skipProvidedScope;
72-
this.skipSystemScope = builder.skipSystemScope;
73-
this.skipRuntimeScope = builder.skipRuntimeScope;
74-
this.testString = builder.testString;
75-
this.expectedResult = builder.expectedResult;
76-
}
77-
78-
@Test
79-
public void shouldExcludeArtifact() {
80-
final Filter<String> artifactScopeExcluded = new ArtifactScopeExcluded(skipTestScope, skipProvidedScope, skipSystemScope, skipRuntimeScope);
81-
assertThat(expectedResult, is(equalTo(artifactScopeExcluded.passes(testString))));
59+
@ParameterizedTest(name = "{0}")
60+
@MethodSource("getParameters")
61+
void shouldExcludeArtifact(final ArtifactScopeExcludedTestBuilder builder) {
62+
final Filter<String> artifactScopeExcluded = new ArtifactScopeExcluded(
63+
builder.skipTestScope, builder.skipProvidedScope, builder.skipSystemScope, builder.skipRuntimeScope);
64+
assertThat(builder.expectedResult, is(equalTo(artifactScopeExcluded.passes(builder.testString))));
8265
}
8366

8467
public static final class ArtifactScopeExcludedTestBuilder {

maven/src/test/java/org/owasp/dependencycheck/maven/ArtifactTypeExcludedTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
*/
1818
package org.owasp.dependencycheck.maven;
1919

20-
import org.junit.Test;
21-
import static org.junit.Assert.assertEquals;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
2223

2324
/**
2425
*
2526
* @author Jeremy Long
2627
*/
27-
public class ArtifactTypeExcludedTest {
28+
class ArtifactTypeExcludedTest {
2829

2930
/**
3031
* Test of passes method, of class ArtifactTypeExcluded.
3132
*/
3233
@Test
33-
public void testPasses() {
34+
void testPasses() {
3435
String artifactType = null;
3536
ArtifactTypeExcluded instance = new ArtifactTypeExcluded(null);
3637
boolean expResult = false;

maven/src/test/java/org/owasp/dependencycheck/maven/BaseDependencyCheckMojoTest.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,33 @@
1717
*/
1818
package org.owasp.dependencycheck.maven;
1919

20-
import java.io.File;
21-
import java.util.Locale;
22-
23-
import org.apache.maven.plugin.MojoExecutionException;
24-
import org.apache.maven.plugin.MojoFailureException;
2520
import org.apache.maven.project.MavenProject;
26-
27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertNull;
29-
import static org.mockito.Mockito.doReturn;
30-
31-
import org.junit.Test;
32-
import org.junit.runner.RunWith;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
3323
import org.mockito.Spy;
34-
import org.mockito.junit.MockitoJUnitRunner;
24+
import org.mockito.junit.jupiter.MockitoExtension;
3525
import org.owasp.dependencycheck.Engine;
3626
import org.owasp.dependencycheck.exception.ExceptionCollection;
3727

28+
import java.io.File;
29+
import java.util.Locale;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNull;
33+
import static org.mockito.Mockito.doReturn;
34+
3835
/**
3936
*
4037
* @author Jeremy Long
4138
*/
42-
@RunWith(MockitoJUnitRunner.class)
43-
public class BaseDependencyCheckMojoTest extends BaseTest {
39+
@ExtendWith(MockitoExtension.class)
40+
class BaseDependencyCheckMojoTest extends BaseTest {
4441

4542
@Spy
4643
MavenProject project;
4744

4845
@Test
49-
public void should_newDependency_get_pom_from_base_dir() {
46+
void should_newDependency_get_pom_from_base_dir() {
5047
// Given
5148
BaseDependencyCheckMojo instance = new BaseDependencyCheckMojoImpl();
5249

@@ -62,7 +59,7 @@ public void should_newDependency_get_pom_from_base_dir() {
6259
}
6360

6461
@Test
65-
public void should_newDependency_get_default_virtual_dependency() {
62+
void should_newDependency_get_default_virtual_dependency() {
6663
// Given
6764
BaseDependencyCheckMojo instance = new BaseDependencyCheckMojoImpl();
6865

@@ -77,7 +74,7 @@ public void should_newDependency_get_default_virtual_dependency() {
7774
}
7875

7976
@Test
80-
public void should_newDependency_get_pom_declared_as_module() {
77+
void should_newDependency_get_pom_declared_as_module() {
8178
// Given
8279
BaseDependencyCheckMojo instance = new BaseDependencyCheckMojoImpl();
8380

@@ -99,7 +96,7 @@ public void should_newDependency_get_pom_declared_as_module() {
9996
public static class BaseDependencyCheckMojoImpl extends BaseDependencyCheckMojo {
10097

10198
@Override
102-
protected void runCheck() throws MojoExecutionException, MojoFailureException {
99+
protected void runCheck() {
103100
throw new UnsupportedOperationException("Operation not supported");
104101
}
105102

@@ -119,11 +116,11 @@ public boolean canGenerateReport() {
119116
}
120117

121118
@Override
122-
protected ExceptionCollection scanDependencies(Engine engine) throws MojoExecutionException {
119+
protected ExceptionCollection scanDependencies(Engine engine) {
123120
throw new UnsupportedOperationException("Operation not supported");
124121
}
125122
@Override
126-
protected ExceptionCollection scanPlugins(Engine engine, ExceptionCollection exCollection) throws MojoExecutionException {
123+
protected ExceptionCollection scanPlugins(Engine engine, ExceptionCollection exCollection) {
127124
throw new UnsupportedOperationException("Operation not supported");
128125
}
129126
}

maven/src/test/java/org/owasp/dependencycheck/maven/BaseTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
*/
1818
package org.owasp.dependencycheck.maven;
1919

20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.owasp.dependencycheck.utils.Settings;
23+
2024
import java.io.IOException;
2125
import java.io.InputStream;
22-
import org.junit.After;
23-
import org.junit.Before;
24-
import org.owasp.dependencycheck.utils.Settings;
2526

2627
/**
2728
*
@@ -42,7 +43,7 @@ public abstract class BaseTest {
4243
/**
4344
* Initialize the {@link Settings}.
4445
*/
45-
@Before
46+
@BeforeEach
4647
public void setUp() throws IOException {
4748
settings = new Settings();
4849
try (InputStream mojoProperties = BaseTest.class.getClassLoader().getResourceAsStream(BaseTest.PROPERTIES_FILE)) {
@@ -53,7 +54,7 @@ public void setUp() throws IOException {
5354
/**
5455
* Clean the {@link Settings}.
5556
*/
56-
@After
57+
@AfterEach
5758
public void tearDown() {
5859
settings.cleanup(true);
5960
}

maven/src/test/resources/maven_project_base_dir/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
<version>2.1.2</version>
4949
</dependency>
5050
<dependency>
51-
<groupId>junit</groupId>
52-
<artifactId>junit</artifactId>
53-
<version>3.8.1</version>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter</artifactId>
53+
<version>5.12.2</version>
5454
<scope>test</scope>
5555
</dependency>
5656
</dependencies>

0 commit comments

Comments
 (0)