Using Mockito Objects in Java

Ist step is to download mockitoall.jar from http://code.google.com/p/mockito/

If you are using Eclipse,add it to your build path from Configure Build Path

Below is the sample example using Collections .In the second iteration ,I am testing for the iterator object.

package temp.testmock.tmp;

import static org.testng.AssertJUnit.assertEquals;
import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import static org.mockito.Mockito.*;
import java.util.Iterator;
import org.testng.annotations.Test;

public class NewTest {
@Test
public void f() {

Comparable c=mock(Comparable.class);
when(c.compareTo(“Test”)).thenReturn(1);
assertEquals(1,c.compareTo(“Test”));
}

@Test
public void testthat_iterator_will_return_hello_world(){
//arrange
Iterator i=mock(Iterator.class);
when(i.next()).thenReturn(“Hello”).thenReturn(“World”);
//act
String result=i.next()+” “+i.next();
System.out.println(“Result is “+result);
//assert
assertEquals(“Hello World”, result);
}
}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s