Uploaded image for project: 'OpenMRS Core'
  1. OpenMRS Core
  2. TRUNK-5811 Update Junit from 4 to 5
  3. TRUNK-5813

Replace all ExpectedException Rules and the @Test(expected..)

    XMLWordPrintable

Details

    • Sub-task
    • Status: Closed
    • TBD
    • Resolution: Fixed
    • None
    • Core 2.4.0
    • None
    • High

    Description

      Background

      Junit 5 will no longer have Junit Rules. Junit 5 will also no longer allow setting an expected Exception in the @Test annotation. So in Junit 4.13 assertThrows was introduced which is from now on the preferred way of testing that an exception has been thrown in a test. So the work here is a task on the path to migrating to Junit 5.
      For details on the new assertion see https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.13.md#pull-request-1154-and-1504-add-assertthrows
      and
      https://github.com/junit-team/junit4/wiki/Exception-testing#using-assertthrows-method

      Task

      Replace all occurrences of ExpectedException like in this sample test

       @Rule
      public ExpectedException expectedException = ExpectedException.none();
      
      @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);
      
      }
      

      with assertThrows and assertThat  as in the example below:

      @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));
      
      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"));
      
      }
      

      The same should also be done for tests that set the expected Exception in the test annotation like @Test(expected = APIException.class)

      Gliffy Diagrams

        Attachments

          Issue Links

            Activity

              People

                achilep Pahonsi Bebeto Achile
                achilep Pahonsi Bebeto Achile
                Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: