Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

EasyMock Mocking Framework

EasyMock allows a test to create a mocked object directly from an interface, without having to define a class and mock every method defined by the API.  The test writer can define only the methods that are required to execute the test. In this example, a mock is created for the HostService API which is passed in to the HostToHostIntentCompiler class.

Code Block
languagejava
titleEasyMock Example
import static org.easymock.EasyMock.*;
 
    private HostService mockHostService;
 
    private static final String HOST_ONE_MAC = "00:00:00:00:00:01";
    private static final String HOST_TWO_MAC = "00:00:00:00:00:02";
    private static final String HOST_ONE_VLAN = "-1";
    private static final String HOST_TWO_VLAN = "-1";
    private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN;
    private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN;
    private HostId hostOneId = HostId.hostId(HOST_ONE);
    private HostId hostTwoId = HostId.hostId(HOST_TWO);
 
    @Test
    public void testHostToHostIntentCompiler() throws Exception {
        mockHostService = EasyMock.createMock(HostService.class);
        expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes();
        expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes();
        replay(mockHostService);
 
        final HostToHostIntentCompiler compiler = new HostToHostIntentCompiler();
		compiler.hostService = mockHostService;

        final Intent intent = new HostToHostIntent(APPID, hid(oneIdString), hid(twoIdString),
                                                   selector, treatment);
 
        final List<Intent> result = compiler.compile(intent, null, null);
        assertThat(result, is(Matchers.notNullValue()));
        assertThat(result, hasSize(2));
    }
 
 

...