aslak

Arquillian - Maven Surefire - Bug

Jul 15th, 2010
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.83 KB | None | 0 0
  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2009, Red Hat Middleware LLC, and individual contributors
  4.  * by the @authors tag. See the copyright.txt in the distribution for a
  5.  * full listing of individual contributors.
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.maven.surefire.junitcore;
  18.  
  19. import java.util.Arrays;
  20.  
  21. import junit.framework.Assert;
  22.  
  23. import org.apache.maven.surefire.report.ConsoleReporter;
  24. import org.apache.maven.surefire.report.ReporterManager;
  25. import org.junit.BeforeClass;
  26. import org.junit.Test;
  27. import org.junit.runner.Computer;
  28. import org.junit.runner.JUnitCore;
  29. import org.junit.runner.Result;
  30. import org.junit.runner.notification.RunListener;
  31.  
  32. /**
  33.  * TestCase that expose "No tests were executed!" on Test failure using Maven Surefire 2.5
  34.  * and the JUnit 4.7 Runner.
  35.  *
  36.  * -------------------------------------------------------
  37.  *  T E S T S
  38.  *  -------------------------------------------------------
  39.  *
  40.  *  Results :
  41.  *
  42.  *  Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
  43.  *  
  44.  *  [INFO] ------------------------------------------------------------------------
  45.  *  [INFO] BUILD FAILURE
  46.  *  [INFO] ------------------------------------------------------------------------
  47.  *  [INFO] Total time: 11.011s
  48.  *  [INFO] Finished at: Thu Jul 15 13:59:14 CEST 2010
  49.  *  [INFO] Final Memory: 24M/355M
  50.  *  [INFO] ------------------------------------------------------------------------
  51.  *  [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
  52.  *  (default-test) on project xxxxxx: No tests were executed!  (Set -DfailIfNoTests=false to
  53.  *  ignore this error.) -> [Help 1]
  54.  *
  55.  *
  56.  *  <dependency>
  57.  *      <groupId>junit</groupId>
  58.  *      <artifactId>junit</artifactId>
  59.  *      <version>4.8.1</version>
  60.  *      <scope>test</scope>
  61.  *  </dependency>
  62.  *
  63.  *  <dependency>
  64.  *      <groupId>org.apache.maven.surefire</groupId>
  65.  *      <artifactId>surefire-booter</artifactId>
  66.  *      <version>2.5</version>
  67.  *      <scope>test</scope>
  68.  *      </dependency>
  69.  *  <dependency>
  70.  *      <groupId>org.apache.maven.plugins</groupId>
  71.  *      <artifactId>maven-surefire-plugin</artifactId>
  72.  *      <version>2.5</version>
  73.  *      <scope>test</scope>
  74.  *  </dependency>
  75.  *  <dependency>
  76.  *      <groupId>org.apache.maven.surefire</groupId>
  77.  *      <artifactId>surefire-junit47</artifactId>
  78.  *      <version>2.5</version>
  79.  *      <scope>test</scope>
  80.  *  </dependency>
  81.  *
  82.  * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
  83.  * @version $Revision: $
  84.  */
  85. public class MavenSurefireJUnit47RunnerTestCase
  86. {
  87.  
  88.    @Test
  89.    public void surefireShouldBeAbleToReportRunStatusEvenWithFailingTests() throws Exception
  90.    {
  91.       ConsoleReporter consoleReporter = new ConsoleReporter(true);
  92.       ReporterManager reporterManager = new ReporterManager(Arrays.asList(consoleReporter));
  93.  
  94.       RunListener mavenRunListener = new DemultiplexingRunListener(
  95.             new JUnitCoreTestSetReporter(reporterManager));
  96.      
  97.       Computer computer = new Computer();
  98.      
  99.       JUnitCore junitCore = new JUnitCore();
  100.       junitCore.addListener(mavenRunListener);
  101.      
  102.       Result result = junitCore.run(computer, FailingTestClassTest.class);
  103.      
  104.       junitCore.removeListener(mavenRunListener);
  105.  
  106.       Assert.assertEquals(
  107.             "JUnit should report correctly number of test ran(Finished)",
  108.             0,
  109.             result.getRunCount());
  110.  
  111.       Assert.assertEquals(
  112.             "JUnit should report correctly number Errors produced by the test run",
  113.             2,
  114.             result.getFailureCount());
  115.  
  116.       Assert.assertEquals(
  117.             "The exception thrown by the failing TestCase",
  118.             RuntimeException.class,
  119.             result.getFailures().get(0).getException().getClass());
  120.      
  121.       /*
  122.        * Assumption:
  123.        * The DemultiplexingRunListener assumes a Test will be Started before it Fails or Finishes.  
  124.        *
  125.        * Reality:
  126.        * JUnits ParentRunner is responsible for adding the BeforeClass/AfterClass statements to the
  127.        * statement execution chain. After BeforeClass is executed, a Statement that delegates to the
  128.        * abstract method: runChild(T child, RunNotifier notifier) is called. As the JavaDoc explains:
  129.        * "Subclasses are responsible for making sure that relevant test events are reported through {@code notifier}".
  130.        * When a @BeforeClass fail, the child that should handle the relevant test events(Started, Failed, Finished)
  131.        * is never executed.
  132.        *
  133.        * Result:
  134.        * When Test Failed event is received in DemultiplexingRunListener without a Started event received first,
  135.        * it causes a NullPointException because there is no ClassReporter setup for that class yet. When this Exception
  136.        * is thrown from the DemultiplexingRunListene, JUnit catches the exception and reports is as a Failed test.
  137.        * But to avoid a wild loop, it removes the failing Listener before calling Failed test again. Since the
  138.        * DemultiplexingRunListener now is removed from the chain it will never receive the RunFinished event
  139.        * and the recorded state will never be replayed on the ReportManager.
  140.        *
  141.        * The End result: ReportManager falsely believe no Test were run.
  142.        *
  143.        */
  144.       Assert.assertEquals(
  145.             "The exception thrown by the failing Maven RunListener",
  146.             NullPointerException.class,
  147.             result.getFailures().get(1).getException().getClass());
  148.  
  149.       result.getFailures().get(1).getException().printStackTrace();
  150.      
  151.       // This reports 0 when a TestCase fail in @BeforeClass
  152.       Assert.assertEquals(
  153.             "Maven should report correctly number of test ran",
  154.             1,
  155.             reporterManager.getNbTests());
  156.      
  157.       reporterManager.runCompleted();
  158.       reporterManager.reset();      
  159.    }
  160.  
  161.    /**
  162.     * Simple TestCase to force a Exception in @BeforeClass.
  163.     *
  164.     */
  165.    public static class FailingTestClassTest
  166.    {
  167.       @BeforeClass
  168.       public static void failingBeforeClass() throws Exception
  169.       {
  170.          throw new RuntimeException("Opps, we failed in @BeforeClass");
  171.       }
  172.  
  173.       @Test
  174.       public void shouldNeverBeCalled() throws Exception
  175.       {
  176.          Assert.assertTrue(true);
  177.       }
  178.    }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment