Created
April 26, 2017 12:25
-
-
Save nielsutrecht/ae75c819fd1be7686d12417a48cf62e3 to your computer and use it in GitHub Desktop.
Simple mockito example
This file contains hidden or 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
public class Car { | |
private Engine engine; | |
private FuelTank fuelTank; | |
public Car(Engine engine, FuelTank fuelTank) { | |
this.engine = engine; | |
this.fuelTank = fuelTank; | |
} | |
public void start() { | |
if(engine.isRunning()) { | |
throw new IllegalStateException("Engine already running"); | |
} | |
if(fuelTank.getFuel() == 0) { | |
throw new IllegalStateException("Can't start: no fuel"); | |
} | |
engine.start(); | |
if(!engine.isRunning()) { | |
throw new IllegalStateException("Started engine but isn't running"); | |
} | |
} | |
public boolean isRunning() { | |
return engine.isRunning(); | |
} | |
} |
This file contains hidden or 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
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.ExpectedException; | |
import org.mockito.Mockito; | |
import static org.junit.Assert.assertEquals; | |
import static org.mockito.Mockito.when; | |
public class CarTest { | |
private Car car; | |
private Engine engine; | |
private FuelTank fuelTank; | |
@Rule | |
public ExpectedException expectedException = ExpectedException.none(); | |
@Before | |
public void setup() { | |
engine = Mockito.mock(Engine.class); | |
fuelTank = Mockito.mock(FuelTank.class); | |
car = new Car(engine, fuelTank); | |
} | |
@Test | |
public void isRunning() { | |
when(engine.isRunning()).thenReturn(true); | |
assertEquals(true, car.isRunning()); | |
when(engine.isRunning()).thenReturn(false); | |
assertEquals(false, car.isRunning()); | |
} | |
@Test | |
public void start() { | |
when(engine.isRunning()).thenReturn(false, true); | |
when(fuelTank.getFuel()).thenReturn(100); | |
car.start(); | |
} | |
@Test | |
public void start_NoFuel() { | |
expectedException.expect(IllegalStateException.class); | |
expectedException.expectMessage("no fuel"); | |
when(engine.isRunning()).thenReturn(false); | |
when(fuelTank.getFuel()).thenReturn(0); | |
car.start(); | |
} | |
@Test | |
public void start_IsRunning() { | |
expectedException.expect(IllegalStateException.class); | |
expectedException.expectMessage("already running"); | |
when(fuelTank.getFuel()).thenReturn(100); | |
when(engine.isRunning()).thenReturn(true); | |
car.start(); | |
} | |
@Test | |
public void start_DidNotStart() { | |
expectedException.expect(IllegalStateException.class); | |
expectedException.expectMessage("Started engine but isn't running"); | |
when(engine.isRunning()).thenReturn(false, false); | |
when(fuelTank.getFuel()).thenReturn(100); | |
car.start(); | |
} | |
} |
This file contains hidden or 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
public class Engine { | |
private boolean running; | |
public boolean isRunning() { | |
return running; | |
} | |
public void start() { | |
running = true; | |
} | |
public void stop() { | |
running = false; | |
} | |
} |
This file contains hidden or 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
public class FuelTank { | |
private int fuel; | |
public int getFuel() { | |
return fuel; | |
} | |
public void setFuel(int fuel) { | |
this.fuel = fuel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment