Creating Mock Objects

The following instructions guide you through creating Mock Objects using CoViewDeveloper. It assumes your workspace already contains a project with JUnit test cases. CoView uses the JMockit libraries to create Mock Objects.

Through CoViewDeveloper and JMockit, mocks can be created for the following:

  • Argument objects passed directly to the method under test or fields which can be set by public methods of the class under test.
  • Public instance (i.e., non-static) methods of the class under test (not including the method under test) which the method under test invokes.
  • private/protected fields of the class under test containing some objects that the method under tes uses directly, without calling public getters.
  • private/protected methods of the class under test wich return some objects that the method under test uses. The method under test invokes the private/protected methods directly, without the help of other public methods
  • Objects instantiated within the method under test using the 'new' operator
  • Various static invocations from the method under test

To create mocks:

  • 1. Create a CoView JUnit Test Case and select the appropriate Mock Object option. Note: If you select the option 'Also generate Mocks for all invocations lying on corresponding paths', then all Mock for invocations on the path will be created in the JUnit test case. Given that you sometimes do not want to generate every mock along a path, you have the option of creating the mocks one at a time. Proceed to step 2 if this is the case.
  • 2. From the Package Explorer, open the class under test in the CoView Editor.
  • 3. Place the cursor on the invocation to be mocked and select either CTRL-1 or right-click and select CoView > Highlight Invocations. You will be presented with decorators in the left-hand column.

    • Add Mock: Mock the highlighted invocation
    • Mock Exists:Mock exists for highlighted invocation

  • For example:

  • CoView Mock Object Creator
  • 4. Click on the Add Mock and select Mock [invocation] and the CoView Mock Wizard will be displayed.

The CoView Mock Wizard (below) lets you create a new mock or select existing mocks. There are three sections to the CoView Mock Wizard.

  • Select the Invocation to Mock: The first section highlights the mockable invocations. Invocations highlighted in pink have no corresponding mocks. Invocations highlighted in purple have mocks. You can change the invocation you want to mock by simply clicking on the new invocation.
  • Select what test case uses the new mock: Since CoView creates JUnit tests for each path, you can specify the test case (and therefore the path) the new mock will be used in.
  • Select the options for the new mock: In this section, you can provide the mock name and additional options in case the mock needs ot be repeated.

CoView Mock Wizard