Instantly share code, notes, and snippets.

What would you like to do?
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 {
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");
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