EasyMock Hello World

EasyMock Hello World

In Object oriented software development mock(Dummy) objects are simulation of real object in controlled ways. This is required when you start unit testing of your classes. Unit test is nothing but testing of your single program which is dependent on other classes. To perform unit test you create mock object to simulate same behavior of real, complex objects, it’s useful when it’s impossible or impractical to incorporate real objects to complete your testing.

EasyMock is testing framework which can be use to create dynamic mock objects it provides mock objects by generating them on fly by using java proxy mechanism. To start with EasyMock let’s first setup everything up and do Hello World testing then we next tutorial we will move on some dummy real time testing.

Note: We will do testing with today’s latest version 3.3.1 which supports mocking classes. If you have experience working with old EasyMock it was not possible to create mock objects for classes and you had to create interface which was causing chaos to to create lots of interfaces before performing real unit testing.

  • Create maven java project below is final structure:

EasyMock Hello World

  • pom.xml to dependency details:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javahonk</groupId>
	<artifactId>EasyMockHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>EasyMockHelloWorld</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.easymock</groupId>
			<artifactId>easymock</artifactId>
			<version>3.3.1</version>
		</dependency>
	</dependencies>
</project>
  • Class from data comes HelloWorld.java:
package com.javahonk;

public class HelloWorld {

	public String printHelloWorld() {

		String value = "";
		//Suppose it return value Hello World!. We will create mock object which will return same
		value = "Returns value that we don't know";
		return value;

	}

}
  • Client class which consume HelloWorld.java:
package com.javahonk;

public class HelloWorldUser {
	
	public String getHelloWorld(HelloWorld helloWorld) {
		return helloWorld.printHelloWorld();
	}

}
  • EasyMock testing classes to perform unit test:
package com.javahonk;

import static org.junit.Assert.assertEquals;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



public class HelloWorldTest {

	private HelloWorld helloWorld;

	@Before
	public void setUp() throws Exception {
		helloWorld = EasyMock.createMock(HelloWorld.class);
	}

	@After
	public void tearDown() throws Exception {
		helloWorld = null;
	}

	@Test
	public void testPrintHelloWorld() {

		EasyMock.expect(helloWorld.printHelloWorld()).andReturn("Hello World!");
		EasyMock.replay(helloWorld);

		HelloWorldUser helloWorldUser = new HelloWorldUser();
		assertEquals("Hello World!", helloWorldUser.getHelloWorld(helloWorld));
		EasyMock.verify(helloWorld);
	}

}
  • I understand what you are thinking, what the heck we doing above. Let me clear this out:
helloWorld = EasyMock.createMock(HelloWorld.class);
  • Here we do create mock object of HelloWorld.class
EasyMock.expect(helloWorld.printHelloWorld()).andReturn("Hello World!");
  • Above we have define what EasyMock expect. When it will call helloWorld.printHelloWorld() it will expect return type which is String so we are using andReturn(“Hello World!”) to create mock data.
EasyMock.replay(helloWorld);
  • Calling EasyMock.replay method makes mock object ready to use.
HelloWorldUser helloWorldUser = new HelloWorldUser();
		assertEquals("Hello World!", helloWorldUser.getHelloWorld(helloWorld));
		EasyMock.verify(helloWorld);
  • On above three lines is self explanatory. We are creating HelloWorodUser object then use assertEquals to compare actual and return value and finally verify it.

Now to run: Right click HelloWorldTest.java –> Run As –> JUnit:

EasyMock Hello World

  • You will see Unit test got successful:

EasyMock Hello World

Leave a Reply

Your email address will not be published. Required fields are marked *