Skip to content

Instantly share code, notes, and snippets.

@nielsutrecht
Created April 26, 2017 12:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nielsutrecht/ae75c819fd1be7686d12417a48cf62e3 to your computer and use it in GitHub Desktop.
Save nielsutrecht/ae75c819fd1be7686d12417a48cf62e3 to your computer and use it in GitHub Desktop.
Simple mockito example
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();
}
}
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();
}
}
public class Engine {
private boolean running;
public boolean isRunning() {
return running;
}
public void start() {
running = true;
}
public void stop() {
running = false;
}
}
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