| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Mockito

Page history last edited by PBworks 16 years ago

Hello Mockito
 

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

 

Main Learning's

After following this tutorial you will learn how to:

  • Download and install Mockito
  • Use Mockito in Eclipse
  • Create a Mockito based Unit Test
  • Compile and run the ~Mockito based Unit Test
     

Versions used:

  • Eclipse SDK 3.3.1
  • JDKversion 1.5.0_12
  • Mockito

 

Assumptions:

 

The JDK is properly installed

You are familiar with Eclipse IDE from Java

 

 

Tutorial Steps:

  1. Go to http://code.google.com/p/mockito/
  2. Download a stable version of Mockito – e.g mockito1.2
  3. Extract mockito-1.1.2.zip to a folder – e.g C:Program Files/mockito-1.2.0
  4. Open Eclipse for Java IDE
  5. Create a new project (name it helloMockito)
    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);

 

                    }

 

 

  1. 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;

                            }

                    }

  1. Add the mockito-all-1.2.jar to the build path
    1. Click on add external JARs
    2. Select the mockito-all-1.2.jar files (e.g C:Program Files/mockito-1.2.0/mockito-all-1.2.jar)
  2. 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.junit.Assert.*;

                    import static org.mockito.Mockito.stub;

                    import static org.mockito.Mockito.verify;

                    import static org.mockito.Mockito.mock;

 

                    import org.junit.Test;

 

 

 

                    public class GreetingTest {

 

                        @Test

                        public void shouldTestGreetingInItalian(){

                            //setup

                            ITranslator mockTranslator = mock(ITranslator.class);

                            Greeting greeting = new Greeting(mockTranslator);

                            stub(mockTranslator.translate("English", "Italian", "Hello")).toReturn("Ciau");

                           //execute

                            assertEquals("Ciau Paulo", greeting.sayHello("Italian", "Paulo"));

                            //verify

                            verify(mockTranslator).translate("English", "Italian", "Hello");

                        }

                    }      

 

  1. Run GreetingTest Unit Test

 

Comments (0)

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