View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.its;
20  
21  import java.util.ArrayList;
22  
23  import org.apache.maven.it.VerificationException;
24  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.Parameterized;
29  import org.junit.runners.Parameterized.Parameter;
30  import org.junit.runners.Parameterized.Parameters;
31  
32  import static org.hamcrest.Matchers.containsString;
33  import static org.hamcrest.Matchers.equalTo;
34  
35  /**
36   * Tests using the JUnit 47 provider to rerun failing tests with the cucumber runner. The main problem that the junit4
37   * provider has with the cucumber runner is that the junit Description instance created by the runner has a null test
38   * class attribute. This requires that tests are rerun based on their description.
39   *
40   * @author mpkorstanje
41   */
42  @RunWith(Parameterized.class)
43  @SuppressWarnings("checkstyle:magicnumber")
44  public class JUnit47RerunFailingTestWithCucumberIT extends SurefireJUnit4IntegrationTestCase {
45      private static final String LEGACY_FORK_NODE = "org.apache.maven.plugin.surefire.extensions.LegacyForkNodeFactory";
46  
47      private static final String SUREFIRE_FORK_NODE =
48              "org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory";
49  
50      @Parameters
51      public static Iterable<Object[]> data() {
52          ArrayList<Object[]> args = new ArrayList<>();
53          args.add(new Object[] {"tcp"});
54          args.add(new Object[] {null});
55          return args;
56      }
57  
58      @Parameter
59      @SuppressWarnings("checkstyle:visibilitymodifier")
60      public String profileId;
61  
62      private SurefireLauncher unpack() {
63          SurefireLauncher launcher =
64                  unpack("junit47-rerun-failing-tests-with-cucumber", profileId == null ? "" : "-" + profileId);
65  
66          if (profileId != null) {
67              launcher.activateProfile(profileId);
68          }
69  
70          return launcher;
71      }
72  
73      @Test
74      public void testRerunFailingErrorTestsFalse() throws VerificationException {
75          String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
76          unpack().debugLogging()
77                  .maven()
78                  .sysProp("surefire.rerunFailingTestsCount", 0)
79                  .withFailure()
80                  .executeTest()
81                  .assertTestSuiteResults(1, 0, 1, 0, 0)
82                  .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
83      }
84  
85      @Test
86      public void testRerunFailingErrorTestsWithOneRetry() throws VerificationException {
87          String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
88          unpack().debugLogging()
89                  .maven()
90                  .sysProp("surefire.rerunFailingTestsCount", 1)
91                  .withFailure()
92                  .executeTest()
93                  .assertTestSuiteResults(1, 0, 1, 0, 0)
94                  .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
95      }
96  
97      @Test
98      public void testRerunFailingErrorTestsTwoRetry() throws VerificationException {
99          String cls = profileId == null ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
100         unpack().maven()
101                 .debugLogging()
102                 .sysProp("surefire.rerunFailingTestsCount", 2)
103                 .executeTest()
104                 .assertTestSuiteResults(1, 0, 0, 0, 2)
105                 .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
106     }
107 }