Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create a mock with Mockito (in "Java statements" context):</p> <pre><code>${:importStatic('org.mockito.Mockito.mock')}${Type} ${mockName} = mock(${Type}.class); </code></pre> <p>And in "Java type members":</p> <pre><code>${:import(org.mockito.Mock)}@Mock ${Type} ${mockName}; </code></pre> <p>Mock a void method to throw an exception:</p> <pre><code>${:import(org.mockito.invocation.InvocationOnMock,org.mockito.stubbing.Answer)} doThrow(${RuntimeException}.class).when(${mock:localVar}).${mockedMethod}(${args}); </code></pre> <p>Mock a void method to do something:</p> <pre><code>${:import(org.mockito.invocation.InvocationOnMock,org.mockito.stubbing.Answer)}doAnswer(new Answer&lt;Object&gt;() { public Object answer(InvocationOnMock invocation) throws Throwable { Object arg1 = invocation.getArguments()[0]; return null; } }).when(${mock:localVar}).${mockedMethod}(${args}); </code></pre> <p>Verify mocked method called exactly once:</p> <pre><code>${:importStatic(org.mockito.Mockito.verify,org.mockito.Mockito.times)} verify(${mock:localVar}, times(1)).${mockMethod}(${args}); </code></pre> <p>Verify mocked method is never invoked:</p> <pre><code>${:importStatic(org.mockito.Mockito.verify,org.mockito.Mockito.never)}verify(${mock:localVar}, never()).${mockMethod}(${args}); </code></pre> <p>New linked list using Google Guava (and similar for hashset and hashmap):</p> <pre><code>${import:import(java.util.List,com.google.common.collect.Lists)}List&lt;${T}&gt; ${newName} = Lists.newLinkedList(); </code></pre> <p>Also I use a huge template that generates a Test class. Here is a shortened fragment of it that everyone interested should customize:</p> <pre><code>package ${enclosing_package}; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.Matchers.*; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import org.mockito.Mockito; import org.slf4j.Logger; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.junit.runner.RunWith; // TODO autogenerated test stub @RunWith(MockitoJUnitRunner.class) public class ${primary_type_name} { @InjectMocks protected ${testedType} ${testedInstance}; ${cursor} @Mock protected Logger logger; @Before public void setup() throws Exception { } @Test public void shouldXXX() throws Exception { // given // when // TODO autogenerated method stub // then fail("Not implemented."); } } // Here goes mockito+junit cheetsheet </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload