Skip to content

Instantly share code, notes, and snippets.

@rchatley
Last active December 19, 2015 14:59
Show Gist options
  • Save rchatley/5973408 to your computer and use it in GitHub Desktop.
Save rchatley/5973408 to your computer and use it in GitHub Desktop.
CameraControl Exercise
package com.develogical.camera;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(value = JMock.class)
public class TestCamera {
Mockery context = new Mockery();
Sensor sensor = context.mock(Sensor.class);
MemoryCard memoryCard = context.mock(MemoryCard.class);
Camera camera = new Camera(sensor, memoryCard);
static final byte[] PHOTO_DATA = new byte[10];
@Test
public void switchingTheCameraOnPowersUpTheSensor() {
context.checking(new Expectations() {{
oneOf(sensor).powerUp();
}});
camera.powerOn();
}
@Test
public void switchingTheCameraOffPowersDownTheSensor() {
context.checking(new Expectations() {{
ignoring(sensor).powerUp();
oneOf(sensor).powerDown();
}});
camera.powerOn();
camera.powerOff();
}
@Test
public void pressingTheShutterWithThePowerOnCopiesDataFromTheSensorToTheMemoryCard() {
context.checking(new Expectations() {{
ignoring(sensor).powerUp();
oneOf(sensor).readData(); will(returnValue(PHOTO_DATA));
oneOf(memoryCard).write(PHOTO_DATA);
}});
camera.powerOn();
camera.pressShutter();
}
@Test
public void pressingTheShutterWithThePowerOffDoesNothing() {
context.checking(new Expectations() {{
never(sensor);
never(memoryCard);
}});
camera.pressShutter();
}
@Test
public void doesNotPowerDownTheSensorWhileDataIsBeingWritten() {
context.checking(new Expectations() {{
ignoring(sensor).powerUp();
allowing(sensor).readData(); will(returnValue(PHOTO_DATA));
allowing(memoryCard).write(PHOTO_DATA);
}});
camera.powerOn();
camera.pressShutter();
camera.powerOff();
}
@Test
public void powersDownTheSensorOnceWritingIsComplete() {
context.checking(new Expectations() {{
ignoring(sensor).powerUp();
allowing(sensor).readData(); will(returnValue(PHOTO_DATA));
allowing(memoryCard).write(PHOTO_DATA);
}});
camera.powerOn();
camera.pressShutter();
camera.powerOff();
context.checking(new Expectations() {{
oneOf(sensor).powerDown();
}});
camera.writeComplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment