Java 测试Spring Boot Rest API Post方法
我对Junit完全陌生,我必须为我的Rest控制器编写Junit测试用例,但我不知道从哪里开始。任何帮助都将不胜感激 这是我的Rest控制器类Java 测试Spring Boot Rest API Post方法,java,rest,spring-boot,junit,Java,Rest,Spring Boot,Junit,我对Junit完全陌生,我必须为我的Rest控制器编写Junit测试用例,但我不知道从哪里开始。任何帮助都将不胜感激 这是我的Rest控制器类 @RestController public class RecognitionController { private FrameDecoder frameDecoder; private TagEncoder tagEncoder; private RecognitionService recognitionService;
@RestController
public class RecognitionController {
private FrameDecoder frameDecoder;
private TagEncoder tagEncoder;
private RecognitionService recognitionService;
@Autowired
public RecognitionController(FrameDecoder frameDecoder, TagEncoder tagEncoder,
RecognitionService recognitionService) {
this.frameDecoder = frameDecoder;
this.tagEncoder = tagEncoder;
this.recognitionService = recognitionService;
}
/**
*
* @param take the input as Json Frame and map the output at api/detection Url.
* @return List of Json tag in the Http response.
*/
@RequestMapping(value = "/api/detection", method = RequestMethod.POST)
public List<JsonTag> analyseframe(@RequestBody JsonFrame frame) {
SimpleFrame simpleFrame = frameDecoder.decodeFrame(frame);
List<OrientedTag> orientedTags = recognitionService.analyseFrame(simpleFrame);
return tagEncoder.encodeTag(orientedTags);
}
}
@RestController
公共类识别控制器{
专用帧解码器;
专用标记编码器;
私人认可服务认可服务;
@自动连线
公共识别控制器(帧解码器、帧解码器、标记编码器、标记编码器、,
认可服务(认可服务){
this.frameDecoder=frameDecoder;
this.tagEncoder=tagEncoder;
this.recognitionService=recognitionService;
}
/**
*
*@param将输入作为Json帧,并将输出映射到api/检测Url。
*@返回Http响应中Json标记的列表。
*/
@RequestMapping(value=“/api/detection”,method=RequestMethod.POST)
公共列表analyseframe(@RequestBody JsonFrame){
SimpleName SimpleName=frameDecoder.decodeFrame(帧);
List-orientedTags=recognitionService.AnalyseSFrame(SimpleName);
返回tagEncoder.encodeTag(定向标记);
}
}
要测试Rest控制器,您需要:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/Entity")
public class EntityRestController {
private EntityService service;
@RequestMapping(value = "/entity/all", method = RequestMethod.GET)
public List<Entity> findAll() {
List<Entity> models = service.findAll();
return createEntities(models);
}
private List<EntityDTO> createDTOs(List<Entity> models) {
List<EntityDTO> dtos = new ArrayList<>();
for (Entitymodel: models) {
dtos.add(createDTO(model));
}
return dtos;
}
private EntityDTO createDTO(Entity model) {
EntityDTO dto = new EntityDTO();
dto.setId(model.getId());
dto.setDescription(model.getDescription());
dto.setTitle(model.getTitle());
return dto;
}
}
有关更多详细信息,请按照
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
我不明白“thenReturn”方法是从哪里来的
静态方法Mockito.when()
具有以下签名:
public static <T> OngoingStubbing<T> when(T methodCall)
publicstaticongoingstublingwhen(T methodCall)
当您模拟某个服务并将其作为参数放入
中时,它返回的对象是ongoingstubing
。所有实现了ongoingstubing
的类都有thenReturn(T值)
方法,它被调用了。谢谢你的回答@J-Alex,但我不明白“thenReturn”方法是从哪里来的,你能解释一下它对我真的有帮助吗。
public static <T> OngoingStubbing<T> when(T methodCall)