I am currently trying to test a method using Mockito and I am stuck on a certain method.
For example, there are classes called Car and CarSystem.
The car class contains two parameters, Name(String) and its License Plate(String) and their getter/setter methods.
The method I am trying to test using Mockito is a method called, addCar which is in the CarSystem class.
CarSystem class:
private final List<Car> cars= new ArrayList<>();
public Car addCar(String name, String plateNumber) throws IllegalStateException {
Car clash = getCar(plateNumber);
if (null != clash) throw new IllegalStateException("Car with that Number already exists");
Car car = new Car(name, plateNumber);
cars.add(car);
return car;
}
public Car getCar(String match) {
for (Car car: cars) {
if (car.getPlateNumber().equals(match)) return car;
}
return null;
}
What I am trying to test is the two things:
It successfully throws an IllegalStateException exception when there is a car with the plate number that already exists in the car list.
It successfully adds a car to the list.
What I did solely using JUnit is:
@Test
public void testAddCar_ThrowingException() {
try {
CarSystem sys = new CarSystem();
Car car = sys.addCar("1234", "Toyota123");
Car car1= sys.addCar("1234", "Honda123");
Assert.fail();
} catch(IllegalStateException e) {
System.out.println(e);
}
}
@Test
public void testAddCar() {
CarSystem sys = new CarSystem();
Car car = sys.addCar("1234", "Toyota123");
Assert.assertEquals(sys.getCar(1234).getName(), "Toyota123");
}
But I have no idea how to test them using Mockito... Can anyone please help me or give me a hint about this?
P.S) I can freely change the content of the classes for Mockito test.
@Test(expected=IllegalStateException.class). Then you don't need the manualAssert.fail(), or the try-catch.nullin bothgetCar()andaddCar()?