NullPointerException on reflection during Robolectric startup - any hints?

后端 未结 3 776
醉梦人生
醉梦人生 2021-01-05 18:28

I am developing an Android project on Eclipse, and I\'m trying to switch from running tests on the emulator/device (which is very slow) to Robolectric.

I re

相关标签:
3条回答
  • 2021-01-05 18:32

    We have had the same problems in the past. What works for us is extending RoboletricTestRunner and providing it a RoboletricConfig instance that is aware of our Project's location. This way I don't have to tinker with any run configurations with eclipse, it just works.

        package ...;
    
        import java.io.File;
    
        import org.junit.runners.model.InitializationError;
    
        import com.xtremelabs.robolectric.RobolectricConfig;
        import com.xtremelabs.robolectric.RobolectricTestRunner;
    
        public class TestRunner extends RobolectricTestRunner {
    
            public static final string MAIN_PROJECT_PATH = "../path_to_android_project";
    
            public TestRunner(Class<?> testClass) throws InitializationError {
                 super(testClass, new RobolectricConfig(new File(MAIN_PROJECT_PATH)));
            }
        }
    
    0 讨论(0)
  • 2021-01-05 18:34

    Normally you won't need to extend TestCase, but I don't think that should be causing this problem. Could you include the source of the failing test?

    Regarding the simplified project in your update, make sure that Eclipse is configured to set the current working directory to the root of the main project, not the test project, so it will find AndroidManifest.xml and the res directory.

    0 讨论(0)
  • 2021-01-05 18:46

    No need to create a custom RoboelectricTestRunner, just play a bit with your 'Run Configurations' on Eclipse. On the configuration for running your tests, go to the classpath tab and ensure that the libraries required for your test, e.g. robolectric-2.2-jar-with-dependencies.jar and android.jar and the project itself are in the User Entries and ONLY the Java SDK is in the Bootstrap Entries.

    Then add this file:

    [project_path]/test/org.robolectric.Config.properties

     manifest=../AndroidManifest.xml
    
    0 讨论(0)
提交回复
热议问题