2012-11-04 90 views
6

我試圖運行在延伸SherlockActivity的活性測試。我讀了所有我能找到與ActionBarSherlock測試活動的解決方案,並還嘗試以下https://github.com/passy/absshadow-sampleRobolectric:如何測試SherlockActivity

這是目前我在做什麼:

定製測試運行:

public class CustomTestRunner extends RobolectricTestRunner { 
    private static final int SDK_INT = Build.VERSION.SDK_INT; 

    public CustomTestRunner(Class<?> testClass) throws InitializationError { 
     super(testClass); 
     addClassOrPackageToInstrument("com.actionbarsherlock.app.SherlockActivity"); 
    } 

    @Override 
    protected void bindShadowClasses() { 
     super.bindShadowClasses(); 
     Robolectric.bindShadowClass(ShadowSherlockActivity.class); 
    } 

    @Override 
    public void beforeTest(final Method method) { 
     final int targetSdkVersion = robolectricConfig.getSdkVersion(); 
     setStaticValue(Build.VERSION.class, "SDK_INT", targetSdkVersion); 
    } 

    @Override 
    public void afterTest(final Method method) { 
     resetStaticState(); 
    } 

    @Override 
    public void resetStaticState() { 
     setStaticValue(Build.VERSION.class, "SDK_INT", SDK_INT); 
    } 
} 

shadow Sherlock活動:

@Implements(SherlockActivity.class) 
public class ShadowSherlockActivity extends ShadowActivity { 

    @Implementation 
    public void setContentView(int layoutResID) { 
     super.setContentView(layoutResID); // TODO Auto-generated method stub 
    } 

    @Implementation 
    public ActionBar getSupportActionBar() { 
     return new ActionBar() { 
      // removed for readability 
     }; 
    } 
} 

由於某種原因,它的贈品與此失敗:

WARNING: you probably should have called setContentView() first 
java.lang.Exception: Stack trace 
    at java.lang.Thread.dumpStack(Thread.java:1342) 
    at  com.xtremelabs.robolectric.shadows.ShadowActivity.findViewById(ShadowActivity.java:183) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:99) 
    at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:144) 
    at android.app.Activity.findViewById(Activity.java) 
    at com.dgti.ds.activities.ChooseLocationActivity.findViews(ChooseLocationActivity.java:44) 
    at com.dgti.ds.activities.ChooseLocationActivity.onCreate(ChooseLocationActivity.java:34) 
    at com.dgti.ds.activities.ChooseLocationActivityTests.shouldGetGoogleAPIKeyIfNull(ChooseLocationActivityTests.java:81) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
    at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288) 
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 

我的活動不叫setContentView第一,任何findViewById之前。

奇怪的是,無論我在setContentView的影子實現中做什麼,似乎都沒有發生(例如,拋出NullPojnterException)。 我的影子確實得到了註冊,就好像我試圖覆蓋onCreate的執行並拋出異常,它確實有效。

我缺少什麼嗎?

回答

2

顯然改變了Java SDK中的IntelliJ的項目設置到Oracle的OpenJDK的,而不是固定的這個問題對我來說。

+0

我有同樣的問題,儘管我使用的是Oracle的Java SDK,總是有警告:你也許應該叫的setContentView()第一 你有一個小竅門/修復? – vsm