1
Pins

Mockito Tutorial

published 829 days, 11 hours, 44 minutes ago posted by vasantvasant 831 days, 13 hours, 10 minutes ago
Monday, January 16, 2012 2:42:12 PM GMT Saturday, January 14, 2012 1:15:17 PM GMT

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;

category: Java EE | clicked: 6 | | source: www.ajaxprojects.com | show counter code
tags: mockito, Tutorials


To post comments using your Facebook,Twitter, Gmail etc credentials in the Discus comments section. Or use your Javapins credentials at the end of the page.
To post your comment as a Javapins user please login or signup to Javapins database.