MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 6 as of 2019-07-08 20:35:48
  • Mockito

Mockito

Unit tests mocks framework.

  • https://site.mockito.org/

  • https://github.com/mockito/mockito

  • https://www.tutorialspoint.com/mockito/mockito_quick_guide.htm

methods purpose

  • mock(): create a mock
  • when(): specify how a mock should behave
  • verify(): to check mocked/stubbed methods were called with given arguments

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         String rsp2="rsp2";
  19         ClassX cx = mock(ClassX.class);
  20         when(cx.getURL(anyObject(), anyObject(), eq("aaaa"))).thenReturn(rsp1).thenReturn(rsp2);
  21         doCallRealMethod().when(cx).initialize(any(), any());
  22         doCallRealMethod().when(cx).doStuff(any(), any());
  23 
  24         cx.initialize(null, env);
  25         List<String> responsex = cx.doStuff(null, "kkll"));
  26         Assert.assertEquals(123, responsex.size());
  27     }
  28 }

BDDMockito

Behavior Driven Development style of writing tests uses //given //when //then comments as fundamental parts of your test methods.

  • https://en.wikipedia.org/wiki/Behavior-driven_development

import static org.mockito.BDDMockito.*;

 Dummy dummy = mock(Dummy.class);
 Foo foo = new Foo(dummy);

 public void doStuff() throws Exception {
   //given
   given(dummy.getObj()).willReturn(new Obj());

   //when
   PlentyStuff ps = shop.getObj();

   //then
   assertThat(ps, containObj());
 }
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01