模拟 class 方法在那时没有使用 Mockito 返回预期值
Mocked class method is not returning expected value with Mockito when then
控制器
这是我的控制器 class,我正在为其编写单元测试。
@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));
}
服务
这是我的服务 class,它实现了 DocumentService 接口。
@Lazy
@Service
public class DocumentServiceImpl implements DocumentService{
@Override
public JsonResponse createDocument(String documentInfo){
return saveDocument(documentInfo, false);
}
测试Class
包含文档控制器单元测试的测试class。
@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>
如果您通过同时包含 MockitoAnnotations.initMocks(this)
和 @RunWith(MockitoJUnitRunner.class)
来复制 Mockito 设置,Mockito 会感到困惑,并期望模拟方法被调用两次。
控制器
这是我的控制器 class,我正在为其编写单元测试。
@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));
}
服务
这是我的服务 class,它实现了 DocumentService 接口。
@Lazy
@Service
public class DocumentServiceImpl implements DocumentService{
@Override
public JsonResponse createDocument(String documentInfo){
return saveDocument(documentInfo, false);
}
测试Class
包含文档控制器单元测试的测试class。
@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>
如果您通过同时包含 MockitoAnnotations.initMocks(this)
和 @RunWith(MockitoJUnitRunner.class)
来复制 Mockito 设置,Mockito 会感到困惑,并期望模拟方法被调用两次。