Testing Applications with JUnit5 and JMock. Part 2

  1. As always, we start the listing by importing all the necessary objects we need (1). As you can see, unlike EasyMock, the JMock framework doesn’t rely on any static import features.
  2. JUnit5 provides a programmatic way to register extensions. For JMock, this is done by annotating a JUnit5Mockery non-private instance field with @RegisterExtension. The context object serves us to create mocks and to define expectations (2).
  3. In (3) we declare the AccountManager which we want to mock. Like EasyMock, the core JMock framework only mocks interfaces.
  4. In the @BeforeEach method, which is executed before each of the @Test methods, we programmatically create the mock by means of the context object (4).
  5. As with any of the previous listings, we declare two accounts which we’ll use to transfer money in between (5).
  6. In (6) we start declaring the expectations, by constructing a new Expectations object.
  7. In (7) we declare the first expectation, each expectation having the form:
  • We open an HTTP connection (1)
  • We read all the content that is received (2)
  • If an error occurs, we return null (3)
  1. We start the test-case by registering the JMock extension. The JUnit5Mockery non-private instance field context is annotated with @RegisterExtension (1).
  2. To tell JMock to create mock objects not only for interfaces, but also for classes, we need to set the imposteriser property of the context (2). Now we can continue creating mocks the normal way.
  3. In (3) we declare and programmatically initialize the two objects we want to create mocks of.
  4. In (4) we start declaration of the expectations. Notice the fine way we declare the consecutive execution of the read() method of the stream (5), and also the returned values.
  5. In (6) we call the method under test and in (7) we assert the expected result.
  6. For a full view of how to use the JMock mocking library, we also provide another @Test method, which tests our WebClient under exceptional conditions. In (8) we declare the expectation of the close() method being triggered and in (9) we instruct JMock to raise an IOException when this trigger happens.



Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How we achieved green builds in an enterprise Ember.js app

An Introduction to React Testing Library and Test Driven Development

Converting Codecademy to TypeScript 1: Converting Ourselves

Codecademy logo, a heart emoji, and the TypeScript logo

Start with ES6 and Some of Its Special Features

New to medium.

What the shit is the Shadow DOM, anyway?

Run your own Unity Package Server!

How does React Context work?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luxoft Training

Luxoft Training


More from Medium

Object-relational Mapping Using JPA, Hibernate and Spring Data JPA.

Graal “The Holy Grail” VM (Part I)

Cache Access Patterns and Eviction Policies (LRU and LFU implementations in Java)

Replace Text in Word Documents in Java