我已经用Eclipse ADT + maven + Robolectric编写了一个测试,如下所示。
public void testPostToConnectionsCorrect() {
setCorrectIP();
activity.postToConnections();
Mockito.verify(mainWebView).postUrl(Mockito.anyString(), EncodingUtils.getBytes(Mockito.anyString(), "UTF-8"));
}
private void setCorrectIP() {
Intent i = new Intent(Robolectric.getShadowApplication().getApplicationContext(),
ConnectionsContainerActivity.class);
i.putExtra("IPAddress", "135.127.21.183");
activity = Robolectric.buildActivity(ConnectionsContainerActivity.class).withIntent(i).create().visible().get();
mainWebView = Mockito.mock(WebView.class);
activity.setMainWebView(mainWebView);
}
但是当我运行“maven install”时会出现错误,如下所示:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.744 sec <<< FAILURE! - in com.ncr.cxp.client.connections.container.test.ConnectionsContainerActivityTest
testPostToConnectionsCorrect(com.ncr.cxp.client.connections.container.test.ConnectionsContainerActivityTest) Time elapsed: 7.65 sec <<< ERROR!
java.lang.RuntimeException: Stub!
at org.apache.http.util.EncodingUtils.getBytes(EncodingUtils.java:7)
at com.ncr.cxp.client.connections.container.ConnectionsContainerActivity.postToConnections(ConnectionsContainerActivity.java:166)
at com.ncr.cxp.client.connections.container.ConnectionsContainerActivity.onCreate(ConnectionsContainerActivity.java:150)
at android.app.Activity.performCreate(Activity.java:5008)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:116)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:123)
at com.ncr.cxp.client.connections.container.test.ConnectionsContainerActivityTest.setCorrectIP(ConnectionsContainerActivityTest.java:79)
at com.ncr.cxp.client.connections.container.test.ConnectionsContainerActivityTest.testPostToConnectionsCorrect(ConnectionsContainerActivityTest.java:70)
似乎robolectric无法捕获此“存根”异常并在VM中运行测试。我在过去的讨论中尝试了一些解决方案,但没有一个有效。
PS:我还在robolectric的同一个项目中写了其他测试,他们没问题。只有这个测试有问题。
任何人都可以提供帮助吗?
答案 0 :(得分:0)
我在依赖项中添加了“RoboGuice”作为修复它。