[英]Spring boot integration test mock bean method with multiple arguments returns null
I have simple spring boot application with Controller
, Service
, Business
and Util
classes, so I'm trying to mock the method in MockUtil
bean which takes four parameters but it returns null
我有一个带有
Controller
、 Service
、 Business
和Util
类的简单 spring boot 应用程序,所以我试图模拟MockUtil
bean 中的方法,它采用四个参数,但它返回null
MockMainController模拟主控制器
@RestController
public class MockMainController {
@Autowired
private MockBusiness mockBusiness;
@GetMapping("request")
public MockOutput mockRequest() {
return mockBusiness.businessLogic(new MockInput());
}
}
MockBusiness模拟业务
@Service
public class MockBusiness {
@Autowired
private MockService mockService;
public MockOutput businessLogic(MockInput input) {
return mockService.serviceLogic(input);
}
}
MockService模拟服务
@Service
public class MockService {
@Autowired
private MockUtil mockUtil;
public MockOutput serviceLogic(MockInput input) {
ResponseEntity<MockOutput> res = mockUtil.exchange(UriComponentsBuilder.fromUriString(" "), HttpMethod.GET,
HttpEntity.EMPTY, new ParameterizedTypeReference<MockOutput>() {
});
return res.getBody();
}
}
MockUtil模拟工具
@Component
public class MockUtil {
@Autowired
private RestTemplate restTemplate;
public <T> ResponseEntity<T> exchange(UriComponentsBuilder uri, HttpMethod method, HttpEntity<?> entity,
ParameterizedTypeReference<T> typeReference) {
try {
ResponseEntity<T> response = restTemplate.exchange(uri.toUriString(), method, entity, typeReference);
return response;
} catch (HttpStatusCodeException ex) {
System.out.println(ex);
return new ResponseEntity<T>(ex.getStatusCode());
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
}
Below is my simple test class, when ever mockUtil.exchange
method is called i want to return object based on ParameterizedTypeReference<T>
下面是我的简单测试类,当调用
mockUtil.exchange
方法时,我想根据ParameterizedTypeReference<T>
返回对象
MockControllerTest模拟控制器测试
@SpringBootTest
@ActiveProfiles("test")
@Profile("test")
@RunWith(SpringRunner.class)
public class MockControllerTest {
@Autowired
private MockMainController mockMainController;
@MockBean
private MockUtil mockUtil;
@Test
public void controllerTest() {
given(this.mockUtil.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.any(new ParameterizedTypeReference<MockOutput>() {
}.getClass()))).willReturn(ResponseEntity.ok().body(new MockOutput("hello", "success")));
MockOutput output = mockMainController.mockRequest();
System.out.println(output);
}
}
By debugging I can see that mockUtil.exchange
is returning null
通过调试我可以看到
mockUtil.exchange
正在返回null
It seems that the way you match ParameterizedTypeReference
is not working.您匹配
ParameterizedTypeReference
的方式似乎不起作用。 It does not match as you expect.它与您预期的不匹配。
Try the following:请尝试以下操作:
given(mockUtil
.exchange(
ArgumentMatchers.any(),
ArgumentMatchers.any(),
ArgumentMatchers.any(),
// eq matches to any param of the same generic type
ArgumentMatchers.eq(new ParameterizedTypeReference<MockOutput>(){})))
.willReturn(ResponseEntity.ok().body(new MockOutput("hello", "success")));
It seems after few tests that if you do not use eq
Mockito expects the passed ParameterizedTypeReference
to be the same instance as in given(..)
and with eq
it just checks that it represents the same generic type.经过几次测试,如果您不使用
eq
Mockito 似乎希望传递的ParameterizedTypeReference
与given(..)
实例相同,并且使用eq
它只是检查它是否代表相同的泛型类型。
Check this question for more details.检查此问题以获取更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.