【问题标题】:Unable to execute test method from mocked class无法从模拟类执行测试方法
【发布时间】:2017-04-11 13:33:37
【问题描述】:

我正在编写单元测试来寻找我所在位置附近的银行。 我嘲笑了这个班级并试图调用这些方法。 但是,控制不会去执行它的方法。 下面是单元测试用例。

@Test
public void testFindBanksByGeo() {

    String spatialLocation = "45.36134,14.84400";
    String Address = "Test Address";
    String spatialLocation2 = "18.04706,38.78501";

    // 'SearchClass' is class where 'target' method resides
    SearchClass searchClass = Mockito.mock(SearchClass.class);
    BankEntity bank = Mockito.mock(BankEntity.class);

    // 'findAddressFromGeoLocation' and 'getGeo_location' to be mocked. They are called within 'target' method
    when(searchClass.findAddressFromGeoLocation(anyString())).thenReturn(Address);
    when(bank.getGeo_location()).thenReturn(spatialLocation2);

    // 'writeResultInJson' is void method. so needed to 'spy' 'SearchClass' 
    SearchClass spy = Mockito.spy(SearchClass.class);
    Mockito.doNothing().when(spy).writeResultInJson(anyObject(), anyString());

    //This is test target method called. **Issue is control is not going into this method**
    SearchedBanksEntity searchBanksEntity = searchClass.findNearbyBanksByGeoLocation(spatialLocation, 500);

    assertNull(searchBankEntity);
}

我试过的也是调用真实方法就可以了,

Mockito.when(searchClass.findNearbyBanksByGeoLocation(anyString(), anyDouble())).thenCallRealMethod();

这调用了真正的方法,但我上面模拟的方法却像真正的方法一样执行。意味着“模拟方法”没有返回我要求他们返回的内容。

那么,我在这里做错了什么? 为什么方法没有执行?

【问题讨论】:

  • searchClass 是一个模拟,你想要达到的目标很奇怪
  • 感谢您的评论。但我是 Mockito 的新手。只是说它奇怪不会帮助我成长。而是提一下出了什么问题,以便用户可以对其进行处理。
  • 你能分享你正在为 junit 编写的代码的相关部分吗?

标签: java unit-testing junit mocking mockito


【解决方案1】:

该方法没有被调用,因为您是在模拟上调用它。您应该在实际对象上调用该方法。

或者你可以在调用方法之前使用类似的东西。

Mockito.when(searchClass.findNearbyBanksByGeoLocation(Mockito.eq(spatialLocation), Mockito.eq(500))).thenCallRealMethod();

但我认为这不是你应该编写测试的方式。首先,您不应该嘲笑 SearchClass。取而代之的是 SearchClass 中的依赖项,它可以为您提供地址和地理位置。你应该嘲笑那个特定的依赖。

【讨论】:

    【解决方案2】:

    好的,假设我们有这个代码:

    class Foo {
        // has a setter
        SomeThing someThing;
    
        int bar(int a) {
           return someThing.compute(a + 3);
        }
    }
    

    我们想测试Foo#bar(),但是对SomeThing有依赖,然后我们可以使用模拟:

    @RunWith(MockitoJunitRunner.class)
    class FooTest {
        @Mock // Same as "someThing = Mockito.mock(SomeThing.class)"
        private SomeThing someThing,
    
        private final Foo foo;
    
        @Before
        public void setup() throws Exception {
            foo = new Foo(); // our instance of Foo we will be testing
            foo.setSomeThing(someThing); // we "inject" our mocked SomeThing
        } 
    
        @Test
        public void testFoo() throws Exception {
            when(someThing.compute(anyInt()).thenReturn(2); // we define some behavior
            assertEquals(2, foo.bar(5)); // test assertion
            verify(someThing).compute(7); // verify behavior.
        } 
    }
    

    使用模拟我们可以避免使用真实的SomeThing

    一些阅读:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多