Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package carpckg;
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.Rule;
- import org.junit.jupiter.api.Test;
- import org.junit.rules.ExpectedException;
- import carpckg.MyCar.MyCarEx;
- import testpckg.*;
- class MyCarTest {
- /*@Rule
- public final ExpectedException exception = ExpectedException.none();*/
- @Test(expected = WrongCapacity_Exception.class)
- void capacityIsLowerThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car, tankCapacity is 10(has to start from 20 at least), fuelConsumption is 5
- //fuel lvl is at 0
- //will throw WrongCapacity_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("10,5,bentley");
- //expected exception for capacity lvl
- // exception.expect(WrongCapacity_Exception.class);
- }
- @Test(expected = WrongCapacity_Exception.class)
- void capacityIsMoreThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car, tankCapacity is 81(has to be 80 at max), fuelConsumption is 5
- //fuel lvl is at 0
- //will throw WrongCapacity_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("81,5,bentley");
- //expected exception for capacity lvl
- //exception.expect(WrongCapacity_Exception.class);
- }
- @Test(expected = WrongFuel_Exception.class)
- void consumptionIsLowerThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car, tankCapacity is 50(allowed), fuelConsumption is 2(has to be 3 at least)
- //fuel lvl is at 0
- //will throw WrongFuel_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("50,2,bentley");
- //expected exception to be thrown
- //exception.expect(WrongFuel_Exception.class);
- //throw new WrongFuel_Exception();
- }
- @Test(expected = WrongFuel_Exception.class)
- void consumptionIsMoreThanExpected() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car, tankCapacity is 50(allowed), fuelConsumption is 21(has to be 20 at max)
- //fuel lvl is at 0
- //will throw WrongFuel_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("50,21,bentley");
- //expected exception to be thrown
- //exception.expect(WrongFuel_Exception.class);
- //throw new WrongFuel_Exception();
- }
- @Test(expected = WrongInput_Exception.class)
- void notNumericConsumption() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car, tankCapacity is not numeric, fuelConsumption is 3
- //fuel lvl is at 0
- //will throw WrongInput_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("number,3,bentley");
- //expected exception to be thrown
- //exception.expect(WrongInput_Exception.class);
- }
- @Test(expected = WrongInput_Exception.class)
- void notNumericFuel() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car, tankCapacity is 50(allowed), fuelConsumption is not numeric
- //fuel lvl is at 0
- //will throw WrongInput_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("50,number,bentley");
- //expected exception to be thrown
- //exception.expect(WrongInput_Exception.class);
- }
- @Test(expected = WrongInput_Exception.class)
- void incorrectCarMaker() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //tankCapacity is 50(allowed), fuelConsumption is 5,car maker contains not allowed chars
- //fuel lvl is at 0
- //will throw WrongInput_Exception
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("50,5,bentley3");
- //expected exception to be thrown
- //exception.expect(WrongInput_Exception.class);
- }
- @Test
- void checkForCorrectness() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception {
- //bentley car,tankCapacity is 50(allowed), fuelConsumption is 5(allowed)
- //fuel lvl is at 0
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("50,5,bentley");
- //check if constructor worked properly
- assertEquals(50d,car.getTankCapacity(50d));
- assertEquals(5,car.getFuelConsumption(5));
- assertEquals("OTHER",car.getCarMaker("bentley").toString());
- assertEquals(0d,car.getFuelLevel());
- }
- @Test
- void checkMethods() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception, TankIt_Exception, Fuel_Exception {
- //GM car,tankCapacity is 75(allowed), fuelConsumption is 5(allowed)
- //fuel lvl is at 0
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("75,5,GM");
- car.tankIt(75);
- //fuelLvl has to be 75
- assertEquals(75,car.getFuelLevel());
- //start 200 km trip
- car.startTrip(200);
- //uses 5 per 100 km so lvl of fuel has to be 65(75 - 10lt)
- assertEquals(65,car.getFuelLevel());
- //lastTripDistance is 200
- assertEquals(200,car.getLastTripDistance());
- //mileage is 200km
- assertEquals(200,car.getMileage());
- car.startTrip(1000);
- //fuel lvl has to be 15 now(uses 5lt per 100,50lt per 1000)
- assertEquals(15,car.getFuelLevel());
- //last trip distance is 1000
- assertEquals(1000,car.getLastTripDistance());
- //mileage is 1200 km now
- assertEquals(1200,car.getMileage());
- //use up all fuel
- car.startTrip(300);
- //fuel lvl is 0
- assertEquals(0,car.getFuelLevel());
- //last trip distance is 300
- assertEquals(300,car.getLastTripDistance());
- //mileage is 1500 km
- assertEquals(1500, car.getMileage());
- //tank car by 20lt
- car.tankIt(15);
- //fuel lvl has to be 15
- assertEquals(15,car.getFuelLevel());
- }
- @Test(expected = Fuel_Exception.class)
- void testStartTrip() throws WrongLength_Exception, WrongInput_Exception, WrongFuel_Exception, WrongCapacity_Exception, Fuel_Exception {
- //GM car,tankCapacity is 75(allowed), fuelConsumption is 5(allowed)
- //fuel lvl is at 0
- MyCar mc = new MyCar();
- MyCar.MyCarEx car = mc.new MyCarEx("75,5,GM");
- car.startTrip(100);
- //exception.expect(Fuel_Exception.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement