我想了解并查看為我的服務層創建單元測驗的最簡單方法。幾天來,我一直在研究最簡單的方法,但不幸的是,我仍然沒有為我的特定代碼找到解決方案。我自己多次嘗試撰寫測驗,但人們告訴我代碼可以做得更好。先感謝您。
我的物體:
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@Table
public class Appointment {
@Id
@SequenceGenerator(
name = "appointment_sequence",
sequenceName = "appointment_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "appointment_sequence"
)
private Long appointmentId;
@Column(
name = "date_of_appointment",
nullable = false
)
private LocalDate dateOfAppointment;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(
name = "patient_id",
referencedColumnName = "patientId"
)
private Patient patient;
public void connectWithPatient(Patient patient) {
this.patient = patient;
}
}
我的倉庫:
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
}
我的服務:
@Service
public class AppointmentService implements AppointmentServiceInterface {
private final AppointmentRepository appointmentRepository;
private final PatientRepository patientRepository;
@Autowired
public AppointmentService(AppointmentRepository appointmentRepository, PatientRepository patientRepository) {
this.appointmentRepository = appointmentRepository;
this.patientRepository = patientRepository;
}
@Override
public List<Appointment> getAllAppointments() {
return appointmentRepository.findAll();
}
@Override
public ResponseEntity<Appointment> getAppointmentById(Long appointmentId) {
Appointment appointment = appointmentRepository.findById(appointmentId)
.orElseThrow(()-> new ResourceNotFoundException("Appointment with id " appointmentId " doesn't exist."));
return ResponseEntity.ok(appointment);
}
@Override
public Appointment createNewAppointment(Appointment appointment) {
return appointmentRepository.save(appointment);
}
@Override
public ResponseEntity<Appointment> updateAppointment(Long appointmentId, Appointment appointmentUpdatedDetails) {
Appointment updatedAppointment = appointmentRepository.findById(appointmentId)
.orElseThrow(()-> new ResourceNotFoundException("Appointment with id " appointmentId " doesn't exist."));
updatedAppointment.setDateOfAppointment(appointmentUpdatedDetails.getDateOfAppointment());
appointmentRepository.save(updatedAppointment);
return ResponseEntity.ok(updatedAppointment);
}
@Override
public ResponseEntity<Appointment> deleteAppointment(Long appointmentId) {
Appointment appointment = appointmentRepository.findById(appointmentId)
.orElseThrow(()-> new ResourceNotFoundException("Appointment with id " appointmentId " doesn't exist."));
appointmentRepository.delete(appointment);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@Override
public Appointment makeAppointmentWithPatient(Long appointmentId, Long patientId) {
Appointment appointment = appointmentRepository.findById(appointmentId)
.orElseThrow(()-> new ResourceNotFoundException("Appointment with id " appointmentId " doesn't exist."));
Patient patient = patientRepository.findById(patientId)
.orElseThrow(()-> new ResourceNotFoundException("Patient with id " patientId " doesn't exist."));
appointment.connectWithPatient(patient);
return appointmentRepository.save(appointment);
}
}
uj5u.com熱心網友回復:
回傳ResponseEntity
不是您真正想從服務層做的事情——它是業務邏輯的地方,而不是 Web 回應的地方。所以目前你的服務系結到spring MVC。
積極的一面是,它不直接呼叫資料庫,而是將 DAO 作為依賴項注入。
所以真正的問題是你想在這里測驗什么?如果要測驗服務本身,可以通過簡單的單元測驗對其進行單元測驗,這里不需要spring:
- 為存盤庫創建模擬并將它們注入到服務實體中,這將成為您測驗的“主題”,就好像它們是真正的 DAO 一樣。
- 然后指定期望,例如“如果有人呼叫
findAll
則回傳實??體物件串列。同樣,這里不需要使用任何型別的資料庫 - 驗證您使用某些斷言庫測驗的服務方法的預期回傳結果
而已。當然,如果您將回傳真實物件而不是ResponseEntity
從某些方法回傳,那么您的代碼將更加清晰,因此測驗會更容易,因為您將驗證真實的域物件而不是 web-mvc 抽象。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/505308.html