So the canonical example for this:
@Test(expected = APIException.class)
public void add_shouldNotAllowDuplicateCodedAllergen(){
Allergy allergy1 = new Allergy();
Concept concept = new Concept();
allergy1.setAllergen(new Allergen(null, concept, null));
Allergy allergy2 = new Allergy();
allergy2.setAllergen(new Allergen(null, concept, null));
allergies.add(allergy1);
allergies.add(allergy2);
}
In JUnit5 would be something like this:
@Test
public void add_shouldNotAllowDuplicateCodedAllergen(){
Allergy allergy1 = new Allergy();
Concept concept = new Concept();
allergy1.setAllergen(new Allergen(null, concept, null));
Allergy allergy2 = new Allergy();
allergy2.setAllergen(new Allergen(null, concept, null));
allergies.add(allergy1);
assertThrows(APIException.class, () -> allergies.add(allergy2));
}
By itself:
@Rule
public ExpectedException expectedException = ExpectedException.none();
Does not have any equivalent in JUnit5, so this code should be removed. However, where expectedException
is something other than ExpectedException.none()
it’s normally used for something like this:
@Test
public void savePatientProgram_shouldTestThrowPatientStateRequiresException() {
expectedException.expect(APIException.class);
expectedException.expectMessage("'PatientProgram(id=1, patient=Patient#2, program=Program(id=1, concept=Concept #1738, " +
"workflows=[ProgramWorkflow(id=1), ProgramWorkflow(id=2)]))' failed to validate with reason: states: State is required for a patient state");
PatientProgram patientProgram = pws.getPatientProgram(1);
for (PatientState state : patientProgram.getStates()) {
state.setState(null);
}
pws.savePatientProgram(patientProgram);
}
The JUnit5 idiom for this is:
@Test
public void savePatientProgram_shouldTestThrowPatientStateRequiresException() {
PatientProgram patientProgram = pws.getPatientProgram(1);
for (PatientState state : patientProgram.getStates()) {
state.setState(null);
}
APIException exception = assertThrows(APIException.class, () -> pws.savePatientProgram(patientProgram));
// you could also use assertEquals() here
assertThat(exception.getMessage(), is("'PatientProgram(id=1, patient=Patient#2, program=Program(id=1, concept=Concept #1738, workflows=[ProgramWorkflow(id=1), ProgramWorkflow(id=2)]))' failed to validate with reason: states: State is required for a patient state"));
}