Advertisement
Guest User

shit

a guest
Dec 10th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.19 KB | None | 0 0
  1. package carpckg;
  2.  
  3. import static org.junit.jupiter.api.Assertions.*;
  4.  
  5. import org.junit.Rule;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.rules.ExpectedException;
  8.  
  9. import carpckg.MyCar.MyCarEx;
  10. import testpckg.*;
  11.  
  12. class MyCarTest {
  13.    
  14.     /*@Rule
  15.       public final ExpectedException exception = ExpectedException.none();*/
  16.  
  17.     @Test(expected = WrongCapacity_Exception.class)
  18.     void capacityIsLowerThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  19.         //bentley car, tankCapacity is 10(has to start from 20 at least), fuelConsumption is 5
  20.         //fuel lvl is at 0
  21.         //will throw WrongCapacity_Exception
  22.        
  23.         MyCar mc =  new MyCar();  
  24.         MyCar.MyCarEx car = mc.new MyCarEx("10,5,bentley");
  25.         //expected exception for capacity lvl
  26.     //  exception.expect(WrongCapacity_Exception.class);
  27.     }
  28.    
  29.     @Test(expected = WrongCapacity_Exception.class)
  30.     void capacityIsMoreThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  31.         //bentley car, tankCapacity is 81(has to be 80 at max), fuelConsumption is 5
  32.         //fuel lvl is at 0
  33.         //will throw WrongCapacity_Exception
  34.        
  35.         MyCar mc =  new MyCar();  
  36.         MyCar.MyCarEx car = mc.new MyCarEx("81,5,bentley");
  37.         //expected exception for capacity lvl
  38.         //exception.expect(WrongCapacity_Exception.class);
  39.     }
  40.    
  41.     @Test(expected = WrongFuel_Exception.class)
  42.     void consumptionIsLowerThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  43.         //bentley car, tankCapacity is 50(allowed), fuelConsumption is 2(has to be 3 at least)
  44.         //fuel lvl is at 0
  45.         //will throw WrongFuel_Exception
  46.         MyCar mc =  new MyCar();  
  47.         MyCar.MyCarEx car = mc.new MyCarEx("50,2,bentley");
  48.         //expected exception to be thrown
  49.         //exception.expect(WrongFuel_Exception.class);
  50.         //throw new WrongFuel_Exception();
  51.     }
  52.    
  53.     @Test(expected = WrongFuel_Exception.class)
  54.     void consumptionIsMoreThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  55.         //bentley car, tankCapacity is 50(allowed), fuelConsumption is 21(has to be 20 at max)
  56.         //fuel lvl is at 0
  57.         //will throw WrongFuel_Exception
  58.         MyCar mc =  new MyCar();  
  59.         MyCar.MyCarEx car = mc.new MyCarEx("50,21,bentley");
  60.         //expected exception to be thrown
  61.         //exception.expect(WrongFuel_Exception.class);
  62.         //throw new WrongFuel_Exception();
  63.     }
  64.    
  65.     @Test(expected = WrongInput_Exception.class)
  66.     void notNumericConsumption() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  67.         //bentley car, tankCapacity is not numeric, fuelConsumption is 3
  68.         //fuel lvl is at 0
  69.         //will throw WrongInput_Exception
  70.         MyCar mc =  new MyCar();  
  71.         MyCar.MyCarEx car = mc.new MyCarEx("number,3,bentley");
  72.         //expected exception to be thrown
  73.         //exception.expect(WrongInput_Exception.class);
  74.     }
  75.    
  76.     @Test(expected = WrongInput_Exception.class)
  77.     void notNumericFuel() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  78.         //bentley car, tankCapacity is 50(allowed), fuelConsumption is not numeric
  79.         //fuel lvl is at 0
  80.         //will throw WrongInput_Exception
  81.         MyCar mc =  new MyCar();  
  82.         MyCar.MyCarEx car = mc.new MyCarEx("50,number,bentley");
  83.         //expected exception to be thrown
  84.         //exception.expect(WrongInput_Exception.class);
  85.     }
  86.    
  87.     @Test(expected = WrongInput_Exception.class)
  88.     void incorrectCarMaker() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  89.         //tankCapacity is 50(allowed), fuelConsumption is 5,car maker contains not allowed chars
  90.         //fuel lvl is at 0
  91.         //will throw WrongInput_Exception
  92.         MyCar mc =  new MyCar();  
  93.         MyCar.MyCarEx car = mc.new MyCarEx("50,5,bentley3");
  94.         //expected exception to be thrown
  95.         //exception.expect(WrongInput_Exception.class);
  96.     }
  97.    
  98.     @Test
  99.     void checkForCorrectness() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
  100.         //bentley car,tankCapacity is 50(allowed), fuelConsumption is 5(allowed)
  101.         //fuel lvl is at 0
  102.         MyCar mc =  new MyCar();  
  103.         MyCar.MyCarEx car = mc.new MyCarEx("50,5,bentley");
  104.        
  105.         //check if constructor worked properly
  106.         assertEquals(50d,car.getTankCapacity(50d));
  107.         assertEquals(5,car.getFuelConsumption(5));
  108.         assertEquals("OTHER",car.getCarMaker("bentley").toString());
  109.         assertEquals(0d,car.getFuelLevel());
  110.     }
  111.    
  112.     @Test
  113.     void checkMethods() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception, TankIt_Exception, Fuel_Exception {
  114.         //GM car,tankCapacity is 75(allowed), fuelConsumption is 5(allowed)
  115.         //fuel lvl is at 0
  116.         MyCar mc =  new MyCar();  
  117.         MyCar.MyCarEx car = mc.new MyCarEx("75,5,GM");
  118.        
  119.         car.tankIt(75);
  120.         //fuelLvl has to be 75
  121.         assertEquals(75,car.getFuelLevel());
  122.         //start 200 km trip
  123.         car.startTrip(200);
  124.         //uses 5 per 100 km so lvl of fuel has to be 65(75 - 10lt)
  125.         assertEquals(65,car.getFuelLevel());
  126.         //lastTripDistance is 200
  127.         assertEquals(200,car.getLastTripDistance());
  128.         //mileage is 200km
  129.         assertEquals(200,car.getMileage());
  130.        
  131.         car.startTrip(1000);
  132.         //fuel lvl has to be 15 now(uses 5lt per 100,50lt per 1000)
  133.         assertEquals(15,car.getFuelLevel());
  134.         //last trip distance is 1000
  135.         assertEquals(1000,car.getLastTripDistance());
  136.         //mileage is 1200 km now
  137.         assertEquals(1200,car.getMileage());
  138.        
  139.         //use up all fuel
  140.         car.startTrip(300);
  141.         //fuel lvl is 0
  142.         assertEquals(0,car.getFuelLevel());
  143.         //last trip distance is 300
  144.         assertEquals(300,car.getLastTripDistance());
  145.         //mileage is 1500 km
  146.         assertEquals(1500, car.getMileage());
  147.        
  148.         //tank car by 20lt
  149.         car.tankIt(15);
  150.         //fuel lvl has to be 15
  151.         assertEquals(15,car.getFuelLevel());   
  152.     }
  153.    
  154.     @Test(expected = Fuel_Exception.class)
  155.     void testStartTrip() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception, Fuel_Exception {
  156.         //GM car,tankCapacity is 75(allowed), fuelConsumption is 5(allowed)
  157.         //fuel lvl is at 0
  158.         MyCar mc =  new MyCar();  
  159.         MyCar.MyCarEx car = mc.new MyCarEx("75,5,GM");
  160.        
  161.         car.startTrip(100);
  162.         //exception.expect(Fuel_Exception.class);
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement