<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <!-- 1.10.10 -->
import static org.mockito.Mockito.*; import static org.junit.Assert.*; import java.util.Iterator; import org.junit.Test; .... @Test public void iterator_will_return_hello_world() { // Iterator i = mock(Iterator.class); when(i.next()).thenReturn("Hello").thenReturn("World"); // String result = i.next()+" "+i.next(); // assertEquals("Hello World", result); }
@Test public void with_arguments() { Comparable c = mock(Comparable.class); when(c.compareTo("Test")).thenReturn(1); assertEquals(1, c.compareTo("Test")); }
@Test public void with_unspecified_arguments() { Comparable c = mock(Comparable.class); when(c.compareTo(anyInt())).thenReturn(-1); assertEquals(-1, c.compareTo(5)); }
@Test(expected=IOException.class) public void OutputStreamWriter_rethrows_an_exception_from_OutputStream() throws IOException { OutputStream mock = mock(OutputStream.class); OutputStreamWriter osw = new OutputStreamWriter(mock); doThrow(new IOException()).when(mock).close(); osw.close(); }
@Test public void OutputStreamWriter_Closes_OutputStream_on_Close() throws IOException { OutputStream mock = mock(OutputStream.class); OutputStreamWriter osw = new OutputStreamWriter(mock); osw.close(); verify(mock).close(); }
@Test public void OutputStreamWriter_Buffers_And_Forwards_To_OutputStream() throws IOException { OutputStream mock = mock(OutputStream.class); OutputStreamWriter osw = new OutputStreamWriter(mock); osw.write('a'); osw.flush(); // , , // // verify(mock).write(new byte[]{'a'}, 0, 1); BaseMatcher<byte[]> arrayStartingWithA = new BaseMatcher<byte[]>() { @Override public void describeTo(Description description) { // } // , - A @Override public boolean matches(Object item) { byte[] actual = (byte[]) item; return actual[0] == 'a'; } }; // , - A, 0 1. verify(mock).write(argThat(arrayStartingWithA), eq(0), eq(1)); }
import static org.mockito.Mockito.*; import static org.junit.Assert.*; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Iterator; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.junit.Test; public class MockitoTests { @Test public void iterator_will_return_hello_world() { // Iterator i = mock(Iterator.class); when(i.next()).thenReturn("Hello").thenReturn("World"); // String result = i.next() + " " + i.next(); // assertEquals("Hello World", result); } @Test public void with_arguments() { Comparable c = mock(Comparable.class); when(c.compareTo("Test")).thenReturn(1); assertEquals(1, c.compareTo("Test")); } @Test public void with_unspecified_arguments() { Comparable c = mock(Comparable.class); when(c.compareTo(anyInt())).thenReturn(-1); assertEquals(-1, c.compareTo(5)); } @Test(expected = IOException.class) public void OutputStreamWriter_rethrows_an_exception_from_OutputStream() throws IOException { OutputStream mock = mock(OutputStream.class); OutputStreamWriter osw = new OutputStreamWriter(mock); doThrow(new IOException()).when(mock).close(); osw.close(); } @Test public void OutputStreamWriter_Closes_OutputStream_on_Close() throws IOException { OutputStream mock = mock(OutputStream.class); OutputStreamWriter osw = new OutputStreamWriter(mock); osw.close(); verify(mock).close(); } @Test public void OutputStreamWriter_Buffers_And_Forwards_To_OutputStream() throws IOException { OutputStream mock = mock(OutputStream.class); OutputStreamWriter osw = new OutputStreamWriter(mock); osw.write('a'); osw.flush(); // , , // // verify(mock).write(new byte[]{'a'},0,1); BaseMatcher<byte[]> arrayStartingWithA = new BaseMatcher<byte[]>() { @Override public void describeTo(Description description) { // } // , - A @Override public boolean matches(Object item) { byte[] actual = (byte[]) item; return actual[0] == 'a'; } }; // , - A, 0 1. verify(mock).write(argThat(arrayStartingWithA), eq(0), eq(1)); } }
Source: https://habr.com/ru/post/243155/
All Articles