Uploaded image for project: 'OpenMRS Core'
  1. OpenMRS Core
  2. TRUNK-3586

Update unit testing environment to use mocks

    XMLWordPrintable

    Details

    • Complexity:
      High

      Description

      Mockito is now included in core as of openmrs 1.9.0. See TRUNK-2586. All unit tests go down and hit the database. This makes them more like functional tests. (Or, precisely speaking, "Component Tests".) To make the tests run 10x faster we need to be using mocks.

      We need to start by demonstrating a good example of this, and we'd like to focus on some tests that take a particularly long time to run...

      For a few examples (see below):

      • rename the existing test class to end with "IT" (for integration test) instead of "Test". The idea is that we want to keep the existing test around, since we have it, and run it in CI, but not during the regular build. (See TRUNK-3774.)
      • write new tests in a new test class (ending with Test, i.e. the original name of the class you renamed to IT in the prior step.)
        • the new tests should not extend ContextSensitiveTest, and use mocks instead
      • bring what you just did back to a design call, and we'll decide whether this was the right approach to apply "everywhere" or we need further tweaks

      Examples to start with:

      • org.openmrs.scheduler.SchedulerServiceTest
      • org.openmrs.reporting.export.DataExportTest
      • org.openmrs.module.ModuleUtilTest

      Doing this will probably require using PowerMock to deal with the fact that the code you're testing makes static calls to things like Context.getAuthenticatedUser(), Context.getXyzService(), etc. To get around this, you need to use the @RunWith(PowerMockRunner.class) and @PrepareForTest(Context.class) annotations, and PowerMockito.mockStatic as you can see in these two examples:

      Another sample implementation (though this is older than the examples above) is in the last email in this thread: http://listarchives.openmrs.org/Context-class-td5471380.html

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                raff Rafal Korytkowski
                Reporter:
                bwolfe Ben Wolfe
                Designated Committer:
                Rafal Korytkowski
                Votes:
                1 Vote for this issue
                Watchers:
                8 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:

                    Time Tracking

                    Estimated:
                    Original Estimate - 4 days
                    4d
                    Remaining:
                    Time Spent - 2 days Remaining Estimate - 2 days
                    2d
                    Logged:
                    Time Spent - 2 days Remaining Estimate - 2 days
                    2d