aslak

Arquillian - Maven Surefire - Bug

Jul 15th, 2010
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.79 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 and the JUnit 4.7 Runner.
  34.  *
  35.  * -------------------------------------------------------
  36.  *  T E S T S
  37.  *  -------------------------------------------------------
  38.  *
  39.  *  Results :
  40.  *
  41.  *  Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
  42.  *  
  43.  *  [INFO] ------------------------------------------------------------------------
  44.  *  [INFO] BUILD FAILURE
  45.  *  [INFO] ------------------------------------------------------------------------
  46.  *  [INFO] Total time: 11.011s
  47.  *  [INFO] Finished at: Thu Jul 15 13:59:14 CEST 2010
  48.  *  [INFO] Final Memory: 24M/355M
  49.  *  [INFO] ------------------------------------------------------------------------
  50.  *  [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test (default-test) on project xxxxxx: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
  51.  *
  52.  *
  53.  *  <dependency>
  54.  *      <groupId>junit</groupId>
  55.  *      <artifactId>junit</artifactId>
  56.  *      <version>4.8.1</version>
  57.  *      <scope>test</scope>
  58.  *  </dependency>
  59.  *
  60.  *  <dependency>
  61.  *      <groupId>org.apache.maven.surefire</groupId>
  62.  *      <artifactId>surefire-booter</artifactId>
  63.  *      <version>2.5</version>
  64.  *      <scope>test</scope>
  65.  *      </dependency>
  66.  *  <dependency>
  67.  *      <groupId>org.apache.maven.plugins</groupId>
  68.  *      <artifactId>maven-surefire-plugin</artifactId>
  69.  *      <version>2.5</version>
  70.  *      <scope>test</scope>
  71.  *  </dependency>
  72.  *  <dependency>
  73.  *      <groupId>org.apache.maven.surefire</groupId>
  74.  *      <artifactId>surefire-junit47</artifactId>
  75.  *      <version>2.5</version>
  76.  *      <scope>test</scope>
  77.  *  </dependency>
  78.  *
  79.  * @author <a href="mailto:[email protected]">Aslak Knutsen</a>
  80.  * @version $Revision: $
  81.  */
  82. public class MavenSurefireJUnit47RunnerTestCase
  83. {
  84.  
  85.    @Test
  86.    public void surefireShouldBeAbleToReportRunStatusEvenWithFailingTests() throws Exception
  87.    {
  88.       ConsoleReporter consoleReporter = new ConsoleReporter(true);
  89.       ReporterManager reporterManager = new ReporterManager(Arrays.asList(consoleReporter));
  90.  
  91.       RunListener mavenRunListener = new DemultiplexingRunListener(
  92.             new JUnitCoreTestSetReporter(reporterManager));
  93.      
  94.       Computer computer = new Computer();
  95.      
  96.       JUnitCore junitCore = new JUnitCore();
  97.       junitCore.addListener(mavenRunListener);
  98.      
  99.       Result result = junitCore.run(computer, FailingTestClassTest.class);
  100.      
  101.       junitCore.removeListener(mavenRunListener);
  102.  
  103.       Assert.assertEquals(
  104.             "JUnit should report correctly number of test ran(Finished)",
  105.             0,
  106.             result.getRunCount());
  107.  
  108.       Assert.assertEquals(
  109.             "JUnit should report correctly number Errors produced by the test run",
  110.             2,
  111.             result.getFailureCount());
  112.  
  113.       Assert.assertEquals(
  114.             "The exception thrown by the failing TestCase",
  115.             RuntimeException.class,
  116.             result.getFailures().get(0).getException().getClass());
  117.      
  118.       /*
  119.        * Assumption:
  120.        * The DemultiplexingRunListener assumes a Test will be Started before it Fails or Finishes.  
  121.        *
  122.        * Reality:
  123.        * JUnits ParentRunner is responsible for adding the BeforeClass/AfterClass statements to the statement execution
  124.        * chain. After BeforeClass is executed, a Statement that delegates to the abstract method: runChild(T child, RunNotifier notifier)
  125.        * is called. As the JavaDoc explains: "Subclasses are responsible for making sure that relevant test events are
  126.        * reported through {@code notifier}". When a @BeforeClass fail, the child that should handle the relevant test events(Started, Failed, Finished)
  127.        * is never executed.
  128.        *
  129.        * Result:
  130.        * When Test Failed event is received in DemultiplexingRunListener without a Started event received first, it causes a NullPointException
  131.        * because there is no ClassReporter setup for that class yet. When this Exception is thrown from the DemultiplexingRunListene, JUnit catches the
  132.        * exception and reports is as a Failed test. But to avoid a wild loop, it removes the failing Listener before calling Failed test again.
  133.        * Since the DemultiplexingRunListener now is removed from the chain it will never receive the RunFinished event and the recorded state will
  134.        * never be replayed on the ReportManager.
  135.        *
  136.        * The End result: ReportManager falsely believe no Test were run.
  137.        *
  138.        */
  139.       Assert.assertEquals(
  140.             "The exception thrown by the failing Maven RunListener",
  141.             NullPointerException.class,
  142.             result.getFailures().get(1).getException().getClass());
  143.  
  144.       result.getFailures().get(1).getException().printStackTrace();
  145.      
  146.       // This reports 0 when a TestCase fail in @BeforeClass
  147.       Assert.assertEquals(
  148.             "Maven should report correctly number of test ran",
  149.             1,
  150.             reporterManager.getNbTests());
  151.      
  152.       reporterManager.runCompleted();
  153.       reporterManager.reset();      
  154.    }
  155.  
  156.    /**
  157.     * Simple TestCase to force a Exception in @BeforeClass.
  158.     *
  159.     */
  160.    public static class FailingTestClassTest
  161.    {
  162.       @BeforeClass
  163.       public static void failingBeforeClass() throws Exception
  164.       {
  165.          throw new RuntimeException("Opps, we failed in @BeforeClass");
  166.       }
  167.  
  168.       @Test
  169.       public void shouldNeverBeCalled() throws Exception
  170.       {
  171.          Assert.assertTrue(true);
  172.       }
  173.    }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment