Package org.jboss.arquillian.junit

Source Code of org.jboss.arquillian.junit.JUnitIntegrationTestCase

/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.junit;

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.jboss.arquillian.test.spi.TestMethodExecutor;
import org.jboss.arquillian.test.spi.TestResult;
import org.jboss.arquillian.test.spi.TestRunnerAdaptor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.mockito.runners.MockitoJUnitRunner;


/**
* Verify the that JUnit integration adaptor fires the expected events even when Handlers are failing.
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
@RunWith(MockitoJUnitRunner.class)
public class JUnitIntegrationTestCase extends JUnitTestBaseClass
{
   @Test
   public void shouldNotCallAnyMethodsWithoutLifecycleHandlers() throws Exception
   {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
      when(adaptor.test(isA(TestMethodExecutor.class))).thenReturn(TestResult.passed());
     
      Result result = run(adaptor, ArquillianClass1.class);

      Assert.assertTrue(result.wasSuccessful());
      assertCycle(0, Cycle.values());
     
      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }
  
   @Test
   public void shouldCallAllMethods() throws Exception
   {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
      executeAllLifeCycles(adaptor);
     
      Result result = run(adaptor, ArquillianClass1.class);
     
      Assert.assertTrue(result.wasSuccessful());
      assertCycle(1, Cycle.values());

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }
  
   @Test
   public void shouldCallAfterClassWhenBeforeThrowsException() throws Exception
   {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
      executeAllLifeCycles(adaptor);
     
      throwException(Cycle.BEFORE_CLASS, new Throwable());
     
      Result result = run(adaptor, ArquillianClass1.class);
      Assert.assertFalse(result.wasSuccessful());
     
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.AFTER_CLASS);
      assertCycle(0, Cycle.BEFORE, Cycle.AFTER, Cycle.TEST);
  
      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }

   @Test
   public void shouldCallAfterWhenBeforeThrowsException() throws Exception
   {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
      executeAllLifeCycles(adaptor);
     
      throwException(Cycle.BEFORE, new Throwable());
     
      Result result = run(adaptor, ArquillianClass1.class);
      Assert.assertFalse(result.wasSuccessful());
     
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.AFTER_CLASS, Cycle.BEFORE, Cycle.AFTER);
      assertCycle(0, Cycle.TEST);

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }
  
   @Test
   public void shouldOnlyCallBeforeAfterSuiteOnce() throws Exception
   {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
      executeAllLifeCycles(adaptor);
     
      Result result = run(adaptor, ArquillianClass1.class, ArquillianClass1.class, ArquillianClass1.class, ArquillianClass1.class);
      Assert.assertTrue(result.wasSuccessful());

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }
  
   /*
    * ARQ-391, After not called when Error's are thrown, e.g. AssertionError
    */
   @Test
   public void shouldCallAllWhenTestThrowsException() throws Exception
   {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);
      executeAllLifeCycles(adaptor);

      throwException(Cycle.TEST, new Throwable());

      Result result = run(adaptor, ArquillianClass1.class);
      Assert.assertFalse(result.wasSuccessful());

      assertCycle(1, Cycle.values());

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }

   @Test
   public void shouldWorkWithTimeout() throws Exception {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);

      executeAllLifeCycles(adaptor);

      Result result = run(adaptor, ArquillianClass1WithTimeout.class);

      Assert.assertFalse(result.wasSuccessful());
      Assert.assertTrue(result.getFailures().get(0).getMessage().contains("timed out"));
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.BEFORE, Cycle.AFTER, Cycle.AFTER_CLASS);

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }

   @Test
   public void shouldWorkWithExpectedExceptionRule() throws Exception {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);

      executeAllLifeCycles(adaptor);

      Result result = run(adaptor, ArquillianClass1WithExpectedExceptionRule.class);

      Assert.assertTrue(result.wasSuccessful());
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.BEFORE, Cycle.TEST, Cycle.AFTER, Cycle.AFTER_CLASS);

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }

   @Test
   public void shouldWorkWithExpectedException() throws Exception {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);

      executeAllLifeCycles(adaptor);

      Result result = run(adaptor, ArquillianClass1WithExpectedException.class);

      Assert.assertTrue(result.wasSuccessful());
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.BEFORE, Cycle.TEST, Cycle.AFTER, Cycle.AFTER_CLASS);

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }

   @Test
   public void shouldWorkWithAssume() throws Exception {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);

      executeAllLifeCycles(adaptor);
      final List<Failure> assumptionFailure = new ArrayList<Failure>();
      Result result = run(adaptor, new RunListener() {
          @Override
        public void testAssumptionFailure(Failure failure) {
              assumptionFailure.add(failure);
        }
      }, ArquillianClass1WithAssume.class);

      Assert.assertEquals(1, assumptionFailure.size());
      Assert.assertTrue(result.wasSuccessful());
      Assert.assertEquals(0, result.getFailureCount());
      Assert.assertEquals(0, result.getIgnoreCount());
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.BEFORE, Cycle.AFTER, Cycle.AFTER_CLASS);

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }

   @Test
   public void shouldThrowMultipleExceptionsWhenBeforeAndAfterThrowException() throws Exception {
      TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class);

      executeAllLifeCycles(adaptor);

      Result result = run(adaptor, ArquillianClass1ExceptionInBeforeAndAfter.class);

      Assert.assertFalse(result.wasSuccessful());
      Assert.assertEquals(2,  result.getFailureCount());
      Assert.assertTrue(result.getFailures().get(0).getMessage().equals("BeforeException"));
      Assert.assertTrue(result.getFailures().get(1).getMessage().equals("AfterException"));
      assertCycle(1, Cycle.BEFORE_CLASS, Cycle.BEFORE, Cycle.AFTER, Cycle.AFTER_CLASS);
      assertCycle(0, Cycle.TEST);

      verify(adaptor, times(1)).beforeSuite();
      verify(adaptor, times(1)).afterSuite();
   }
}
TOP

Related Classes of org.jboss.arquillian.junit.JUnitIntegrationTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.