Loading
JMockito Unit Testcase

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  JMockito Unit Testcase Example ?


Ans)


Junit has some limitaions , for instance we could not test the Private and Static methods
with JUNIT, this issue is addressed by some other Unit Testing APIs such EasyMock, Mockito
and all.

Here is the sample Mocktio Unit Test case.

Step 1 :
Util class which has a Static Method.

class TestUtil {
 
 public static printName (Srting name ) {
  
  System.out.println("Your Name is " + name);
 }
}

Step 2 :
 Your service that to be tested.

class YourService {
 public YourService() {
  
 }
 public void printName (Srting Name) {
  TestUtil.printName(Name);
 }
}

Step 3:
Your Test Case

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.whenNew;


import org.apache.tools.ant.taskdefs.Length.When;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ TestUtil.class})

public class TestBroadBandService {
 
 @Before
 public void createData() {
     PowerMockito.mockStatic(TestUtil.class);
  when (TestUtil.printName("") .thenReturn("newName");
 }

   @Test
 public void testInvokeWebService() {
  YourService YourService = new YourService();
  yourService.printName("YourName");
 }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

JMockito Unit Testcase


JMockito Unit Testcase

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free