My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

JMockit allows developers to write unit/integration tests without the testability issues typically found with other mocking APIs. Tests can easily be written that will mock final classes, static methods, constructors, and so on. There are no limitations.

The JMockit mocking API is simple, consistent, and minimal. There are no special methods or annotations that need to be used in test code, apart from those that really make sense. In general, the use of JMockit APIs for mocking leads to test code that is better structured and more readable.

Most importantly, with JMockit all programming practices, patterns, and styles are supported equally, without forcing developers to make compromises just for the sake of unit testing.

   // Declare the mocks you need through mock fields or parameters.
   public void testDoOperationAbc(final DependencyXyz mock)
   {
      // Record the desired results for method invocations, *if* any are needed.
      new NonStrictExpectations() {
         // An internal dependency ("new-ed" later) can be mocked as well:
         AnotherDependency anotherMock;

         {
            // Simply invoke a mocked method/constructor to record an expectation.
            anotherMock.doSomething("test");
            result = 123; // assign results (values to return, exceptions to throw)

            // Will cause an exception to be thrown if called:
            DependencyXyz.someStaticMethod(); result = new IOException();
         }
      };

      // Exercise the code under test.
      new ServiceAbc(mock).doOperation("some data");

      // Verify expected invocations, if any.
      new Verifications() {{
         // Use argument matchers (anyXyz, etc.) for any subset of parameters.
         mock.complexOperation(true, anyInt, null);
         times = 1; // specify invocation count constraints if/as needed
      }};
   }

Getting started | About | Tutorial | API documentation | Sample tests | Sample coverage report | Version history

Powered by Google Project Hosting