在创建异常时测试活动



我有以下活动,如果配置错误,则会引发异常。

public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
throw new IllegalStateException("something went wrong");
}
}

我试图为此编写一个测试ActivityInstrumentationTestCase2

public void testException() throws Exception {
try {
getActivity().onCreate(null);
fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("something went wrong");
}
}

它抛出了正确的Exception但由于ActivityInstrumentationTestCase2的一些内部Sandboxing而无法在我的catch块中运行。

所以我用普通的Java尝试了它

public void testException() throws Exception {
final MockNavigationDrawerActivity activity = Mockito.mock(MockNavigationDrawerActivity.class);
Mockito.doCallRealMethod().when(activity).onCreate(null);
try {
activity.onCreate(null);
fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("something went wrong");
}
}

哪个不起作用

java.lang.AbstractMethodError: abstract method "boolean org.mockito.internal.invocation.AbstractAwareMethod.isAbstract()"
at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:109)
at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41)
at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:34)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:91)
at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
at com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:49)
at MockNavigationDrawerActivity_Proxy.onCreate(MockNavigationDrawerActivity_Proxy.generated)

知道如何测试这个简单的案例吗?

更新 #1

我什么都试过了。我将其降低到绝对最小值,这是行不通的。

public void testUpdate1() throws Exception {
try {
getActivity();
fail();
} catch (Exception e) {
assertThat(e.getMessage()).contains("something went wrong");
}
}

堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MyActivity}: java.lang.IllegalStateException: something went wrong
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: something went wrong
at com.example.MyActivity.onCreate(MyActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:346)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more

更新 #2

我从头开始。生成一个新项目,抛出异常

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
throw new IllegalStateException("something");
}

安试图用Throwable抓住它.

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest() {
super(MainActivity.class);
}
public void testOnCreate() throws Exception {
try {
getActivity();
fail();
} catch (Throwable throwable) {
assertTrue(throwable.getCause().getMessage().contains("something"));
}
}
}

我得到了这个(完整的)堆栈跟踪,它不会导致我的测试。系统似乎调用了onCreate,也许来自不同的过程,而不是我的测试。

Process: com.pascalwelsch.onccreatetest, PID: 3915    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pascalwelsch.onccreatetest/com.pascalwelsch.onccreatetest.MainActivity}: java.lang.IllegalStateException: something
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: something
at com.pascalwelsch.onccreatetest.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

你正在扔IllegalArgumentException并接住IllegalStateException.您可以添加另一个带有捕获Exception的捕获块 - 它将起作用。

你为什么要模拟你试图测试的类?您应该模拟MyActivity依赖项,以测试其方法是否正确使用模拟。

例如,如果要测试依赖于BC的类A,那么要为BC创建一个模拟以及一个具体的A对象。然后,您将这 2 个模拟注入到您的对象中,您可以开始对其调用方法。

这可能也是您获得java.lang.AbstractMethodError的原因(尽管没有足够的代码来确认它)。如果你在模拟上调用一个真正的方法,而这个方法是抽象的(例如,你正在模拟一个接口或抽象类),那么就会抛出这个错误。

下面我发布了一些代码和一个测试,作为如何将模拟插入具体对象的示例。

在代码中:

class A {
B b;
C c;
void doSomething() {
b.aMethod();
c.anotherMethod();
throw new IllegalArgumentException("something went wrong");
}
}
interface B {
void aMethod();
}
abstract class C {
void anotherMethod();
}

通过测试:

@RunWith(MockitoJUnitRunner.class)
class ATest {
@Mock B b;
@Mock C c;
// The inject mocks will insert both b and c into a by using reflection
@InjectMocks A a;
@Test(expected=IllegalArgumentException.class)
public void testSomething() {
a.doSomething();
}
}

getActivity确实在调用onCreate方法,因此它因编程异常而失败,并且会抛出另一个(RuntimeException),并以您生成的异常作为根本原因。

相关内容

  • 没有找到相关文章