将Robolectric与ActionBarSherlock一起使用时存在很大问题。
我不是为什么,但似乎Roboletric找不到一个类:
例外:
java.lang.RuntimeException: java.lang.NoSuchMethodException: android.support.v4.app.Watson.$$robo$$Watson_7e47___constructor__()
at java.lang.Class.getDeclaredMethod(Class.java:1937)
at org.robolectric.bytecode.ShadowWrangler$InvocationPlan.callOriginal(ShadowWrangler.java:589)
at org.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:185)
at org.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:118)
at android.support.v4.app.Watson.__constructor__(Watson.java)
at android.content.Context.<init>(Context.java)
at android.content.ContextWrapper.<init>(ContextWrapper.java)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java)
at android.app.Activity.<init>(Activity.java)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java)
at android.support.v4.app.Watson.<init>(Watson.java)
at com.actionbarsherlock.app.SherlockFragmentActivity.<init>(SherlockFragmentActivity.java:21)
at pl.activities.CoreFragmentMenuActivity.<init>(CoreFragmentMenuActivity.java:21)
at pl.activities.CoreNavigationActivity.<init>(CoreNavigationActivity.java:16)
at pl.activities.MainActivity.<init>(MainActivity.java:18)
at MainActivityTest.setUp(MainActivityTest.java:20)
测试类:
@RunWith(CustomTestRunner.class)
public class MainActivityTest
{
private MainActivity activity;
private Button pressMeButton;
private TextView results;
@Before
public void setUp() throws Exception
{
activity = new MainActivity();
activity.onCreate(null);
}
@Test
public void shouldUpdateResultsWhenButtonIsClicked() throws Exception
{
Assert.assertEquals(activity.getTitle(), activity.getResources().getString(R.string.activity_main_title));
}
}
CustomTestRunner:
import java.io.File;
public class CustomTestRunner extends RobolectricTestRunner
{
public CustomTestRunner(Class<?> testClass) throws InitializationError
{
super(RobolectricContext.bootstrap(CustomTestRunner.class, testClass,
new RobolectricContext.Factory()
{
@Override
public RobolectricContext create()
{
return new RobolectricContext()
{
//http://stackoverflow.com/questions/15603583/impossible-runtimeexception-stub-with-robolectric
@Override
public boolean useAsm() // this override does the trick
{
return false;
}
@Override
protected AndroidManifest createAppManifest()
{
return new AndroidManifest(
new File("AndroidManifest.xml"),
new File("res"));
}
};
}
}));
}
我正在使用:
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
<version>2.0-alpha-2</version>
<scope>test</scope>
</dependency>
有没有人有类似的问题?