Skip to content

Commit 2bca19a

Browse files
slachiewiczTeamModernemfriedenhagen
authored
JUnit 5 best practices (#343)
--------- Co-authored-by: Moderne <team@moderne.io> Co-authored-by: Mirko Friedenhagen <mfriedenhagen@gmx.de>
1 parent 52e2ec1 commit 2bca19a

File tree

12 files changed

+190
-167
lines changed

12 files changed

+190
-167
lines changed

pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,15 @@
9494
<artifactId>commons-codec</artifactId>
9595
<version>1.19.0</version>
9696
</dependency>
97-
98-
<!-- testing support -->
9997
<dependency>
100-
<groupId>junit</groupId>
101-
<artifactId>junit</artifactId>
98+
<groupId>org.mockito</groupId>
99+
<artifactId>mockito-core</artifactId>
100+
<version>4.11.0</version>
102101
<scope>test</scope>
103102
</dependency>
104103
<dependency>
105104
<groupId>org.mockito</groupId>
106-
<artifactId>mockito-core</artifactId>
105+
<artifactId>mockito-junit-jupiter</artifactId>
107106
<version>4.11.0</version>
108107
<scope>test</scope>
109108
</dependency>

src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileHelper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@
2525
import java.nio.file.Files;
2626

2727
import org.apache.maven.artifact.Artifact;
28-
import org.junit.rules.TemporaryFolder;
2928

3029
/**
3130
* Test utility to make writing tests with {@link ClassFile}s easier.
3231
*/
3332
public class ClassFileHelper {
3433
private static int uniqueId = 0;
3534

36-
private final TemporaryFolder temporaryFolder;
35+
private final File temporaryFolder;
3736

38-
public ClassFileHelper(TemporaryFolder temporaryFolder) {
37+
public ClassFileHelper(File temporaryFolder) {
3938
this.temporaryFolder = temporaryFolder;
4039
}
4140

@@ -60,7 +59,7 @@ public ClassFile createWithContent(String pathToClassFile, String fileContents)
6059

6160
private File createTempDirectory(String uniqueIdStr) {
6261
try {
63-
return temporaryFolder.newFolder(uniqueIdStr);
62+
return newFolder(temporaryFolder, uniqueIdStr);
6463
} catch (IOException exception) {
6564
throw new RuntimeException("unable to create temporary folder", exception);
6665
}
@@ -80,4 +79,13 @@ private void createClassFile(File directory, String pathToClassFile, String file
8079
writer.write(fileContents);
8180
}
8281
}
82+
83+
private static File newFolder(File root, String... subDirs) throws IOException {
84+
String subFolder = String.join("/", subDirs);
85+
File result = new File(root, subFolder);
86+
if (!result.mkdirs()) {
87+
throw new IOException("Couldn't create folders " + root);
88+
}
89+
return result;
90+
}
8391
}

src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassFileTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
package org.codehaus.mojo.extraenforcer.dependencies;
22

3-
import org.junit.Rule;
4-
import org.junit.Test;
5-
import org.junit.rules.TemporaryFolder;
3+
import java.io.File;
64

7-
import static org.junit.Assert.assertEquals;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.io.TempDir;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
89

910
public class ClassFileTest {
1011
private static final String PATH_TO_CLASS_FILE =
1112
ClassFileTest.class.getName().replace('.', '/') + ".class";
1213

13-
@Rule
14-
public TemporaryFolder tempFolder = new TemporaryFolder();
14+
@TempDir
15+
private File tempFolder;
1516

1617
private final ClassFileHelper classFileHelper = new ClassFileHelper(tempFolder);
1718

1819
@Test
19-
public void getHashComputesHashOfFile() throws Exception {
20+
void getHashComputesHashOfFile() throws Exception {
2021
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "the content of the file");
2122

2223
assertEquals("7b7f48e1c0e847133d8881d5743d253756bf44e490e2252556ad4816a0a77b67", classFile.getHash());
2324
}
2425

2526
@Test
26-
public void getHashReturnsConsistentHashWhenInvokedTwice() throws Exception {
27+
void getHashReturnsConsistentHashWhenInvokedTwice() throws Exception {
2728
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content");
2829

2930
String hash1 = classFile.getHash();

src/test/java/org/codehaus/mojo/extraenforcer/dependencies/ClassesWithSameNameTest.java

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
* under the License.
2020
*/
2121

22+
import java.io.File;
2223
import java.util.Set;
2324

2425
import org.apache.maven.artifact.Artifact;
2526
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
26-
import org.junit.Before;
27-
import org.junit.Rule;
28-
import org.junit.Test;
29-
import org.junit.rules.TemporaryFolder;
30-
31-
import static org.junit.Assert.assertEquals;
32-
import static org.junit.Assert.assertFalse;
33-
import static org.junit.Assert.assertTrue;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.io.TempDir;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
3435
import static org.mockito.Mockito.mock;
3536

3637
public class ClassesWithSameNameTest {
@@ -48,13 +49,13 @@ public class ClassesWithSameNameTest {
4849

4950
private static final EnforcerLogger LOG = mock(EnforcerLogger.class);
5051

51-
@Rule
52-
public TemporaryFolder temporaryFolder = new TemporaryFolder();
52+
@TempDir
53+
private File temporaryFolder;
5354

5455
private ClassFileHelper classFileHelper;
5556

56-
@Before
57-
public void beforeEachTest() {
57+
@BeforeEach
58+
void beforeEachTest() {
5859
classFileHelper = new ClassFileHelper(temporaryFolder);
5960
}
6061

@@ -64,7 +65,7 @@ public void beforeEachTest() {
6465
* files are exactly the same.
6566
*/
6667
@Test
67-
public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicate() throws Exception {
68+
void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicate() throws Exception {
6869
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
6970
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
7071
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -75,7 +76,7 @@ public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicate() throws Excep
7576
}
7677

7778
@Test
78-
public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateButBytecodeIsIdentical() throws Exception {
79+
void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateButBytecodeIsIdentical() throws Exception {
7980
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
8081
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
8182
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -86,7 +87,7 @@ public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateButBytecodeIsI
8687
}
8788

8889
@Test
89-
public void hasDuplicatesShouldReturnFalseWhenClassHasNoDuplicates() throws Exception {
90+
void hasDuplicatesShouldReturnFalseWhenClassHasNoDuplicates() throws Exception {
9091
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
9192
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile);
9293

@@ -104,7 +105,7 @@ public void hasDuplicatesShouldReturnFalseWhenClassHasNoDuplicates() throws Exce
104105
* bytecode).
105106
*/
106107
@Test
107-
public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicateButBytecodeDiffers() throws Exception {
108+
void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicateButBytecodeDiffers() throws Exception {
108109
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "1");
109110
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "2");
110111
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -122,7 +123,7 @@ public void hasDuplicatesShouldReturnTrueWhenClassNameIsDuplicateButBytecodeDiff
122123
* We set the test up so it finds duplicates only if the bytecode differs.
123124
*/
124125
@Test
125-
public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateAndBytecodeDiffers() throws Exception {
126+
void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateAndBytecodeDiffers() throws Exception {
126127
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "1");
127128
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "2");
128129
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -136,7 +137,7 @@ public void hasDuplicatesShouldReturnFalseWhenClassNameIsDuplicateAndBytecodeDif
136137
* This tests the normal condition where we just output the class file path.
137138
*/
138139
@Test
139-
public void toOutputStringOutputsPlainArtifactWhenJustNamesAreDuplicate() throws Exception {
140+
void toOutputStringOutputsPlainArtifactWhenJustNamesAreDuplicate() throws Exception {
140141
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
141142
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
142143
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -152,7 +153,7 @@ public void toOutputStringOutputsPlainArtifactWhenJustNamesAreDuplicate() throws
152153
* determine which artifacts they can ignore when fix the BanDuplicateClasses error.
153154
*/
154155
@Test
155-
public void toOutputStringOutputsTwoArtifactsWhereBytecodeIsExactMatch() throws Exception {
156+
void toOutputStringOutputsTwoArtifactsWhereBytecodeIsExactMatch() throws Exception {
156157
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
157158
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "content matches in both");
158159
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -171,8 +172,7 @@ public void toOutputStringOutputsTwoArtifactsWhereBytecodeIsExactMatch() throws
171172
* 1 and 2 don't match 3 and 4.
172173
*/
173174
@Test
174-
public void toOutputStringOutputsFourArtifactsWhereBytecodeIsExactMatchInTwoAndExactMatchInOtherTwo()
175-
throws Exception {
175+
void toOutputStringOutputsFourArtifactsWhereBytecodeIsExactMatchInTwoAndExactMatchInOtherTwo() throws Exception {
176176
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
177177
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
178178
ClassFile classFile3 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 3 and 4");
@@ -194,16 +194,15 @@ public void toOutputStringOutputsFourArtifactsWhereBytecodeIsExactMatchInTwoAndE
194194
* The method should return the 2nd-to-last element in the last, but if there's only 1 element
195195
* there's no 2nd-to-last element to return.
196196
*/
197-
@Test(expected = IllegalArgumentException.class)
198-
public void previousShouldThrowIfOnlyOneArtifact() throws Exception {
197+
@Test
198+
void previousShouldThrowIfOnlyOneArtifact() throws Exception {
199199
ClassFile classFile = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
200200
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile);
201-
202-
classesWithSameName.previous();
201+
assertThrows(IllegalArgumentException.class, () -> classesWithSameName.previous());
203202
}
204203

205204
@Test
206-
public void previousShouldReturn2ndToLastElement() throws Exception {
205+
void previousShouldReturn2ndToLastElement() throws Exception {
207206
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
208207
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "file content of 1 and 2");
209208
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);
@@ -214,7 +213,7 @@ public void previousShouldReturn2ndToLastElement() throws Exception {
214213
}
215214

216215
@Test
217-
public void addShouldAddArtifact() throws Exception {
216+
void addShouldAddArtifact() throws Exception {
218217
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
219218
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
220219
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1);
@@ -224,25 +223,25 @@ public void addShouldAddArtifact() throws Exception {
224223
assertEquals(2, classesWithSameName.getAllArtifactsThisClassWasFoundIn().size());
225224
}
226225

227-
@Test(expected = IllegalArgumentException.class)
228-
public void addShouldThrowWhenClassNameDoesNotMatch() throws Exception {
226+
@Test
227+
void addShouldThrowWhenClassNameDoesNotMatch() throws Exception {
229228
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
230229
ClassFile classFile2 = classFileHelper.createWithContent("some/other/path.class", "");
231230
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1);
232-
233-
classesWithSameName.add(classFile2);
231+
assertThrows(IllegalArgumentException.class, () -> classesWithSameName.add(classFile2));
234232
}
235233

236-
@Test(expected = IllegalArgumentException.class)
237-
public void constructorShouldThrowWhenClassNameDoesNotMatch() throws Exception {
234+
@Test
235+
void constructorShouldThrowWhenClassNameDoesNotMatch() throws Exception {
238236
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
239237
ClassFile classFile2 = classFileHelper.createWithContent("some/other/path.class", "");
240-
241-
new ClassesWithSameName(LOG, classFile1, classFile2);
238+
assertThrows(IllegalArgumentException.class, () -> {
239+
new ClassesWithSameName(LOG, classFile1, classFile2);
240+
});
242241
}
243242

244243
@Test
245-
public void getAllArtifactsThisClassWasFoundInShouldReturnAllArtifacts() throws Exception {
244+
void getAllArtifactsThisClassWasFoundInShouldReturnAllArtifacts() throws Exception {
246245
ClassFile classFile1 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
247246
ClassFile classFile2 = classFileHelper.createWithContent(PATH_TO_CLASS_FILE, "");
248247
ClassesWithSameName classesWithSameName = new ClassesWithSameName(LOG, classFile1, classFile2);

src/test/java/org/codehaus/mojo/extraenforcer/dependencies/EnforceBytecodeVersionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
package org.codehaus.mojo.extraenforcer.dependencies;
1818

19-
import org.junit.Test;
19+
import org.junit.jupiter.api.Test;
2020

21-
import static org.junit.Assert.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
2222

23-
public class EnforceBytecodeVersionTest {
23+
class EnforceBytecodeVersionTest {
2424

2525
@Test
26-
public void renderVersion() {
26+
void renderVersion() {
2727
assertEquals("JDK 1.5", EnforceBytecodeVersion.renderVersion(49, 0));
2828
assertEquals("JDK 1.7", EnforceBytecodeVersion.renderVersion(51, 0));
2929
assertEquals("JDK 11", EnforceBytecodeVersion.renderVersion(55, 0));

src/test/java/org/codehaus/mojo/extraenforcer/dependencies/JarUtilsTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package org.codehaus.mojo.extraenforcer.dependencies;
22

33
import org.apache.maven.artifact.Artifact;
4-
import org.junit.Test;
4+
import org.junit.jupiter.api.Test;
55

66
import static org.codehaus.mojo.extraenforcer.dependencies.ArtifactBuilder.newBuilder;
7-
import static org.junit.Assert.assertFalse;
8-
import static org.junit.Assert.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
99

10-
public class JarUtilsTest {
10+
class JarUtilsTest {
1111
/**
1212
* "Sunny day" test: the method should return true for a jar artifact.
1313
*/
1414
@Test
15-
public void isJarFileShouldReturnTrueForJarFile() {
15+
void isJarFileShouldReturnTrueForJarFile() {
1616
Artifact artifact = newBuilder().withType("jar").build();
1717
assertTrue(JarUtils.isJarFile(artifact));
1818
}
@@ -22,7 +22,7 @@ public void isJarFileShouldReturnTrueForJarFile() {
2222
* a folder with a bunch of packages/class files in it).
2323
*/
2424
@Test
25-
public void isJarFileShouldReturnFalseForDirectory() {
25+
void isJarFileShouldReturnFalseForDirectory() {
2626
Artifact artifact = newBuilder().withType("jar").withAnyDirectory().build();
2727
assertFalse(JarUtils.isJarFile(artifact));
2828
}
@@ -32,7 +32,7 @@ public void isJarFileShouldReturnFalseForDirectory() {
3232
* not "jar". For example: a war or a zip file.
3333
*/
3434
@Test
35-
public void isJarFileShouldReturnFalseWhenArtifactTypeIsNotJar() {
35+
void isJarFileShouldReturnFalseWhenArtifactTypeIsNotJar() {
3636
Artifact artifact = newBuilder().withType("war").build();
3737
assertFalse(JarUtils.isJarFile(artifact));
3838
}

src/test/java/org/codehaus/mojo/extraenforcer/encoding/RequireEncodingTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
77
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
88
import org.apache.maven.project.MavenProject;
9-
import org.junit.Before;
10-
import org.junit.Test;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
1111

12-
import static org.junit.Assert.assertThrows;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
1313
import static org.mockito.Mockito.mock;
1414
import static org.mockito.Mockito.when;
1515

16-
public class RequireEncodingTest {
16+
class RequireEncodingTest {
1717

1818
private RequireEncoding rule;
1919

2020
private MavenProject project;
2121

22-
@Before
23-
public void initFields() {
22+
@BeforeEach
23+
void initFields() {
2424
project = mock(MavenProject.class);
2525
rule = new RequireEncoding(project);
2626
rule.setLog(mock(EnforcerLogger.class));
2727
}
2828

2929
@Test
30-
public void failUTF8() throws Exception {
30+
void failUTF8() throws Exception {
3131

3232
when(project.getBasedir()).thenReturn(new File("src/test/resources").getAbsoluteFile());
3333

0 commit comments

Comments
 (0)