java - Feign客户端单元测试
问题描述
我想知道在这种情况下编写单元测试的最佳方法是什么:
我的API:
@RestController
public class MyApi{
@Autowired
MyAction myAction;
@PostMapping
public ResponseEntity addAction(@ResponseBody MyDto myDto){
return myAction.addAction(myDto);
}
}
我的行动:
@Service
public class MyAction{
@Autowired
private MyClient myClient;
public ResponseEntity<AuthenticationResponseDto> login(MyDto myDto{
return ResponseEntity.ok(myClient.addClient(myDto));
}
}
例如,是否必须添加构造函数?
谢谢
解决方案
使用构造函数注入被认为是一种很好的做法,但是如果您不想使用它,则需要使用@Mock
and @InjectMocks
。它使用反射,不需要定义构造函数。
@RunWith(MockitoJUnitRunner.class)
public class Test {
@Mock
private Client client;
@InjectMocks
private ServiceImpl plannerService = new ServiceImpl();
@Test
public void test() throws Exception {
....
}
}
推荐阅读
- performance - kafka broker server CPU利用率不均
- node.js - Joi 根据多个键验证替代方案
- python - 如何解决 Azure CosmosDB 上仅出现在大数据集合上的查询问题?
- c - C中的可变大小数组和数组指针
- javascript - 邮递员捕获匹配另一个元素标准的元素
- regex - 正则表达式仅验证捕获组的结果
- javascript - 像这样调用javascript函数是什么意思 func(foo)(bar)
- android - Xamarin.Forms Android 在 VS 2019 上启动时崩溃
- excel - 在匹配的行上只合并一列
- python - Tensorflow,迭代张量