我想使用Mockito Android处理我的TestClass中的异常

时间:2017-01-10 12:48:05

标签: android testing mockito

当我的testCase在进行电子邮件验证测试时失败我总是在我的日志中找到它

junit.framework.ComparisonFailure: expected:<user@e[]mail.com > but was:<user@e[ee]mail.com >
at junit.framework.Assert.assertEquals(Assert.java:85)
at junit.framework.Assert.assertEquals(Assert.java:91)
at com.zoser.android.activities.ParentActivityTest.testMailValidFail(ParentActivityTest.java:58)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1996)

///这是我的TestClass

@RunWith(MockitoJUnitRunner.class)
public class ParentActivityTest extends InstrumentationTestCase {
    String email = "user@email.com ";
    User user ;
    User spyUser ;
    @Test`enter code here`
     public void testMailValidSucess () throws Exception{
          ParentActivity parentActivity = Mockito.mock(ParentActivity.class);
          user = new User();
          user.setEmail(email);
          spyUser = Mockito.spy(user) ;
          spyUser.setEmail("user@email.com ");

          when(parentActivity.isNameValid(email)).thenReturn(true);
          Assert.assertEquals(user.getEmail() ,spyUser.getEmail());
     }


    @Test
    public void testMailValidFail() throws Exception{

        ParentActivity parentActivity = Mockito.mock(ParentActivity.class);
        user = new User();
        user.setEmail(email);
        spyUser = Mockito.spy(user) ;
        spyUser.setEmail("user@eeemail.com ");

        when(parentActivity.isNameValid(email)).thenReturn(false);
        Assert.assertEquals(user.getEmail() ,spyUser.getEmail());
    }

}

/////////////////////////////////////////////// ///////////////////// 这是我父项活动中的功能

protected boolean isEmailValid(String email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

0 个答案:

没有答案