Guest User

Untitled

a guest
Jun 24th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package de.kopis.example;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import mockit.Expectations;
  6.  
  7. import org.junit.Test;
  8.  
  9. public class GarageTest {
  10. @Test
  11. public void testRepair() {
  12. Garage g = new CheapGarage();
  13. Engine expectedEngine = new GasolineEngine();
  14. Car expectedCar = new Car(expectedEngine);
  15. Car actualCar = g.repairEngine(expectedCar);
  16. assertSame(expectedCar, actualCar);
  17. assertNotSame(expectedCar.engine, expectedEngine);
  18. }
  19.  
  20. @Test
  21. public void testRepairWithExpectationEngineFactory() throws InstantiationException, IllegalAccessException {
  22. final Engine expectedEngine = new DieselEngine();
  23. final Engine actualEngine = new GasolineEngine();
  24. final Garage g = new CheapGarage();
  25. final Car expectedCar = new Car(actualEngine);
  26. // here it is still the Engine built into the car
  27. assertSame(expectedCar.engine, actualEngine);
  28.  
  29. new Expectations() {
  30. EngineFactory mock;
  31. {
  32. mock.instantiate(actualEngine); returns(new DieselEngine());
  33. }
  34. };
  35.  
  36. Car actualCar = g.repairEngine(expectedCar);
  37. assertSame(expectedCar, actualCar);
  38. // normally this will be a GasolineEngine, because that was built into the car
  39. // but because of the mocked EngineFactory, we get a DieselEngine instead
  40. assertTrue(expectedCar.engine instanceof DieselEngine);
  41. assertNotSame(expectedCar.engine, expectedEngine);
  42. }
  43. }
Add Comment
Please, Sign In to add comment