Created
December 26, 2011 20:22
-
-
Save greghelton/1522044 to your computer and use it in GitHub Desktop.
Java: Using Mockito for Unit Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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