Created
October 7, 2010 13:28
-
-
Save MoriTanosuke/615103 to your computer and use it in GitHub Desktop.
Simple testcases to demonstrate JMockit Expectations
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 de.kopis.example; | |
public class Car { | |
protected Engine engine; | |
public Car(Engine e) { | |
engine = e; | |
} | |
public void replaceEngine(Engine e) { | |
engine = e; | |
} | |
} |
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 de.kopis.example; | |
public interface Engine {} | |
class GasolineEngine implements Engine {} | |
class DieselEngine implements Engine {} |
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 de.kopis.example; | |
public interface Garage { | |
public Car repairEngine(Car car); | |
} | |
abstract class AbstractGarage implements Garage { | |
public abstract Car repairEngine(Car car); | |
} | |
class CheapGarage extends AbstractGarage { | |
public Car repairEngine(Car car) { | |
replaceEngineWithNewInstance(car); | |
return car; | |
} | |
private void replaceEngineWithNewInstance(Car car) { | |
try { | |
Engine newEngine = EngineFactory.instantiate(car.engine); | |
car.replaceEngine(newEngine); | |
} catch ( InstantiationException e ) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch ( IllegalAccessException e ) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
class EngineFactory { | |
public EngineFactory() {} | |
public static Engine instantiate(Engine engine) throws InstantiationException, IllegalAccessException { | |
Class<? extends Engine> engineClass = engine.getClass(); | |
Engine newEngine = engineClass.newInstance(); | |
return newEngine; | |
} | |
} |
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 de.kopis.example; | |
import static org.junit.Assert.*; | |
import mockit.Expectations; | |
import org.junit.Test; | |
public class GarageTest { | |
@Test | |
public void testRepair() { | |
Garage g = new CheapGarage(); | |
Engine expectedEngine = new GasolineEngine(); | |
Car expectedCar = new Car(expectedEngine); | |
Car actualCar = g.repairEngine(expectedCar); | |
assertSame(expectedCar, actualCar); | |
assertNotSame(expectedCar.engine, expectedEngine); | |
} | |
@Test | |
public void testRepairWithExpectationEngineFactory() throws InstantiationException, IllegalAccessException { | |
final Engine expectedEngine = new DieselEngine(); | |
final Engine actualEngine = new GasolineEngine(); | |
final Garage g = new CheapGarage(); | |
final Car expectedCar = new Car(actualEngine); | |
// here it is still the Engine built into the car | |
assertSame(expectedCar.engine, actualEngine); | |
new Expectations() { | |
EngineFactory mock; | |
{ | |
mock.instantiate(actualEngine); returns(new DieselEngine()); | |
} | |
}; | |
Car actualCar = g.repairEngine(expectedCar); | |
assertSame(expectedCar, actualCar); | |
// normally this will be a GasolineEngine, because that was built into the car | |
// but because of the mocked EngineFactory, we get a DieselEngine instead | |
assertTrue(expectedCar.engine instanceof DieselEngine); | |
assertNotSame(expectedCar.engine, expectedEngine); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment