我被分配爲將單元測試代碼覆蓋率添加到未使用IoC和0單元測試的15年舊傳統項目。我不允許重構代碼,因爲它適用於生產精細完美,管理層不希望其他球隊參與的重構如QA測試等doNothing方法不適用於無效靜態方法
服務類有一個performService
方法如下代碼
public void performService(requestMessage, responseMessage) {
UserAccount userAccount = requestMessage.getUserAccount();
GroupAccount groupAccount = requestMessage.getGroupAccount();
Type type = requestMessage.getType();
StaticServiceCall.enroll(userAccount, groupAccount, type);
response.setStatus(Status.SUCCESS);
}
This StaticServiceCall.enroll
method is calling remote services。我的單元測試是
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticServiceCall.class)
public class EnrollmentServiceTest {
@Test
public void testPerformService() {
mockStatic(StaticServiceCall.class);
doNothing().when(StaticServiceCall.enroll(any(UserAccount.class), any(GroupAccount.class), any(Type.class)));
service.performService(requestMessage, responseMessage);
assertEquals("Enrollment should be success, but not", Status.SUCCESS, response.getStatus);
}
的Eclipse與The method when(T) in the type Stubber is not applicable for the arguments (void)
的Eclipse停止抱怨抱怨,如果測試代碼改變
mockStatic(StaticServiceCall.class);
doNothing().when(StaticServiceCall.class);
StaticServiceCall.enroll(any(UserAccount.class), any(GroupAccount.class), any(Type.class));
service.performService(requestMessage, responseMessage);
assertEquals("Enrollment should be success, but not", Status.SUCCESS, response.getStatus);
測試用例UnfinishedStubbingException
失敗。我正在使用powermock 1.6.6