public
Last active

Java: Using Mockito for Unit Testing

  • Download Gist
ProgrammableTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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());
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.