jMock Tutorial for Beginners

jmock tutorial for beginners
Sommario

jMock Tutorial for beginners with Test Project

What is jMock?

jMock is a library which helps to create test-driven development of Java code with mock objects.With the help of jMock we can test class independently which depends on another class.

Suppose if one class depends on other class we can create a mock of dependent class and set the initial properties to test the dependent class.

Imock Tutorial

jMock Tutorial Example:

Note: We are using the Eclipse platform for this Test Project. You can download Eclipse from Qui.

Make sure Java SE Runtime Environment is already installed for Eclipse installer to initiate installation.

Create a Test project in java named Test project.Include following jars in its classpath:

Jmock TutorialPin

  • jmock-2.5.1.jar
  • hamcrest-core-1.1.jar
  • hamcrest-library-1.1.jar
  • JUnit-4.5.jar

Jmock Tutorial For BeginnersPin

Create an interface named ITestInterface.Put the following code in this interface :

package test;

public interface ITestInterface

{

public int test();

}

What Is JmockPin

Create TestClass1.Put the following code in this class

package test;

public class TestClass1 implements ITestInterface

{

public int test()

{

return 3;

}

}

Jmock ExamplePin

Create TestClass2.Put the following code in this class





package test;

public class TestClass2

{

ITestInterface testInterface;

public TestClass2()

{

this.testInterface=testInterface;

}

public int testMock()

{

int result=testInterface.test();

return result;

}

}

Jmock Tutorial PdfPin

Create TestClass3.Put the following code in this class

package test;

import junit.framework.Assert;

import org.jmock.Mockery;

import org.junit.Test;

public class TestClass3

{

@Test

public void testJmock()

{

org.jmock.Mockery TestInterfaceMock= new Mockery();

final ITestInterface testInterface=TestInterfaceMock.mock(ITestInterface.class);

TestInterfaceMock.checking(new org.jmock.Expectations()

{{

oneOf(testInterface).test();

will(returnValue((3)));

}});

int j=testInterface.test();

System.out.println(j);

Assert.assertEquals(j, 3);

}

}

Jmock For BeginnersPin

As from above example it is clear that TestClass1 implements the interface ITestInterface. Here if we observe TestClass2 is dependent on TestClass1.So to make it unit testable we are not doing new of TestClass1 in TestClass2.We are using mock object.

In TestClass3 we are making the Mock object of ITestInterface.We are setting the expectation for this object to return 3 when it is called during the unit testing of TestClass2.For example in TestClass3 when we make a call to testInterface.test() the call goes to TestClass2.In TestClass2 when we call testInterface.test() the value is returned by the mock object.In this way we are testing TestClass2 independently.

To test above Example right click on TestClass3 and select run as Junit test.


Junit TestPin
Junit Test

It will show the following screen:

Junit Test RunPin

Test case executed successfully!

Anson Antonio
Anson Antonio
Anson è un autore collaboratore e fondatore di www.askeygeek.com. Imparare qualcosa di nuovo è sempre stata la sua passione, ASKEYGEEK.com è il risultato della sua passione per la tecnologia e il business. Ha un decennio di esperienza versatile in Business Process Outsourcing, Finanza e Contabilità, Informatica, Eccellenza Operativa e Business Intelligence. Durante il mandato, ha lavorato per organizzazioni come Genpact, Hewlett Packard, M*Modal e Capgemini in vari ruoli e responsabilità. Al di fuori degli affari e della tecnologia, è un appassionato di cinema che trascorre ore insieme guardando e imparando il cinema e anche un regista!

5 risposte

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Congratulazioni!
L'hai fatta,
Non chiudere!
Inserisci la tua possibilità di vincere 100.000 crediti per personaggi UberTTS
Grazie per la vostra visita!

Questo popup non mostrerà sta a te Ancora!!!

Uovo di Pasqua UberTTS da 100K
Share to...