Source:  https://sites.google.com/a/pintailconsultingllc.com/java/mockito-examples


package examples.mockito;

2

3 import java.math.BigDecimal;

4

5 /**

6  * @author Christopher Bartling, Pintail Consulting LLC

7  * @since Aug 31, 2008 1:18:11 AM

8  */

9 public interface PricingService {

10

11     void setDataAccess(DataAccess dataAccess);

12

13     BigDecimal getPrice(String sku) throws SkuNotFoundException;

14 }


//-----------------------------------------------------------------------------------

1 package examples.mockito;

2

3 import java.math.BigDecimal;

4

5 /**

6  * @author Christopher Bartling, Pintail Consulting LLC

7  * @since Aug 31, 2008 1:18:22 AM

8  */

9 public interface DataAccess {

10    

11     BigDecimal getPriceBySku(String sku);

12 }

//-----------------------------------------------------------------------------------


public class PricingServiceImpl implements PricingService {

10

11     private DataAccess dataAccess;

12

13     public void setDataAccess(DataAccess dataAccess) {

14         this.dataAccess = dataAccess;

15     }

16

17     public BigDecimal getPrice(String sku) throws SkuNotFoundException {

18         BigDecimal price = this.dataAccess.getPriceBySku(sku);

19         if (price == null) {

20             throw new SkuNotFoundException("SKU not found.");

21         }

22         return price;

23     }

24 }


// MOCKITO unit test -----------------------------------------------------------------------------


17 public class PricingServiceTests {

18     private static final String SKU = "3283947";

19     private static final String BAD_SKU = "-9999993434";

20     private PricingService systemUnderTest;

21

22     @MockitoAnnotations.Mock

23     private DataAccess mockedDependency;

24

25     @Before

26     public void doBeforeEachTestCase() {

27         MockitoAnnotations.initMocks(this);

28         systemUnderTest = new PricingServiceImpl();

29         systemUnderTest.setDataAccess(mockedDependency);

30     }

31

32     @Test

33     public void getPrice() throws SkuNotFoundException {

34         stub(mockedDependency.getPriceBySku(SKU)).toReturn(new BigDecimal(100));

35         final BigDecimal price = systemUnderTest.getPrice(SKU);

36         // Verify state

37         assertNotNull(price);

38

39         // Verify behavior

40         verify(mockedDependency).getPriceBySku(SKU);

41     }

42

43     @Test(expected = SkuNotFoundException.class)

44     public void getPriceNonExistentSkuThrowsException() throws SkuNotFoundException {

45         stub(mockedDependency.getPriceBySku(BAD_SKU)).toReturn(null);

46         final BigDecimal price = systemUnderTest.getPrice(BAD_SKU);

47     }

48

49     @Test(expected = RuntimeException.class)

50     public void getPriceDataAccessThrowsRuntimeException() throws SkuNotFoundException {

51         stub(mockedDependency.getPriceBySku(SKU)).toThrow(new RuntimeException("Fatal data access exception."));

52         final BigDecimal price = systemUnderTest.getPrice(SKU);

53     }

54

55 }