模拟来自 parent class 的方法到 return 模拟数据

Mock a method coming from parent class to return a mocked data

我有一个来自另一个 class 的方法,它是超级 class 对于我正在测试的方法。我想嘲笑它,这样当我到达那个方法时, 我只想 return 模拟数据。

但目前,它一直进入方法而不是跳过它并只分配模拟数据。

请问我的 mocking 为什么不起作用?

请注意 parent class 实际上来自另一个我们不维护的依赖项,它的可访问性受到保护。

因此无法重构被模拟的方法而不是重构代码。就是想嘲讽一下。

希望通过 mockito 或 powermock 来实现这一点,如果需要的话,也可以结合使用。谢谢。 ## 标题##

这是正在测试的 class 和方法。

@Service
public class OfferService extends ParentClass { 

  // many other methods 

  public Object get() {
    // getHttpEntity() comes from ParentClass
    HttpEntity<Object> httpEntity = getHttpEntity("www.example.com"); // looking to mock this getHttpEntity method. I do not want to enter it. 

    // will not reach here. Failed at above line cos didn't mock getHttpEntity. 
    ResponseEntity<OfferResponses> responseEntity = restTemplate.exchange(endPoint, HttpMethod.GET, httpEntity, OfferResponses.class);
    return responseEntity.getBody();
  }

}

我的测试目前失败,因为它不断进入 getHttpEntity 方法。

package com.pack;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.whenNew;

@RunWith(PowerMockRunner.class)
public class OfferServiceTest {

  @InjectMocks
  OfferService offerService;

  @Mock
  RestTemplate restTemplate;

  @Mock
  HttpEntity<Object> entity;

  @Before
  public void setup()  {
    offerService = new OfferService();
    MockitoAnnotations.initMocks(this);
  }

  // failing test 
  @Test
  public void getTest() throws Exception {
    OfferResponses offerResponses = new OfferResponses();
    ResponseEntity<OfferResponses> responseEntity = new ResponseEntity<>(offerResponses, HttpStatus.OK);

    PowerMockito.when(
            restTemplate.exchange(anyString(), Matchers.eq(HttpMethod.GET)
                    , Matchers.anyObject(),
                    Matchers.<Class<OfferResponses>>anyObject()))
            .thenReturn(responseEntity);

    OfferService spy = spy(offerService);
    doReturn(entity).when(spy, "getHttpEntity", Matchers.anyString());

    Object result = offerService.get();

    // assertions
  }
}

所以,既然你想监视你的 OfferService,你必须在这个间谍上调用你的 get 方法。

使用 PowerMockito:

@RunWith(PowerMockRunner.class)
public class OfferServiceTest {

    @Spy
    @InjectMocks
    private OfferService offerService = new OfferService();

    @Mock
    private RestTemplate restTemplate;

    @Mock
    private HttpEntity<Object> entity;


    @Test
    public void test() {
        PowerMockito.when(offerService, "getHttpEntity", "www.example.com")
            .thenReturn(entity);

        PowerMockito.when(restTemplate.exchange(anyString(), any(), any(), any()))
                .thenReturn(something);

        assertEquals(someObject, offerService.get());
    }
}
 

或者不用 PowerMockito 使用 ReflectionTestUtils:

@ExtendWith(MockitoExtension.class)
public class OfferServiceTest {

    @Spy
    @InjectMocks
    private OfferService offerService;

    @Mock
    private RestTemplate restTemplate;

    @Mock
    private HttpEntity<Object> entity;


    @Test
    public void test() {
        when(ReflectionTestUtils.invokeMethod(offerService, "getHttpEntity", "www.example.com"))
                .thenReturn(entity);

        when(restTemplate.exchange(anyString(), any(), any(), any()))
                .thenReturn(something);

        assertEquals(someObject, offerService.get());
    }
}