In this tutorial you will show how to write a simple Mockito based Unit Test by following this steps:
- Download a stable version of Mocki http://code.google.com/p/mockito/
- Extract mockito-1.1.2.zip to a folder
- Open Eclipse for Java IDE
- Create a new project (name it helloMockito)
- Create a ITranslator interface
i.Make sure ITranslator interface has the following code ii.package org.helloopensource.greetings;
public interface ITranslator {
public abstract String translate(String fromLanguage, String toLanguage, String word);
}
1.Create a Greeting class i.Make sure Greeting class has the following code ii.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 i.Click on add external JARs ii.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) i.Click on finish ii.Make sure JUnit library is added to the build path iii.Add the following code for the GreetingTest code: iv.package org.helloopensource.greetings;