如何修复单元测试缺少方法调用异常?
How to fix unit test Missing Method Invocation Exception?
When I run the test it shows the given error which I gave below.
It shows Missing Method Invocation Exception Error. But I use that method in that test.
I use Mockito to inject mockito.
If anyone can solve this please help me.
Entity
public class VesselInfo {
private Long vesselId;
private String ownerName;
}
Service
@Service
public class VesselService {
public List<VesselInfo> prepareMockVesselList(){
List<VesselInfo> vesselMockDataList = new ArrayList<>();
VesselInfo vesselInfo = new VesselInfo();
vesselInfo.setVesselId(1L);
vesselInfo.setOwnerName("Abdul Malek");
}
public List<VesselInfo> findVesselByOwnerNid(String ownerNid){
List<VesselInfo> vesselMockDataList = new ArrayList<>();
vesselMockDataList = prepareMockVesselList();
List<VesselInfo> result = new ArrayList<>();
for(VesselInfo vesselInfo : vesselMockDataList){
if(vesselInfo.getOwnerNID().equals(ownerNid)){
//return vesselInfo;
result.add(vesselInfo);
}
}
return result;
}
}
Test
@SpringBootTest(classes = SurveyServiceTest.class)
public class VesselServiceTest {
@InjectMocks
VesselService vesselService;
@Test
public void findVesselByOwnerNidTest(){
// with valid Nid
List<VesselInfo> vesselInfoList = new ArrayList<>();
VesselInfo vesselInfo = new VesselInfo();
vesselInfo.setOwnerNID("12341654565");
VesselInfo vesselInfo1 = new VesselInfo();
vesselInfo1.setOwnerNID("455454455");
vesselInfoList.add(vesselInfo);
vesselInfoList.add(vesselInfo1);
when(vesselService.findVesselByOwnerNid("12341654565")).thenReturn(vesselInfoList);
}
}
@SpringBootTest
class ConservancyChargeServiceTests {
@InjectMocks
private TaxService taxService;
@Mock
private ICriteriaQueryAPI iCriteriaQueryAPI;
@Mock
private JwtTokenProvider jwtTokenProvider;
@Mock
private RestTemplate restTemplate;
@Mock
private TaxService _taxServiceTest;
public VesselInfo prepareVesselInfo(){
VesselInfo vesselInfo = new VesselInfo();
vesselInfo.setVesselId(1L);
vesselInfo.setRegistrationNo("M16530");
vesselInfo.setVesselNameEnglish("Surovi-8");
return vesselInfo;
}
public VesselTaxStatusResponse vesselTaxStatusResponse(VesselInfo vesselInfo) {
VesselTaxStatusResponse vesselTaxStatusResponse = new VesselTaxStatusResponse();
vesselTaxStatusResponse.setVesselInfo(vesselInfo);
vesselTaxStatusResponse.setTaxPaidTill(LocalDate.now());
vesselTaxStatusResponse.setValidityDate(LocalDate.now() );
vesselTaxStatusResponse.setLastTaxPaidDate(new Timestamp(System.currentTimeMillis()));
vesselTaxStatusResponse.setPendingTaxAmount(150.00);
return vesselTaxStatusResponse;
}
public List<VesselTaxStatusResponse> VesselTaxStatusResponseList(){
List<VesselTaxStatusResponse> vesselTaxStatusResponseList = new ArrayList<>();
VesselTaxStatusResponse vesselTaxStatusResponse = new VesselTaxStatusResponse();
VesselInfo vesselInfo = new VesselInfo();
Timestamp surveyDate = new Timestamp(System.currentTimeMillis());
//dummy expiration date setup with current date + 10 days
Timestamp surveyExpireDate = new Timestamp(System.currentTimeMillis() + 10 * 86400000 );
// dummy certificate issue date for master
Timestamp masterCertificateIssueDate = new Timestamp(System.currentTimeMillis() - 10 * 86400000);
vesselInfo.setVesselId(1L);
vesselInfo.setOwnerName("Abdul Malek");
vesselInfo.setOwnerEmail("malek@gmail.com");
vesselInfo.setOwnerNID("16352641582021");
vesselInfo.setOwnerMobileNo("01677503555");
vesselInfo.setOwnerPresentAddress("201, North Shajahanpur, Dhaka-1217");
vesselInfo.setContactPersonName("Md. Kamrul Hasan");
vesselInfo.setContactPersonMobileNo("01932145005");
vesselInfo.setRegistrationNo("M164505");
vesselInfo.setVesselType("Small");
vesselInfo.setVesselNameEnglish("Surovi 8");
vesselInfo.setVesselLength("317fit");
vesselInfo.setVesselWidth("52 & 54 fit");
vesselInfo.setVesselDepth("11.15 fit");
vesselInfo.setSurveyDate(surveyDate);
vesselInfo.setSurveyExpireDate(surveyExpireDate);
vesselInfo.setSurveyPlace("Narayanganj");
vesselInfo.setMasterName("Tawhid Noor");
vesselInfo.setMasterAddress("101, Adabor, Dhaka-1005");
vesselInfo.setMasterCertificateIssueDate(masterCertificateIssueDate);
vesselInfo.setMasterGrade("A");
vesselInfo.setMasterCertificateNo("153620");
vesselInfo.setMasterNid("199265320158");
vesselInfo.setDriverName("Abdul Mubin");
vesselInfo.setDriverAddress("456/1, Mirpur DOHS, Dhaka-1213");
vesselInfo.setDriverCertificateIssueDate(masterCertificateIssueDate);
vesselInfo.setDriverCertificateNo("105400");
vesselInfo.setDriverGrade("B");
vesselInfo.setDayPassengerCapacityMain(300L);
vesselInfo.setDayPassengerCapacityMiddle(100L);
vesselInfo.setDayPassengerCapacityUpper(150L);
vesselInfo.setDayPassengerCapacityTotal(550L);
vesselInfo.setNightPassengerCapacityMain(250L);
vesselInfo.setNightPassengerCapacityMiddle(75L);
vesselInfo.setNightPassengerCapacityUpper(125L);
vesselInfo.setNightPassengerCapacityTotal(450L);
vesselInfo.setDaynoOfCabinSecondFloorSingle(10L);
vesselInfo.setDaynoOfCabinSecondFloorDouble(8L);
vesselInfo.setDaynoOfCabinThirdFloorSingle(12L);
vesselInfo.setDaynoOfCabinThirdFloorDouble(10L);
vesselInfo.setNoOfCrew(5L);
vesselInfo.setNoOfSailor(10L);
vesselInfo.setEngineName("Daihatsu");
vesselInfo.setEnginePower("2000hp 6 cylinder");
vesselInfo.setNoOfEngine(4L);
vesselTaxStatusResponse.setVesselInfo(vesselInfo);
vesselTaxStatusResponse.setTaxPaidTill(LocalDate.now());
vesselTaxStatusResponse.setValidityDate(LocalDate.now());
vesselTaxStatusResponse.setLastTaxPaidDate(new Timestamp(System.currentTimeMillis()));
vesselTaxStatusResponse.setPendingTaxAmount(150.00);
vesselTaxStatusResponseList.add(vesselTaxStatusResponse);
return vesselTaxStatusResponseList;
}
/**
* @author Nobin
*/
@Test
void findPendingTaxByVesselOwnerId() {
List<VesselInfo> responseData = new ArrayList<>();
VesselInfo vesselInfoResponseData = new VesselInfo();
vesselInfoResponseData.setVesselId(1L);
vesselInfoResponseData.setOwnerNID("16352641582021");
responseData.add(vesselInfoResponseData);
List<VesselInfo> vesselInfos = new ArrayList<>();
VesselTaxStatusResponse vesselTaxStatusResponse = vesselTaxStatusResponse(prepareVesselInfo());
when(this.restTemplate.exchange(anyString(), (org.springframework.http.HttpMethod) any(),
(org.springframework.http.HttpEntity<?>) any(), (Class<Object>) any(), (Object[]) any()))
.thenReturn(new ResponseEntity<Object>(HttpStatus.CONTINUE));
when(this.jwtTokenProvider.generateToken((org.springframework.security.core.Authentication) any()))
.thenReturn("foo");
when(iTaxService.getVesselInfoByOwnerNid("16352641582021")).thenReturn(vesselInfos);
when(iTaxService.findTaxStatusByVesselId(any(Long.class))).thenReturn(vesselTaxStatusResponse);
List<VesselTaxStatusResponse> vesselTaxStatusResponseList = taxService.findPendingTaxByVesselOwnerId("16352641582021");
assertNotNull(vesselTaxStatusResponseList);
}
}
When I run the test it shows the given error which I gave below.
It shows Missing Method Invocation Exception Error. But I use that method in that test.
I use Mockito to inject mockito.
If anyone can solve this please help me.
Entity
public class VesselInfo {
private Long vesselId;
private String ownerName;
}
Service
@Service
public class VesselService {
public List<VesselInfo> prepareMockVesselList(){
List<VesselInfo> vesselMockDataList = new ArrayList<>();
VesselInfo vesselInfo = new VesselInfo();
vesselInfo.setVesselId(1L);
vesselInfo.setOwnerName("Abdul Malek");
}
public List<VesselInfo> findVesselByOwnerNid(String ownerNid){
List<VesselInfo> vesselMockDataList = new ArrayList<>();
vesselMockDataList = prepareMockVesselList();
List<VesselInfo> result = new ArrayList<>();
for(VesselInfo vesselInfo : vesselMockDataList){
if(vesselInfo.getOwnerNID().equals(ownerNid)){
//return vesselInfo;
result.add(vesselInfo);
}
}
return result;
}
}
Test
@SpringBootTest(classes = SurveyServiceTest.class)
public class VesselServiceTest {
@InjectMocks
VesselService vesselService;
@Test
public void findVesselByOwnerNidTest(){
// with valid Nid
List<VesselInfo> vesselInfoList = new ArrayList<>();
VesselInfo vesselInfo = new VesselInfo();
vesselInfo.setOwnerNID("12341654565");
VesselInfo vesselInfo1 = new VesselInfo();
vesselInfo1.setOwnerNID("455454455");
vesselInfoList.add(vesselInfo);
vesselInfoList.add(vesselInfo1);
when(vesselService.findVesselByOwnerNid("12341654565")).thenReturn(vesselInfoList);
}
}
@SpringBootTest
class ConservancyChargeServiceTests {
@InjectMocks
private TaxService taxService;
@Mock
private ICriteriaQueryAPI iCriteriaQueryAPI;
@Mock
private JwtTokenProvider jwtTokenProvider;
@Mock
private RestTemplate restTemplate;
@Mock
private TaxService _taxServiceTest;
public VesselInfo prepareVesselInfo(){
VesselInfo vesselInfo = new VesselInfo();
vesselInfo.setVesselId(1L);
vesselInfo.setRegistrationNo("M16530");
vesselInfo.setVesselNameEnglish("Surovi-8");
return vesselInfo;
}
public VesselTaxStatusResponse vesselTaxStatusResponse(VesselInfo vesselInfo) {
VesselTaxStatusResponse vesselTaxStatusResponse = new VesselTaxStatusResponse();
vesselTaxStatusResponse.setVesselInfo(vesselInfo);
vesselTaxStatusResponse.setTaxPaidTill(LocalDate.now());
vesselTaxStatusResponse.setValidityDate(LocalDate.now() );
vesselTaxStatusResponse.setLastTaxPaidDate(new Timestamp(System.currentTimeMillis()));
vesselTaxStatusResponse.setPendingTaxAmount(150.00);
return vesselTaxStatusResponse;
}
public List<VesselTaxStatusResponse> VesselTaxStatusResponseList(){
List<VesselTaxStatusResponse> vesselTaxStatusResponseList = new ArrayList<>();
VesselTaxStatusResponse vesselTaxStatusResponse = new VesselTaxStatusResponse();
VesselInfo vesselInfo = new VesselInfo();
Timestamp surveyDate = new Timestamp(System.currentTimeMillis());
//dummy expiration date setup with current date + 10 days
Timestamp surveyExpireDate = new Timestamp(System.currentTimeMillis() + 10 * 86400000 );
// dummy certificate issue date for master
Timestamp masterCertificateIssueDate = new Timestamp(System.currentTimeMillis() - 10 * 86400000);
vesselInfo.setVesselId(1L);
vesselInfo.setOwnerName("Abdul Malek");
vesselInfo.setOwnerEmail("malek@gmail.com");
vesselInfo.setOwnerNID("16352641582021");
vesselInfo.setOwnerMobileNo("01677503555");
vesselInfo.setOwnerPresentAddress("201, North Shajahanpur, Dhaka-1217");
vesselInfo.setContactPersonName("Md. Kamrul Hasan");
vesselInfo.setContactPersonMobileNo("01932145005");
vesselInfo.setRegistrationNo("M164505");
vesselInfo.setVesselType("Small");
vesselInfo.setVesselNameEnglish("Surovi 8");
vesselInfo.setVesselLength("317fit");
vesselInfo.setVesselWidth("52 & 54 fit");
vesselInfo.setVesselDepth("11.15 fit");
vesselInfo.setSurveyDate(surveyDate);
vesselInfo.setSurveyExpireDate(surveyExpireDate);
vesselInfo.setSurveyPlace("Narayanganj");
vesselInfo.setMasterName("Tawhid Noor");
vesselInfo.setMasterAddress("101, Adabor, Dhaka-1005");
vesselInfo.setMasterCertificateIssueDate(masterCertificateIssueDate);
vesselInfo.setMasterGrade("A");
vesselInfo.setMasterCertificateNo("153620");
vesselInfo.setMasterNid("199265320158");
vesselInfo.setDriverName("Abdul Mubin");
vesselInfo.setDriverAddress("456/1, Mirpur DOHS, Dhaka-1213");
vesselInfo.setDriverCertificateIssueDate(masterCertificateIssueDate);
vesselInfo.setDriverCertificateNo("105400");
vesselInfo.setDriverGrade("B");
vesselInfo.setDayPassengerCapacityMain(300L);
vesselInfo.setDayPassengerCapacityMiddle(100L);
vesselInfo.setDayPassengerCapacityUpper(150L);
vesselInfo.setDayPassengerCapacityTotal(550L);
vesselInfo.setNightPassengerCapacityMain(250L);
vesselInfo.setNightPassengerCapacityMiddle(75L);
vesselInfo.setNightPassengerCapacityUpper(125L);
vesselInfo.setNightPassengerCapacityTotal(450L);
vesselInfo.setDaynoOfCabinSecondFloorSingle(10L);
vesselInfo.setDaynoOfCabinSecondFloorDouble(8L);
vesselInfo.setDaynoOfCabinThirdFloorSingle(12L);
vesselInfo.setDaynoOfCabinThirdFloorDouble(10L);
vesselInfo.setNoOfCrew(5L);
vesselInfo.setNoOfSailor(10L);
vesselInfo.setEngineName("Daihatsu");
vesselInfo.setEnginePower("2000hp 6 cylinder");
vesselInfo.setNoOfEngine(4L);
vesselTaxStatusResponse.setVesselInfo(vesselInfo);
vesselTaxStatusResponse.setTaxPaidTill(LocalDate.now());
vesselTaxStatusResponse.setValidityDate(LocalDate.now());
vesselTaxStatusResponse.setLastTaxPaidDate(new Timestamp(System.currentTimeMillis()));
vesselTaxStatusResponse.setPendingTaxAmount(150.00);
vesselTaxStatusResponseList.add(vesselTaxStatusResponse);
return vesselTaxStatusResponseList;
}
/**
* @author Nobin
*/
@Test
void findPendingTaxByVesselOwnerId() {
List<VesselInfo> responseData = new ArrayList<>();
VesselInfo vesselInfoResponseData = new VesselInfo();
vesselInfoResponseData.setVesselId(1L);
vesselInfoResponseData.setOwnerNID("16352641582021");
responseData.add(vesselInfoResponseData);
List<VesselInfo> vesselInfos = new ArrayList<>();
VesselTaxStatusResponse vesselTaxStatusResponse = vesselTaxStatusResponse(prepareVesselInfo());
when(this.restTemplate.exchange(anyString(), (org.springframework.http.HttpMethod) any(),
(org.springframework.http.HttpEntity<?>) any(), (Class<Object>) any(), (Object[]) any()))
.thenReturn(new ResponseEntity<Object>(HttpStatus.CONTINUE));
when(this.jwtTokenProvider.generateToken((org.springframework.security.core.Authentication) any()))
.thenReturn("foo");
when(iTaxService.getVesselInfoByOwnerNid("16352641582021")).thenReturn(vesselInfos);
when(iTaxService.findTaxStatusByVesselId(any(Long.class))).thenReturn(vesselTaxStatusResponse);
List<VesselTaxStatusResponse> vesselTaxStatusResponseList = taxService.findPendingTaxByVesselOwnerId("16352641582021");
assertNotNull(vesselTaxStatusResponseList);
}
}