java - 那时,模拟类方法没有使用 Mockito 返回预期值
问题描述
控制器
这是我正在为其编写单元测试的 Controller 类。
@RestController
@RequestMapping("document")
public class DocumentController {
private final Logger logger = LoggerFactory.getLogger(DocumentController.class);
private final PtfCommonService ptfCommonService;
private final DocumentService documentService;
@Autowired
public DocumentController(PtfCommonService ptfCommonService, DocumentService documentService){
this.ptfCommonService = ptfCommonService;
this.documentService = documentService;
}
@RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json")
public String create(@RequestBody String documentInfo){
System.out.println(this.documentService.getClass());
return new Gson().toJson(this.documentService.createDocument(documentInfo));
}
服务
这是我的服务类,它正在实现 DocumentService 接口。
@Lazy
@Service
public class DocumentServiceImpl implements DocumentService{
@Override
public JsonResponse createDocument(String documentInfo){
return saveDocument(documentInfo, false);
}
测试班
包含 Document 控制器单元测试的测试类。
@RunWith(MockitoJUnitRunner.class)
//@ContextConfiguration(value = "classpath:applicationContext.xml")
public class DocumentControllerTest extends TestCase {
@Mock
DocumentService documentService;
@InjectMocks
DocumentController documentController;
@Before
@Override
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
/**
* Test method to verify the functionality of createDocumnet controller method.
*/
@Test
public void createDocumentTest () {
String documentInfo = "testInfo";
JsonResponse jsonResp = new JsonResponse();
jsonResp.setMessage("OK");
jsonResp.setStatus("OK");
jsonResp.setSuccess(true);
// Mockito.when(documentService.createDocument(Mockito.anyString())).thenReturn(jsonResp);
Mockito.when(documentService.createDocument(documentInfo)).thenReturn(jsonResp);
String jsonResponse = documentController.create(documentInfo);
System.out.println(jsonResponse);
assertEquals(jsonResponse, jsonResponse);
}
例外
运行测试时遇到的异常
unnecessary Mockito stubbings(com.persivia.ptf.patientservice.controller.DocumentControllerTest) Time elapsed: 0.511 sec <<< ERROR!
org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected in test class: DocumentControllerTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
Pom.xml 具有所需依赖项的 Pom.xml 文件
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
解决方案
如果您通过包含两者来复制 Mockito 设置,MockitoAnnotations.initMocks(this)
并且@RunWith(MockitoJUnitRunner.class)
Mockito 会感到困惑,并希望模拟的方法被调用两次。
推荐阅读
- mysql - 是否可以在 mysql 中不使用 AND 或 OR 来检查多个属性?如果不是,否认这种行为的逻辑是什么?
- c# - 带有 SQL Server 的 C# - Datetime2(3) 字段的时间部分始终设置为 00:00:00:000
- javascript - 单个数组时间戳作为多个系列的 xAxis
- javascript - 如何使用回退方法在 Xamarin Forms iOS 中实现深度链接
- java - 获取当前正在播放的 Windows 媒体控制栏的歌曲?(爪哇)
- xml - 我们如何使用作为 cron 运行的 Jenkins 作业作为监控仪表板
- c# - XML序列化同一对象的不同elementName
- node.js - Node.js 客户端库对 Gmail API 的批量请求
- docker - 使用旧数据恢复结构网络时出现权限被拒绝错误
- flutter - Draggscroll图像颤动,可能吗?