Mocked方法返回实际值而不是模拟的值

时间:2016-09-06 09:32:15

标签: android unit-testing mockito

我被要求在Android Studio上设置持续集成,所以我开始进行单元测试和模拟。

我觉得我错过了Mockito的基础和工作。我在互联网上找到了这样的例子:

    @RunWith(MockitoJUnitRunner.class)
    public class ExampleUnitTest {

      MainActivity classToTest = new MainActivity();

      @Mock
      ClassIDontWantToTest classToMock;

      /**
       * Examples found a lot of times on the Internet
       * My method is not tested at all
       * I think it doesn't make sense to test Mockito but our own methods
       * @throws Exception
       */
      @Test
      public void testMethodIDontWantToTest() throws Exception {
        when(classToMock.methodIDontWantToTest()).thenReturn("Mocked result");
        assertTrue(classToMock.methodIDontWantToTest().equals("Mocked result"));
      }

    }

这是我的代码:

    public class MainActivity extends AppCompatActivity {
      ClassIDontWantToTest classIWantToMock = new ClassIDontWantToTest();

      /**
       * Actual Method I want to test
       * @return Return value
       */
      public String methodIWantToTestButReturnsUnspectedValue() {

          //  (...) Code to test

          return classIWantToMock.methodIDontWantToTest();
      }
    }


    public class ClassIDontWantToTest {

      public String methodIDontWantToTest() {
        return "Actual result";
      }

    }

这就是我理解它必须要做的事情:

    @RunWith(MockitoJUnitRunner.class)
    public class ExampleUnitTest {
      MainActivity classToTest = new MainActivity();

      @Mock
      ClassIDontWantToTest classToMock;

      /**
       * Test I want to do and I think it makes sense, but it doesn't work
       * @throws Exception
       */
      @Test
      public void testMethodIWantToTest() throws Exception {
        when(classToMock.methodIDontWantToTest()).thenReturn("Mocked result");
        assertTrue(classToTest.methodIWantToTestButReturnsUnspectedValue().equals("Mocked result"));
      }
    }

显然有一些我不明白的东西,但我不知道它是什么。 请问有谁请告诉我哪里错了?

非常感谢!

2 个答案:

答案 0 :(得分:0)

您的模拟对象设置不正确,表示您的urllib2.urlopen(url).read()课程没有注入MainActivity classToTest = new MainActivity();

要做到这一点,你需要这样的东西:

ClassIDontWantToTest classToMock;

答案 1 :(得分:0)

尤里卡!   正如大卫华莱士所说,我必须在@InjectMocks的声明中加上classToTest注释。

现在我的测试工作正常,这很有道理。非常感谢你!

@RunWith(MockitoJUnitRunner.class)
public class ExampleUnitTest {

  @Mock
  ClassIDontWantToTest classToMock;

  @InjectMocks
  MainActivity classToTest;

  ////////// Test that makes sense and works fine
  @Test
  public void testMethodIWantToTest() throws Exception {
    when(classToMock.methodIDontWantToTest()).thenReturn("Mocked result");
    assertTrue(classToTest.methodIWantToTest().equals("Mocked result"));
  }

}



////////////////  Class to mock
public class ClassIDontWantToTest {
  public String methodIDontWantToTest() {
    return "Actual result";
  }
}




////////////// Class to be tested
public class MainActivity extends AppCompatActivity {
  ClassIDontWantToTest classIWantToMock = new ClassIDontWantToTest();

  ////////////  Actual Method I want to test
  public String methodIWantToTest() {
      //  (...) Code to test
      return classIWantToMock.methodIDontWantToTest();
  }

}