我为我的服务做了单元测试,使用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;
}
}
有人知道我该怎么解决这个问题吗?
1条答案
按热度按时间euoag5mw1#
在您的程式码中,传递给
billRepository.save()
的参数是updatedBill
。updatedBill
来自billRepository.findById()
:在您的测试中,请确定
billRepository.findById()
会传回bill
:因此,这意味着传递给
billRepository.save()
的参数也应该是bill
。然而,在您的测试中,您传递了newBill
而不是bill
,这就是测试失败的原因: