gpt4 book ai didi

java - 模拟来自父类的方法以返回模拟数据

转载 作者:行者123 更新时间:2023-12-04 17:14:02 25 4
gpt4 key购买 nike

我有一个来自另一个类的方法,它是父类(super class)
对于我正在测试的方法。我想 mock 它,当我达到那个方法时,
我只想返回一个模拟数据。
但是目前,它会继续进入该方法,而不是跳过它并仅分配模拟数据。
我能否就为什么我的 mock 不起作用获得一些帮助?
请注意,父类实际上来自另一个我们不维护的依赖项,并且它的可访问性受到保护。
因此不能重构被模拟的方法而不是重构代码。只是想 mock 它。
希望通过 mockito 或 powermock 或组合来实现这一点。谢谢。 ##标题##
这是正在测试的类和方法。

@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这个 spy 的方法。
使用 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());
}
}

关于java - 模拟来自父类的方法以返回模拟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69019265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com