Testing Approach - Mocks vs Extending ContextSensitiveTests

Just wanted to know what the testing best practices are for core and modules:

  1. Extending the ContextSensitiveTests

  2. Using Mocks

When to use one or the other

We generally prefer mocks On Writing Mock Test for openmrs

More details behind this answer…

Note the concept of the Testing Pyramid. Ideally we want to have more fast, independent unit tests, and fewer slow interrelated integration tests.

Unit tests use Mocks to stub out the behavior of collaborators. Integration tests load up the Spring application context and use the real collaborators.

And we want to have lots of mock-based tests, and few ContextSensitiveTests, and as Martin Fowler says in the article I linked:

I always argue that high-level tests are there as a second line of test defense.

Now, if you look at most actual OpenMRS codebases, things are the reverse of what we want, because it feels easier for the developer to cut corners and just do a few high-level end to end tests. And once you’ve started down this path, it’s hard to recover.

So, do use Mocks, and use them from the beginning! :slight_smile: