Skip to content

Commit cdaf267

Browse files
authored
JUnit 5 best practices (#505)
1 parent f3f5997 commit cdaf267

File tree

3 files changed

+38
-39
lines changed

3 files changed

+38
-39
lines changed

src/test/java/org/codehaus/mojo/exec/ExecJavaMojoTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void beforeEach() {
8888
@Test
8989
@InjectMojo(goal = "java")
9090
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.HelloRunnable")
91-
void testRunnable(ExecJavaMojo mojo) throws Exception {
91+
void runnable(ExecJavaMojo mojo) throws Exception {
9292
doExecute(mojo, null, null);
9393
assertEquals("junit: true", session.getSystemProperties().getProperty("hello.runnable.output"));
9494
}
@@ -102,15 +102,15 @@ void testRunnable(ExecJavaMojo mojo) throws Exception {
102102
@Test
103103
@InjectMojo(goal = "java")
104104
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.DummyMain")
105-
void testSimpleRun(ExecJavaMojo mojo) throws Exception {
105+
void simpleRun(ExecJavaMojo mojo) throws Exception {
106106
String output = execute(mojo);
107107
assertEquals("Hello" + System.lineSeparator(), output);
108108
}
109109

110110
@Test
111111
@InjectMojo(goal = "java")
112112
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain1")
113-
void testJSR512InstanceMainNoArgs(ExecJavaMojo mojo) throws Exception {
113+
void jsr512InstanceMainNoArgs(ExecJavaMojo mojo) throws Exception {
114114
String output = execute(mojo);
115115
assertEquals("Correct choice" + System.lineSeparator(), output);
116116
}
@@ -119,23 +119,23 @@ void testJSR512InstanceMainNoArgs(ExecJavaMojo mojo) throws Exception {
119119
@InjectMojo(goal = "java")
120120
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain2")
121121
@MojoParameter(name = "arguments", value = "arg1,arg2")
122-
void testJSR512PrefersStringArrayArgs(ExecJavaMojo mojo) throws Exception {
122+
void jsr512PrefersStringArrayArgs(ExecJavaMojo mojo) throws Exception {
123123
String output = execute(mojo);
124124
assertEquals("Correct choice arg1 arg2" + System.lineSeparator(), output);
125125
}
126126

127127
@Test
128128
@InjectMojo(goal = "java")
129129
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain3")
130-
void testJSR512StaticMainNoArgs(ExecJavaMojo mojo) throws Exception {
130+
void jsr512StaticMainNoArgs(ExecJavaMojo mojo) throws Exception {
131131
String output = execute(mojo);
132132
assertEquals("Correct choice" + System.lineSeparator(), output);
133133
}
134134

135135
@Test
136136
@InjectMojo(goal = "java")
137137
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain6")
138-
public void testJSR512PackagePrivateStaticMainNoArgs(ExecJavaMojo mojo) throws Exception {
138+
void jsr512PackagePrivateStaticMainNoArgs(ExecJavaMojo mojo) throws Exception {
139139
String output = execute(mojo);
140140
assertEquals("Correct choice" + System.lineSeparator(), output);
141141
}
@@ -144,15 +144,15 @@ public void testJSR512PackagePrivateStaticMainNoArgs(ExecJavaMojo mojo) throws E
144144
@InjectMojo(goal = "java")
145145
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain7")
146146
@MojoParameter(name = "arguments", value = "arg1,arg2")
147-
public void testJSR512PackagePrivateStaticMainWithArgs(ExecJavaMojo mojo) throws Exception {
147+
void jsr512PackagePrivateStaticMainWithArgs(ExecJavaMojo mojo) throws Exception {
148148
String output = execute(mojo);
149149
assertEquals("Correct choice arg1 arg2" + System.lineSeparator(), output);
150150
}
151151

152152
@Test
153153
@InjectMojo(goal = "java")
154154
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain4")
155-
void testJSR512FailureInstanceMainPrivateNoArgsConstructor(ExecJavaMojo mojo) {
155+
void jsr512FailureInstanceMainPrivateNoArgsConstructor(ExecJavaMojo mojo) {
156156
MojoExecutionException exception = assertThrows(MojoExecutionException.class, () -> execute(mojo));
157157
assertEquals(
158158
"The specified mainClass doesn't contain a main method with appropriate signature.",
@@ -163,7 +163,7 @@ void testJSR512FailureInstanceMainPrivateNoArgsConstructor(ExecJavaMojo mojo) {
163163
@InjectMojo(goal = "java")
164164
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.JSR512DummyMain5")
165165
@MojoParameter(name = "arguments", value = "arg1,arg2")
166-
void testJSR512InheritedMain(ExecJavaMojo mojo) throws Exception {
166+
void jsr512InheritedMain(ExecJavaMojo mojo) throws Exception {
167167
String output = execute(mojo);
168168
assertEquals("Correct choice arg1 arg2" + System.lineSeparator(), output);
169169
}
@@ -177,7 +177,7 @@ void testJSR512InheritedMain(ExecJavaMojo mojo) throws Exception {
177177
*/
178178
@Test
179179
@InjectMojo(goal = "java", pom = "src/test/projects/project5/pom.xml")
180-
void testEmptySystemProperty(ExecJavaMojo mojo) throws Exception {
180+
void emptySystemProperty(ExecJavaMojo mojo) throws Exception {
181181

182182
assertNull(System.getProperty("test.name"), "System property not yet created");
183183

@@ -196,7 +196,7 @@ void testEmptySystemProperty(ExecJavaMojo mojo) throws Exception {
196196
@Test
197197
@InjectMojo(goal = "java")
198198
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.ThrowingMain")
199-
void testRunWhichThrowsExceptionIsNotWrappedInInvocationTargetException(ExecJavaMojo mojo) {
199+
void runWhichThrowsExceptionIsNotWrappedInInvocationTargetException(ExecJavaMojo mojo) {
200200
MojoExecutionException exception = assertThrows(MojoExecutionException.class, () -> execute(mojo));
201201
assertInstanceOf(IOException.class, exception.getCause());
202202
assertEquals("expected IOException thrown by test", exception.getCause().getMessage());
@@ -255,7 +255,7 @@ void testRunWhichThrowsExceptionIsNotWrappedInInvocationTargetException(ExecJava
255255
@InjectMojo(goal = "java")
256256
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.MainWithThreads")
257257
@MojoParameter(name = "arguments", value = "cancelTimer")
258-
void testWaitNoDaemonThreads(ExecJavaMojo mojo) throws Exception {
258+
void waitNoDaemonThreads(ExecJavaMojo mojo) throws Exception {
259259
String output = execute(mojo);
260260
assertEquals(MainWithThreads.ALL_EXITED, output.trim());
261261
}
@@ -271,7 +271,7 @@ void testWaitNoDaemonThreads(ExecJavaMojo mojo) throws Exception {
271271
@InjectMojo(goal = "java")
272272
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.MainWithThreads")
273273
@MojoParameter(name = "daemonThreadJoinTimeout", value = "4000")
274-
void testWaitNonInterruptibleDaemonThreads(ExecJavaMojo mojo) throws Exception {
274+
void waitNonInterruptibleDaemonThreads(ExecJavaMojo mojo) throws Exception {
275275
String output = execute(mojo);
276276
assertEquals(MainWithThreads.TIMER_IGNORED, output.trim());
277277
}
@@ -290,7 +290,7 @@ void testWaitNonInterruptibleDaemonThreads(ExecJavaMojo mojo) throws Exception {
290290
@MojoParameter(name = "daemonThreadJoinTimeout", value = "3000")
291291
@MojoParameter(name = "stopUnresponsiveDaemonThreads", value = "true")
292292
@EnabledForJreRange(max = JRE.JAVA_19)
293-
void testUncooperativeThread(ExecJavaMojo mojo) throws Exception {
293+
void uncooperativeThread(ExecJavaMojo mojo) throws Exception {
294294
String output = execute(mojo);
295295
// note: execute() will wait a little bit before returning the output,
296296
// thereby allowing the stop()'ed thread to output the final "(f)".
@@ -303,7 +303,7 @@ void testUncooperativeThread(ExecJavaMojo mojo) throws Exception {
303303
@MojoParameter(name = "daemonThreadJoinTimeout", value = "3000")
304304
@MojoParameter(name = "stopUnresponsiveDaemonThreads", value = "true")
305305
@EnabledForJreRange(min = JRE.JAVA_20)
306-
void testUncooperativeThreadJdk20(ExecJavaMojo mojo) throws Exception {
306+
void uncooperativeThreadJdk20(ExecJavaMojo mojo) throws Exception {
307307
String output = execute(mojo);
308308
// note: execute() will wait a little bit before returning the output,
309309
// thereby allowing the stop()'ed thread to output the final "(f)".
@@ -332,7 +332,7 @@ void testUncooperativeThreadJdk20(ExecJavaMojo mojo) throws Exception {
332332
@InjectMojo(goal = "java")
333333
@MojoParameter(name = "mainClass", value = "org.codehaus.mojo.exec.DummyMain")
334334
@MojoParameter(name = "commandlineArgs", value = "\"Arg1\" \"Arg2a Arg2b\"")
335-
void testRunWithArgs(ExecJavaMojo mojo) throws Exception {
335+
void runWithArgs(ExecJavaMojo mojo) throws Exception {
336336
String resultString = execute(mojo);
337337

338338
String LS = System.lineSeparator();
@@ -347,7 +347,7 @@ void testRunWithArgs(ExecJavaMojo mojo) throws Exception {
347347
*/
348348
@Test
349349
@InjectMojo(goal = "java", pom = "src/test/projects/project16/pom.xml")
350-
void testExcludedClasspathElementSlf4jSimple(ExecJavaMojo mojo) throws Exception {
350+
void excludedClasspathElementSlf4jSimple(ExecJavaMojo mojo) throws Exception {
351351
String LS = System.lineSeparator();
352352

353353
// slf4j-simple
@@ -362,7 +362,7 @@ void testExcludedClasspathElementSlf4jSimple(ExecJavaMojo mojo) throws Exception
362362

363363
@Test
364364
@InjectMojo(goal = "java", pom = "src/test/projects/project17/pom.xml")
365-
void testExcludedClasspathElementSlf4jJdk14(ExecJavaMojo mojo) throws Exception {
365+
void excludedClasspathElementSlf4jJdk14(ExecJavaMojo mojo) throws Exception {
366366
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
367367
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
368368
execute(mojo, stdout, stderr);
@@ -379,7 +379,7 @@ void testExcludedClasspathElementSlf4jJdk14(ExecJavaMojo mojo) throws Exception
379379
*/
380380
@Test
381381
@InjectMojo(goal = "java", pom = "src/test/projects/project18/pom.xml")
382-
void testProjectProperties(ExecJavaMojo mojo) throws Exception {
382+
void projectProperties(ExecJavaMojo mojo) throws Exception {
383383
// only mojo configuration is taken from the pom, we have to mock the project properties ourselves
384384
Properties properties = new Properties();
385385
properties.put("test.name", "project18 project");

src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import javax.inject.Inject;
1313

1414
import java.io.File;
15-
import java.io.IOException;
1615
import java.io.OutputStream;
1716
import java.nio.charset.StandardCharsets;
1817
import java.nio.file.Files;
@@ -141,7 +140,7 @@ void setUp() throws Exception {
141140
}
142141

143142
@Test
144-
void testRunOK() throws MojoExecutionException {
143+
void runOK() throws Exception {
145144
mojo.execute();
146145

147146
checkMojo(SOME_EXECUTABLE + " --version");
@@ -163,7 +162,7 @@ void testRunOK() throws MojoExecutionException {
163162
// MEXEC-12, MEXEC-72
164163
@Test
165164
@InjectMojo(goal = "exec")
166-
void testGetExecutablePath(ExecMojo realMojo) throws IOException {
165+
void getExecutablePath(ExecMojo realMojo) throws Exception {
167166

168167
File workdir = new File(System.getProperty("user.dir"));
169168
Map<String, String> enviro = new HashMap<>();
@@ -216,7 +215,7 @@ void testGetExecutablePath(ExecMojo realMojo) throws IOException {
216215
// see https://github.com/mojohaus/exec-maven-plugin/issues/42
217216
@Test
218217
@EnabledOnOs(OS.WINDOWS)
219-
void testGetExecutablePathPreferExecutableExtensionsOnWindows() throws IOException {
218+
void getExecutablePathPreferExecutableExtensionsOnWindows() throws Exception {
220219
// this test is for Windows
221220
final ExecMojo realMojo = new ExecMojo(repositorySystem, toolchainManager);
222221

@@ -246,7 +245,7 @@ void testGetExecutablePathPreferExecutableExtensionsOnWindows() throws IOExcepti
246245
}
247246

248247
@Test
249-
void testRunFailure() {
248+
void runFailure() {
250249
mojo.executeResult = 1;
251250

252251
try {
@@ -260,7 +259,7 @@ void testRunFailure() {
260259
}
261260

262261
@Test
263-
void testRunError() {
262+
void runError() {
264263
mojo.failureMsg = "simulated failure";
265264

266265
try {
@@ -274,15 +273,15 @@ void testRunError() {
274273
}
275274

276275
@Test
277-
void testOverrides() throws MojoExecutionException {
276+
void overrides() throws Exception {
278277
mojo.systemProperties.put("exec.args", "-f pom.xml");
279278
mojo.execute();
280279

281280
checkMojo(SOME_EXECUTABLE + " -f pom.xml");
282281
}
283282

284283
@Test
285-
void testOverrides3() throws MojoExecutionException {
284+
void overrides3() throws Exception {
286285
mojo.systemProperties.put("exec.args", null);
287286
mojo.execute();
288287

@@ -297,7 +296,7 @@ void testOverrides3() throws MojoExecutionException {
297296

298297
@Test
299298
@InjectMojo(goal = "exec")
300-
void testIsResultCodeAFailure(ExecMojo execMojo) {
299+
void isResultCodeAFailure(ExecMojo execMojo) {
301300
assertTrue(execMojo.isResultCodeAFailure(1));
302301
assertFalse(execMojo.isResultCodeAFailure(0));
303302

@@ -315,7 +314,7 @@ void testIsResultCodeAFailure(ExecMojo execMojo) {
315314
// MEXEC-81
316315
@Test
317316
@InjectMojo(goal = "exec")
318-
void testParseCommandlineOSWin(ExecMojo execMojo) throws Exception {
317+
void parseCommandlineOSWin(ExecMojo execMojo) throws Exception {
319318
final String javaHome = "C:\\Java\\jdk1.5.0_15";
320319
// can only be set by expression or plugin-configuration
321320
setVariableValueToObject(execMojo, "commandlineArgs", javaHome);
@@ -325,7 +324,7 @@ void testParseCommandlineOSWin(ExecMojo execMojo) throws Exception {
325324

326325
@Test
327326
@InjectMojo(goal = "exec")
328-
void test_exec_receives_all_parameters(ExecMojo execMojo) throws MojoExecutionException {
327+
void exec_receives_all_parameters(ExecMojo execMojo) throws Exception {
329328
// given
330329
execMojo.setExecutable("mkdir");
331330
execMojo.setArguments(Arrays.asList("-p", "dist/mails"));
@@ -342,7 +341,7 @@ void test_exec_receives_all_parameters(ExecMojo execMojo) throws MojoExecutionEx
342341
@Test
343342
@InjectMojo(goal = "exec", pom = "src/test/projects/project20/pom.xml")
344343
@DisabledOnOs(OS.WINDOWS)
345-
void testToolchainJavaHomePropertySetWhenToolchainIsUsed(ExecMojo execMojo) throws Exception {
344+
void toolchainJavaHomePropertySetWhenToolchainIsUsed(ExecMojo execMojo) throws Exception {
346345
// given
347346
File basedir;
348347
String testJavaPath;
@@ -366,7 +365,7 @@ void testToolchainJavaHomePropertySetWhenToolchainIsUsed(ExecMojo execMojo) thro
366365
@Test
367366
@InjectMojo(goal = "exec", pom = "src/test/projects/project21/pom.xml")
368367
@EnabledOnOs(OS.WINDOWS)
369-
void testToolchainJavaHomePropertySetWhenToolchainIsUsedWindows(ExecMojo execMojo) throws Exception {
368+
void toolchainJavaHomePropertySetWhenToolchainIsUsedWindows(ExecMojo execMojo) throws Exception {
370369
// given
371370
File basedir;
372371
String testJavaPath;
@@ -420,7 +419,7 @@ private String getCommandLineAsString(CommandLine commandline) {
420419

421420
@Test
422421
@InjectMojo(goal = "exec")
423-
void testGetShebang(ExecMojo execMojo) throws Exception {
422+
void getShebang(ExecMojo execMojo) throws Exception {
424423
// without shebang
425424
File noShebang = Files.createTempFile("noShebang", ".sh").toFile();
426425
Files.write(noShebang.toPath(), Collections.singletonList("echo hello"), StandardCharsets.UTF_8);
@@ -446,7 +445,7 @@ void testGetShebang(ExecMojo execMojo) throws Exception {
446445

447446
@Test
448447
@InjectMojo(goal = "exec")
449-
void testCreateEnvWrapperFile(ExecMojo execMojo) throws Exception {
448+
void createEnvWrapperFile(ExecMojo execMojo) throws Exception {
450449

451450
// without shebang
452451
File envScript = Files.createTempFile("envScript", ".sh").toFile();

src/test/java/org/codehaus/mojo/exec/LineRedirectOutputStreamTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class LineRedirectOutputStreamTest {
1515

1616
@Test
1717
void givenExtendedUnicodeCharacterOutput_whenRedirectingWithUtf8Charset_thenShouldDecodeProperly()
18-
throws IOException {
18+
throws Exception {
1919
internalTestForCharset(StandardCharsets.UTF_8);
2020
}
2121

2222
@Test
2323
void givenExtendedUnicodeCharacterOutput_whenRedirectingWithIso8859Charset_thenShouldDecodeProperly()
24-
throws IOException {
24+
throws Exception {
2525
internalTestForCharset(StandardCharsets.ISO_8859_1);
2626
}
2727

2828
@Test
29-
void givenExtendedUnicodeCharacterOutput_whenRedirectingWithCp1252_thenShouldDecodeProperly() throws IOException {
29+
void givenExtendedUnicodeCharacterOutput_whenRedirectingWithCp1252_thenShouldDecodeProperly() throws Exception {
3030
assumeTrue(
3131
Charset.availableCharsets().containsKey("windows-1252"),
3232
"The JVM does not contain the cp-1252 charset");
@@ -35,13 +35,13 @@ void givenExtendedUnicodeCharacterOutput_whenRedirectingWithCp1252_thenShouldDec
3535

3636
@Test
3737
void givenExtendedUnicodeCharacterOutput_whenRedirectingWithDefaultCharset_thenShouldDecodeProperly()
38-
throws IOException {
38+
throws Exception {
3939
internalTestForCharset(Charset.defaultCharset());
4040
}
4141

4242
@Test
4343
void givenExtendedUnicodeCharacterOutput_whenRedirectingWithCharsetUnspecified_thenShouldDecodeProperly()
44-
throws IOException {
44+
throws Exception {
4545
internalTestForCharset(sb -> new LineRedirectOutputStream(sb::append), Charset.defaultCharset());
4646
}
4747

0 commit comments

Comments
 (0)