Thursday, 15 June 2017

PowerMock, EasyMock : Mock Static Methods

In this post, I am going to explain how to mock static methods.

public class EmployeeService {

         public static List<String> getEmployeeFirstNames(List<Employee> employees){
                 throw new MethodNotImplementedException();
         }
}


As you see the above snippet, EmployeeService class has unimplemented method 'getEmployeeFirstNames'. If any methods that are using 'getEmployeeFirstNames' method in their implementation end up in 'MethodNotImplementedException'.

By using powermock, we can mock and test static methods like below.

PowerMock.mockStatic(EmployeeService.class);
EasyMock.expect(EmployeeService.getEmployeeFirstNames(employees)).andReturn(employeeNames);
PowerMock.replayAll();

PowerMock.mockStatic(EmployeeService.class)
Above statement enable mocking for the class EmployeeService.

EasyMock.expect(EmployeeService.getEmployeeFirstNames(employees)).andReturn(employeeNames)
When getEmployeeFirstNames method is called with 'employees' as an argument, then return employeeNames, instead of invoking original method.

PowerMock.replayAll();
Replay all classes and mock objects known by PowerMock. This includes all classes that are prepared for test using the @PrepareForTest or @PrepareOnlyThisForTest annotations and all classes that have had their static initializers removed by using the @SuppressStaticInitializationFor annotation.

Find the complete working application below.

MethodNotImplementedException.java

package com.sample.model;

public class MethodNotImplementedException extends RuntimeException {
 private static final long serialVersionUID = 1L;

 public MethodNotImplementedException() {
  super();
 }

 public MethodNotImplementedException(String message) {
  super(message);
 }

}


Employee.java

package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private double salary;
 private String country;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public Employee(int id, String firstName, String lastName, double salary, String country) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.salary = salary;
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append(", salary=").append(salary).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

EmployeeService.java

package com.sample.service;

import java.util.List;

import com.sample.model.Employee;
import com.sample.model.MethodNotImplementedException;

public class EmployeeService {

 public static List<String> getEmployeeFirstNames(List<Employee> employees){
  throw new MethodNotImplementedException();
 }
}


EmployeeUtil.java

package com.sample.util;

import java.util.ArrayList;
import java.util.List;

import com.sample.model.Employee;

import com.sample.service.EmployeeService;

public class EmployeeUtil {


 /**
  * Return all employee names in upper case.
  * 
  * @param employees
  * @return
  */
 public List<String> getAllEmployeeFirstNamesInUpperCase(List<Employee> employees) {
  List<String> empNamesInLowerCase = EmployeeService.getEmployeeFirstNames(employees);
  List<String> result = new ArrayList<>(empNamesInLowerCase.size());

  for (String name : empNamesInLowerCase) {
   if (name == null) {
    continue;
   }

   result.add(name.toUpperCase());
  }

  return result;
 }

}


EmployeeUtilTest.java

package com.sample.util;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.sample.model.Employee;
import com.sample.service.EmployeeService;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ EmployeeService.class })
public class EmployeeUtilTest {

 private List<Employee> employees;
 private List<String> employeeNames;

 @Before
 public void initList() {
  Employee emp1 = new Employee(1, "Hari krishna", "Gurram", 12345, "India");
  Employee emp2 = new Employee(2, "Kiran", "Kumnoor", 234556, "Germany");
  Employee emp3 = new Employee(3, "Soumen", "Mondle", 56789, "India");
  Employee emp4 = new Employee(4, "Sravya", "Guruju", 567890, "Japan");
  Employee emp5 = new Employee(5, "Rachit", "Kumar", 123645, "India");

  employees = new ArrayList<>();
  employeeNames = new ArrayList<>();

  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);

  employeeNames.add("Hari krishna");
  employeeNames.add("Kiran");
  employeeNames.add("Soumen");
  employeeNames.add("Sravya");
  employeeNames.add("Rachit");
 }

 @Test
 public void getAllEmployeeFirstNamesInUpperCase_listOfEmployees_ListOfEmployeeNamesinUpperCase() {
  PowerMock.mockStatic(EmployeeService.class);
  EasyMock.expect(EmployeeService.getEmployeeFirstNames(employees)).andReturn(employeeNames);
  PowerMock.replayAll();
  
  EmployeeUtil empUtil = new EmployeeUtil();

  List<String> employeeFirstNames = empUtil.getAllEmployeeFirstNamesInUpperCase(employees);

  assertEquals(employeeFirstNames.size(), 5);
  assertEquals(employeeFirstNames.get(0), "Hari krishna".toUpperCase());
  assertEquals(employeeFirstNames.get(1), "Kiran".toUpperCase());
  assertEquals(employeeFirstNames.get(2), "Soumen".toUpperCase());
  assertEquals(employeeFirstNames.get(3), "Sravya".toUpperCase());
  assertEquals(employeeFirstNames.get(4), "Rachit".toUpperCase());

 }
}


Previous                                                 Next                                                 Home

Wednesday, 14 June 2017

EasyMock & PowerMock : Hello World Application

Let me try to explain the usage of mocking with simple example.

EmployeeService.java: This class provides 'getEmployeeFirstNames' method, which takes list of employees as input and return the list of all the first names of employees.

EmployeeUtil.java: It uses the method 'getEmployeeFirstNames' that is implemented by EmployeeService

Suppose let us assume, EmployeeUtil.java class implemented its functionality, by assuming EmployeeService will be implemented in time. Unfortunately, EmployeeService class is not yet implemented. Without mocking, you can't unit test the methods of EmployeeUtil class, since it is dependent on EmployeeService. By Mocking the methods of EmployeeService class we can test Employeeutil class.

MethodNotImplementedException.java
package com.sample.model;

public class MethodNotImplementedException extends RuntimeException {
 private static final long serialVersionUID = 1L;

 public MethodNotImplementedException() {
  super();
 }

 public MethodNotImplementedException(String message) {
  super(message);
 }

}

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private double salary;
 private String country;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public Employee(int id, String firstName, String lastName, double salary, String country) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.salary = salary;
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append(", salary=").append(salary).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

EmployeeService.java
package com.sample.service;

import java.util.List;

import com.sample.model.Employee;
import com.sample.model.MethodNotImplementedException;

public class EmployeeService {

 public List<String> getEmployeeFirstNames(List<Employee> employees){
  throw new MethodNotImplementedException();
 }
}

EmployeeUtil.java
package com.sample.util;

import java.util.ArrayList;
import java.util.List;

import com.sample.model.Employee;

import com.sample.service.EmployeeService;

public class EmployeeUtil {

 private EmployeeService empService;

 public EmployeeUtil(EmployeeService empService) {
  this.empService = empService;
 }

 /**
  * Return all employee names in upper case.
  * 
  * @param employees
  * @return
  */
 public List<String> getAllEmployeeFirstNamesInUpperCase(List<Employee> employees) {
  List<String> empNamesInLowerCase = empService.getEmployeeFirstNames(employees);
  List<String> result = new ArrayList<>(empNamesInLowerCase.size());

  for (String name : empNamesInLowerCase) {
   if (name == null) {
    continue;
   }

   result.add(name.toUpperCase());
  }

  return result;
 }

}

EmployeeUtilTest.java
package com.sample.util;

import static org.junit.Assert.*;

import java.util.*;

import org.junit.Before;
import org.junit.Test;

import com.sample.service.EmployeeService;

import com.sample.model.Employee;

public class EmployeeUtilTest {

 private List<Employee> employees;
 private List<String> employeeNames;

 @Before
 public void initList() {
  Employee emp1 = new Employee(1, "Hari krishna", "Gurram", 12345, "India");
  Employee emp2 = new Employee(2, "Kiran", "Kumnoor", 234556, "Germany");
  Employee emp3 = new Employee(3, "Soumen", "Mondle", 56789, "India");
  Employee emp4 = new Employee(4, "Sravya", "Guruju", 567890, "Japan");
  Employee emp5 = new Employee(5, "Rachit", "Kumar", 123645, "India");

  employees = new ArrayList<>();
  employeeNames = new ArrayList<>();

  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);

  employeeNames.add("Hari krishna");
  employeeNames.add("Kiran");
  employeeNames.add("Soumen");
  employeeNames.add("Sravya");
  employeeNames.add("Rachit");
 }

 @Test
 public void getAllEmployeeFirstNamesInUpperCase_listOfEmployees_ListOfEmployeeNamesinUpperCase() {
  EmployeeService employeeService = new EmployeeService();

  EmployeeUtil empUtil = new EmployeeUtil(employeeService);

  List<String> employeeFirstNames = empUtil.getAllEmployeeFirstNamesInUpperCase(employees);

  assertEquals(employeeFirstNames.size(), 5);
  assertEquals(employeeFirstNames.get(0), "Hari krishna".toUpperCase());
  assertEquals(employeeFirstNames.get(1), "Kiran".toUpperCase());
  assertEquals(employeeFirstNames.get(2), "Soumen".toUpperCase());
  assertEquals(employeeFirstNames.get(3), "Sravya".toUpperCase());
  assertEquals(employeeFirstNames.get(4), "Rachit".toUpperCase());

 }
}


Run above application, you will end up in 'MethodNotImplementedException'.

com.sample.model.MethodNotImplementedException
 at com.sample.service.EmployeeService.getEmployeeFirstNames(EmployeeService.java:11)
 at com.sample.util.EmployeeUtil.getAllEmployeeFirstNamesInUpperCase(EmployeeUtil.java:25)
 at com.sample.util.EmployeeUtilTest.getAllEmployeeFirstNamesInUpperCase_listOfEmployees_ListOfEmployeeNamesinUpperCase(EmployeeUtilTest.java:49)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

How to resolve above problem?
By creating a mock object to the class 'EmployeeService', we can test EmployeeUtil class.


EmployeeUtilTest.java

package com.sample.util;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.sample.model.Employee;
import com.sample.service.EmployeeService;

@RunWith(PowerMockRunner.class)
@PrepareForTest( {EmployeeService.class} )
public class EmployeeUtilTest {

 private List<Employee> employees;
 private List<String> employeeNames;

 @Before
 public void initList() {
  Employee emp1 = new Employee(1, "Hari krishna", "Gurram", 12345, "India");
  Employee emp2 = new Employee(2, "Kiran", "Kumnoor", 234556, "Germany");
  Employee emp3 = new Employee(3, "Soumen", "Mondle", 56789, "India");
  Employee emp4 = new Employee(4, "Sravya", "Guruju", 567890, "Japan");
  Employee emp5 = new Employee(5, "Rachit", "Kumar", 123645, "India");

  employees = new ArrayList<>();
  employeeNames = new ArrayList<>();

  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);

  employeeNames.add("Hari krishna");
  employeeNames.add("Kiran");
  employeeNames.add("Soumen");
  employeeNames.add("Sravya");
  employeeNames.add("Rachit");
 }

 @Test
 public void getAllEmployeeFirstNamesInUpperCase_listOfEmployees_ListOfEmployeeNamesinUpperCase() {
  EmployeeService mockedEmployeeService = EasyMock.createMock(EmployeeService.class);
  EasyMock.expect(mockedEmployeeService.getEmployeeFirstNames(employees)).andReturn(employeeNames);
  EasyMock.replay(mockedEmployeeService);

  EmployeeUtil empUtil = new EmployeeUtil(mockedEmployeeService);

  List<String> employeeFirstNames = empUtil.getAllEmployeeFirstNamesInUpperCase(employees);

  assertEquals(employeeFirstNames.size(), 5);
  assertEquals(employeeFirstNames.get(0), "Hari krishna".toUpperCase());
  assertEquals(employeeFirstNames.get(1), "Kiran".toUpperCase());
  assertEquals(employeeFirstNames.get(2), "Soumen".toUpperCase());
  assertEquals(employeeFirstNames.get(3), "Sravya".toUpperCase());
  assertEquals(employeeFirstNames.get(4), "Rachit".toUpperCase());

 }
}

Let’s try to understand what I did in the above application.

@RunWith(PowerMockRunner.class)
Junit invokes the test of the class with PowerMockRunner class, instead of the runner built into Junit.

@PrepareForTest( {EmployeeService.class} )
This annotation tells PowerMock to prepare certain classes for testing. This annotation can be placed at both test classes and individual test methods. If placed on a class all test methods in this test class will be handled by PowerMock (to allow for testability). To override this behavior for a single method just place a @PrepareForTest annotation on the specific test method. This is useful in situations where for example you'd like to modify class X in test method A but in test method B you want X to be left intact. In situations like this you place a @PrepareForTest on method B.

EmployeeService mockedEmployeeService = EasyMock.createMock(EmployeeService.class);
Above statement creates a mock object of this class.

EasyMock.expect(mockedEmployeeService.getEmployeeFirstNames(employees)).andReturn(employeeNames);
When getEmployeeFirstNames method is called with 'employees' as an argument, then return employeeNames, instead of invoking original method.

EasyMock.replay(mockedEmployeeService);
Activate the mocked behavior. When you write 'EasyMock.expect(mockedEmployeeService.getEmployeeFirstNames(employees)).andReturn(employeeNames);',

You recorded the expected behavior. But, when you write 'EasyMock.replay(mockedEmployeeService);' , you are actually activating it.




Previous                                                 Next                                                 Home

Friday, 9 June 2017

EasyMock & PowerMock Tutorial

EasyMock is an open source testing framework to unit test Java applications. It is released under the Apache License. Where as PowerMock is an open source mocking library that extends mocking frameworks like EasyMock, Mockito. PowerMock provides two extension APIS.
         a. For EasyMock
         b. For Mockito

To use PowerMock,
         a. we require either EasyMock (or) Mockito
         b. Test frameworks like JUnit, TestNG
        
Currently PowerMock supports JUnit and TestNG. I am going to use PowerMock, EasyMock and Junit combination for this tutorial series. Most of the mocking frameworks in Java can't mock private, static and final methods. By using PowerMock, we can extend the frameworks like EasyMock and can able to mock private static methods.

I am going to use following maven dependencies.
 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.easymock/easymock -->
  <dependency>
   <groupId>org.easymock</groupId>
   <artifactId>easymock</artifactId>
   <version>3.4</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
  <dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-module-junit4</artifactId>
   <version>1.6.6</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-easymock -->
  <dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-api-easymock</artifactId>
   <version>1.6.6</version>
  </dependency>

 </dependencies>


Previous                                                 Next                                                 Home

Abdera: Get the entry by id

Feed interface provides ‘getEntry’ method, it return the entry by id.

Entry getEntry(String id)
Retrieves the first entry in the feed with the given id value.

Feedutil.java
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Optional;
import java.util.zip.GZIPInputStream;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.Parser;

/**
 * Utility class to parse Atom feed.
 * 
 * Usage :
 * <ul>
 * <li>Instantiate FeedUtil with atom document url. Call the getFeed method</li>
 * <li>to get the feed document associated with url.</li>
 * </ul>
 * 
 * @author Hari Krishna Gurram
 *
 */
public class FeedUtil {
 private String url;
 private InputStream in = null;
 private Feed feed = null;
 HttpURLConnection conn = null;

 public FeedUtil(String url) {
  this.url = url;
 }

 /**
  * 
  * @param url
  * @return Optional.empty if url is null (or) unable to parse the url, else
  *         return the Document object.
  */
 private Optional<Document<Feed>> getDocument() {
  if (url == null) {
   return Optional.empty();
  }

  try {
   URL urlTemp = new URL(url);

   conn = (HttpURLConnection) urlTemp.openConnection();
   conn.setRequestProperty("Accept-Encoding", "gzip");
   conn.connect();

   String contentEncoding = conn.getContentEncoding();
   boolean isGzip = contentEncoding != null && contentEncoding.contains("gzip");

   in = !isGzip ? conn.getInputStream() : new GZIPInputStream(conn.getInputStream());
   Abdera abdera = Abdera.getInstance();
   Parser parser = abdera.getParser();
   Document<Feed> document = parser.parse(in, url.toString());
   return Optional.of(document);

  } catch (Exception e) {
   e.printStackTrace();
  }

  return Optional.empty();
 }

 /**
  * 
  * @return the Feed object associated with given {@link url}. In any case if
  *         the feed creation is failed, then it return Optional.empty().
  */

 public synchronized Optional<Feed> getFeed() {
  if (feed != null) {
   return Optional.of(feed);
  }

  Optional<Document<Feed>> documentOpt = getDocument();

  if (!documentOpt.isPresent()) {
   return Optional.empty();
  }

  Document<Feed> document = documentOpt.get();

  feed = document.getRoot();
  return Optional.of(feed);
 }

 public void closeStreamAndConnection() {
  if (in != null) {
   try {
    in.close();
    //System.out.println("\nInput Stream is closed");
   } catch (IOException e1) {
    e1.printStackTrace();
   }
  }

  if (conn != null) {
   conn.disconnect();
   //System.out.println("Connection is disconnected");
  }
 }

 /**
  * This is just a safety check, if user forget to close the stram, it will
  * be closed once object is out of scope.
  */
 @Override
 public void finalize() {
  closeStreamAndConnection();
 }
}


ParserDemo.java
import java.io.IOException;
import java.util.Optional;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.ParseException;
import org.apache.abdera.writer.Writer;

public class ParserDemo {
 private static String url = "https://self-learning-java-tutorial.blogspot.com/atom.xml";
 private static String id = "tag:blogger.com,1999:blog-3062500619105519975.post-8736482404064859734";

 public static void main(String args[]) throws ParseException, IOException {
  FeedUtil util = new FeedUtil(url);

  Optional<Feed> feedOpt = util.getFeed();

  if (!feedOpt.isPresent()) {
   System.out.println("No feed present");
   return;
  }

  Feed feed = feedOpt.get();
  Entry entry = feed.getEntry(id);

  /* Print the feed to console */
  Writer writer = Abdera.getInstance().getWriterFactory().getWriter("prettyxml");
  entry.writeTo(writer, System.out);

  util.closeStreamAndConnection();

 }
}



Previous                                                 Next                                                 Home