我為我的服務進行了單元測驗,使用 JUnit5 測驗 PUT 方法(請注意,given
靜態匯入來自 DBBMockito,verify
靜態匯入來自 Mockito。我也曾經@Builder
制作過物件。)。請看一下當前的測驗:
@ExtendWith(MockitoExtension.class)
class BillServiceTest {
@Mock
private BillRepository billRepository;
@InjectMocks
private BillService billService;
@Test
void whenGivenId_shouldUpdateBill_ifFound() {
Bill bill =
Bill.builder()
.billId(70L)
.hospitalServicesDescription("Cardiology")
.price(100)
.build();
Bill newBill =
Bill.builder()
.price(400)
.build();
given(billRepository.findById(bill.getBillId())).willReturn(Optional.of(bill));
billService.updateBill(bill.getBillId(), newBill);
verify(billRepository).save(newBill);
verify(billRepository).findById(bill.getBillId());
}
}
當我運行它時,我得到一個引數不同的報告:
Argument(s) are different! Wanted:
billRepository.save(
Bill(billId=null, dateOfBill=null, hospitalServicesDescription=null, price=400, patient=null)
);
-> at com.app.hospitalmanagementsystem.service.BillServiceTest.whenGivenId_shouldUpdateBill_ifFound(BillServiceTest.java:96)
Actual invocations have different arguments:
billRepository.findById(
70L
);
-> at com.app.hospitalmanagementsystem.service.BillService.updateBill(BillService.java:44)
billRepository.save(
Bill(billId=70, dateOfBill=null, hospitalServicesDescription=null, price=400, patient=null)
);
-> at com.app.hospitalmanagementsystem.service.BillService.updateBill(BillService.java:49)
Comparison Failure:
<Click to see difference>
當我單擊時<Click to see difference>
,這就是我得到的:
預期的:
billRepository.save(
Bill(billId=null, dateOfBill=null, hospitalServicesDescription=null, price=400, patient=null)
);
實際的:
billRepository.findById(
70L
);
billRepository.save(
Bill(billId=70, dateOfBill=null, hospitalServicesDescription=null, price=400, patient=null)
);
另外,我要測驗的代碼在這里:
@Service
public class BillService {
private final BillRepository billRepository;
@Autowired
public BillService(BillRepository billRepository) {
this.billRepository = billRepository;
}
public Bill updateBill(Long billId, Bill billUpdatedDetails) {
Bill updatedBill = billRepository.findById(billId)
.orElseThrow(()-> new ResourceNotFoundException("Bill with id " billId " doesn't exist."));
updatedBill.setDateOfBill(billUpdatedDetails.getDateOfBill());
updatedBill.setHospitalServicesDescription(billUpdatedDetails.getHospitalServicesDescription());
updatedBill.setPrice(billUpdatedDetails.getPrice());
return billRepository.save(updatedBill);
}
這是物體:
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@Table(
name = "bill"
)
public class Bill {
@Id
@SequenceGenerator(
name = "bill_sequence",
sequenceName = "bill_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "bill_sequence"
)
private Long billId;
@Column(
name = "date_of_bill",
nullable = false
)
private LocalDate dateOfBill;
@Column(
name = "hospital_services_description",
nullable = false
)
private String hospitalServicesDescription;
@Column(
name = "price",
nullable = false
)
private Integer price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(
name = "patient_id",
referencedColumnName = "patientId"
)
private Patient patient;
@JsonIgnore
@OneToMany(mappedBy = "bill")
@ToString.Exclude
private List<PatientMedicalHistory> patientMedicalHistories;
public void connectPatient(Patient patient) {
this.patient = patient;
}
}
有誰知道我該如何解決這個問題?
uj5u.com熱心網友回復:
在您的代碼中,傳遞給的引數billRepository.save()
是updatedBill
. updatedBill
來自billRepository.findById()
:
public Bill updateBill(Long billId, Bill billUpdatedDetails) {
// updatedBill comes from billRepository.findById()
Bill updatedBill = billRepository
.findById(billId)
.orElseThrow(()-> new ResourceNotFoundException("..."));
// ...
// updatedBill is passed to billRepository.save()
return billRepository.save(updatedBill);
}
在您的測驗中,您確保billRepository.findById()
回傳bill
:
given(billRepository.findById(bill.getBillId())).willReturn(Optional.of(bill));
// ...
所以這意味著傳遞給的引數billRepository.save()
也應該是bill
. 但是,在您的測驗中,您通過newBill
而不是bill
,這就是測驗失敗的原因:
verify(billRepository).save(newBill); // Incorrect
verify(billRepository).save(bill); // Correct
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/505302.html
下一篇:路徑變數用作注釋引數