Home | History | Annotate | Download | only in bytecode
      1 package com.xtremelabs.robolectric.bytecode;
      2 
      3 import android.app.Application;
      4 import com.xtremelabs.robolectric.Robolectric;
      5 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
      6 import org.junit.AfterClass;
      7 import org.junit.Before;
      8 import org.junit.Test;
      9 import org.junit.runner.RunWith;
     10 import org.junit.runners.model.InitializationError;
     11 
     12 import java.lang.reflect.Method;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 import static org.junit.Assert.assertNotNull;
     16 import static org.junit.Assert.assertTrue;
     17 
     18 @RunWith(CustomRobolectricTestRunnerTest.CustomRobolectricTestRunner.class)
     19 public class CustomRobolectricTestRunnerTest {
     20     Object preparedTest;
     21     static Method testMethod;
     22     static int beforeCallCount = 0;
     23     static int afterTestCallCount = 0;
     24 
     25     @Before
     26     public void setUp() throws Exception {
     27         beforeCallCount++;
     28     }
     29 
     30     @Test
     31     public void shouldInitializeApplication() throws Exception {
     32         assertNotNull(Robolectric.application);
     33         assertEquals(CustomApplication.class, Robolectric.application.getClass());
     34     }
     35 
     36     @Test
     37     public void shouldInvokePrepareTestWithAnInstanceOfTheTest() throws Exception {
     38         assertEquals(this, preparedTest);
     39         assertEquals(RobolectricClassLoader.class.getName(), preparedTest.getClass().getClassLoader().getClass().getName());
     40     }
     41 
     42     @Test
     43     public void shouldInvokeBeforeTestWithTheCorrectMethod() throws Exception {
     44         assertEquals("shouldInvokeBeforeTestWithTheCorrectMethod", testMethod.getName());
     45     }
     46 
     47     @AfterClass
     48     public static void shouldHaveCalledAfterTest() {
     49         assertTrue(beforeCallCount > 0);
     50         assertEquals(beforeCallCount, afterTestCallCount);
     51     }
     52 
     53     public static class CustomRobolectricTestRunner extends WithTestDefaultsRunner {
     54         public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError {
     55             super(testClass);
     56         }
     57 
     58         @Override public void prepareTest(Object test) {
     59             ((CustomRobolectricTestRunnerTest) test).preparedTest = test;
     60         }
     61 
     62         @Override public void beforeTest(Method method) {
     63             testMethod = method;
     64         }
     65 
     66         @Override public void afterTest(Method method) {
     67             afterTestCallCount++;
     68         }
     69 
     70         @Override protected Application createApplication() {
     71             return new CustomApplication();
     72         }
     73     }
     74 
     75     public static class CustomApplication extends Application {
     76     }
     77 }
     78