Skip to content

Instantly share code, notes, and snippets.

@greghelton
Created December 26, 2011 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save greghelton/1522044 to your computer and use it in GitHub Desktop.
Save greghelton/1522044 to your computer and use it in GitHub Desktop.
Java: Using Mockito for Unit Testing
package us.home.electronics;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
public class ProgrammableTest {
@Test
public void testBeepBeep() throws Exception {
Programmable clock = new Programmable.AlarmClock();
Programmable.ProgramCoder coder = mock(Programmable.ProgramCoder.class);
// this line 'overrides' the ProgramCoder.getProgramcode() method
when(coder.getProgramCode()).thenReturn("Beep Beep");
clock.programDevice(coder);
assertEquals("Beep Beep", clock.execute());
assertEquals(clock.getCode(), clock.execute());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment