Mockito

Unit tests mocks framework.

Example

   1 import static org.mockito.Matchers.any;
   2 import static org.mockito.Matchers.anyObject;
   3 import static org.mockito.Matchers.eq;
   4 import static org.mockito.Mockito.doCallRealMethod;
   5 import static org.mockito.Mockito.mock;
   6 import static org.mockito.Mockito.when;
   7 import org.junit.Assert;
   8 import org.junit.Test;
   9 import org.springframework.mock.env.MockEnvironment;
  10 
  11 public class TestX{
  12     @Test
  13     public void testXyz() {
  14         MockEnvironment env = new MockEnvironment();
  15         env.setProperty("intx", "1");
  16         env.setProperty("intz", "10");
  17         String rsp1="rsp1";
  18         ClassX cx = mock(ClassX.class);
  19         when(cx.getURL(anyObject(), anyObject(), eq("aaaa"))).thenReturn(rsp1);
  20         doCallRealMethod().when(cx).initialize(any(), any());
  21         doCallRealMethod().when(cx).doStuff(any(), any());
  22 
  23         cx.initialize(null, env);
  24         List<String> responsex = cx.doStuff(null, "kkll"));
  25         Assert.assertEquals(123, responsex.size());
  26     }
  27 }

Mockito (last edited 2017-02-14 14:59:12 by localhost)