helloopensource

 

easymock

Page history last edited by paulo 1 yr ago

Hello EasyMock

This Tutorial will show you how to write a simple EasyMock based Unit Test

 

Main Learning's

After following this tutorial you will learn how to:
  • Download and install EasyMock
  • Use EasyMock in Eclipse
  • Create a EasyMock based Unit Test
  • Compile and run the EasyMock based Unit Test
 

Versions used:

  • Eclipse SDK 3.2.2
  • JDKversion 1.5.0_12
  • EasyMock 2.3

 

Assumptions:

 

 

The JDK is properly installed

You are familiar with Eclipse IDE fro Java

 

 

Tutorial Steps:

  1. Go to http://www.easymock.org/Downloads.html
  2. Download a stable version of EasyMock – e.g easymock2.3
  3. Extract easymock2.3.zip to a folder – e.g C:\Program Files\easymock  
  4. Open Eclipse for Java IDE
  5. Create a new project (name it helloEasyMock)
    1. Click on Finish
  6. Create a ITranslator interface
    1. Make sure ITranslator interface has the following code
    2. package org.helloopensource.greetings;

       

      public interface ITranslator {

          public abstract String translate(String fromLanguage, String toLanguage, String word);

       

      }

       

       

  7. Create a Greeting class
    1. Make sure Greeting class has the following code
    2. package org.helloopensource.greetings;

       

      public class Greeting {

          private ITranslator translator;

          public Greeting(ITranslator translator) {

              this.translator = translator;

          }

       

          public String sayHello(String language, String name) {

              return translator.translate("English", language, "Hello") + " " + name;

          }

      }

  8. Add the easymock.jar to the build path
    1. Click on add external JARs
    2. Select the easymock.jar files (e.g C:\Program Files\easymock\easymock2.3\easymock.jar) 
  9. Create a new Unit Test (new / Unit Test)
    1. Click on finish
    2. Make sure JUnit library is added to the build path
    3. Add the following code for the GreetingTest code:
    4. package org.helloopensource.greetings;

       

      import static org.easymock.EasyMock.createMock;

      import static org.easymock.EasyMock.expect;

      import static org.easymock.EasyMock.replay;

      import static org.easymock.EasyMock.verify;

      import junit.framework.TestCase;

       

      public class GreetingTest extends TestCase {

          public void testGreetingInAnotherLanguage() throws Exception {

              ITranslator mockTranslator = createMock(ITranslator.class);

              Greeting greeting = new Greeting(mockTranslator);

              expect(mockTranslator.translate("English", "Hindi", "Hello")).andReturn("Namaste");

              replay(mockTranslator);

              assertEquals("Namaste Paulo", greeting.sayHello("Hindi", "Paulo"));

              verify(mockTranslator);

          }    

  10. Run GreetingTest Unit Test

 

 

Comments (0)

You don't have permission to comment on this page.