O'Reilly logo

Mockito Cookbook by Marcin Grzejszczak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mockito versus JMock

In this recipe, we will write a simple test using JMock that verifies the behavior of the system under test when an exception is thrown.

Getting ready

To profit from JMock, you need to add it to your classpath. There are three factors that you must take into consideration when adding JMock to your project, as follows:

  • Jmock: This contains the core of JMock (pick it if you want to use TestNG)
  • jmock-junit4: This is to integrate JUnit with JMock (pick this one if you want to use JUnit 4+)
  • jmock-legacy: This allows you to create mocks of classes

The following is the JMock configuration for Gradle for a JUnit-based project:

testCompile "org.jmock:jmock-junit4:2.6.0" testCompile "org.jmock:jmock-legacy:2.6.0" testCompile "org.jmock:jmock:2.6.0" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required