1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.surefire;
20
21 import java.io.File;
22
23 import junit.framework.TestCase;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.surefire.api.suite.RunResult;
26 import org.junit.Rule;
27 import org.junit.rules.ExpectedException;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30
31
32
33
34 public class SurefireMojoTest extends TestCase {
35 @Rule
36 public final ExpectedException e = ExpectedException.none();
37
38 public void testDefaultIncludes() {
39 assertThat(new SurefireMojo().getDefaultIncludes())
40 .containsOnly("**/Test*.java", "**/*Test.java", "**/*Tests.java", "**/*TestCase.java");
41 }
42
43 public void testReportSchemaLocation() {
44 assertThat(new SurefireMojo().getReportSchemaLocation())
45 .isEqualTo("https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd");
46 }
47
48 public void testFailIfNoTests() throws Exception {
49 RunResult runResult = new RunResult(0, 0, 0, 0);
50 try {
51 SurefireMojo mojo = new SurefireMojo();
52 mojo.setFailIfNoTests(true);
53 mojo.handleSummary(runResult, null);
54 } catch (MojoFailureException e) {
55 assertThat(e.getLocalizedMessage())
56 .isEqualTo("No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)");
57 return;
58 }
59 fail("Expected MojoFailureException with message "
60 + "'No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)'");
61 }
62
63 public void testTestFailure() throws Exception {
64 RunResult runResult = new RunResult(1, 0, 1, 0);
65 try {
66 SurefireMojo mojo = new SurefireMojo();
67 mojo.handleSummary(runResult, null);
68 } catch (MojoFailureException e) {
69 assertThat(e.getLocalizedMessage())
70 .isEqualTo("There are test failures.\n\nSee null "
71 + "for the individual test results.\nSee dump files (if any exist) "
72 + "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream.");
73 return;
74 }
75 fail("Expected MojoFailureException with message "
76 + "'There are test failures.\n\nSee null "
77 + "for the individual test results.\nSee dump files (if any exist) "
78 + "[date].dump, [date]-jvmRun[N].dump and [date].dumpstream.'");
79 }
80
81 public void testPluginName() {
82 assertThat(new SurefireMojo().getPluginName()).isEqualTo("surefire");
83 }
84
85 public void testShouldGetNullEnv() {
86 SurefireMojo mojo = new SurefireMojo();
87 assertThat(mojo.getExcludedEnvironmentVariables()).hasSize(0);
88 }
89
90 public void testShouldGetEnv() {
91 SurefireMojo mojo = new SurefireMojo();
92 mojo.setExcludedEnvironmentVariables(new String[] {"ABC", "KLM"});
93 assertThat(mojo.getExcludedEnvironmentVariables()).hasSize(2).contains("ABC", "KLM");
94 }
95
96 public void testShouldGetPropertyFile() {
97 SurefireMojo mojo = new SurefireMojo();
98 mojo.setSystemPropertiesFile(new File("testShouldGetPropertyFile"));
99 assertThat(mojo.getSystemPropertiesFile()).isEqualTo(new File("testShouldGetPropertyFile"));
100 }
101
102 public void testNegativeFailOnFlakeCount() {
103 SurefireMojo mojo = new SurefireMojo();
104 mojo.setFailOnFlakeCount(-1);
105 e.expect(MojoFailureException.class);
106 e.expectMessage("Parameter \"failOnFlakeCount\" should not be negative.");
107 }
108
109 public void testFailOnFlakeCountWithoutRerun() {
110 SurefireMojo mojo = new SurefireMojo();
111 mojo.setFailOnFlakeCount(1);
112 e.expect(MojoFailureException.class);
113 e.expectMessage("\"failOnFlakeCount\" requires rerunFailingTestsCount to be at least 1.");
114 }
115
116 public void testShouldHaveJUnit5EnginesFilter() {
117 SurefireMojo mojo = new SurefireMojo();
118
119 mojo.setIncludeJUnit5Engines(new String[] {"e1", "e2"});
120 assertThat(mojo.getIncludeJUnit5Engines()).isEqualTo(new String[] {"e1", "e2"});
121
122 mojo.setExcludeJUnit5Engines(new String[] {"e1", "e2"});
123 assertThat(mojo.getExcludeJUnit5Engines()).isEqualTo(new String[] {"e1", "e2"});
124 }
125 }