jeudi 15 avril 2021

AssertionFailedError: i dont understand what is wrong in my code

I'm trying to run a test for a method but i always get the same error.

This is my test class:

private Car car;

@Autowired
protected CarService carService;

@Mock
private CarRepository carRepository;

@BeforeEach
void setUp() {
    car = new Car();
    car.setId(4L);
    car.setName("test1");
    car.setUrlCar("https://carconfigurator.ferrari.com/assets/cars/portofinom/packages/default/car-ferrari-portofino-m_splash.jpg");
    car.setColor("red");
    carRepository.save(car);
    }

@Test
void getCarById() throws Exception {
    Car res = this.carService.getCarById(4L);
    Assertions.assertTrue(res != null);
}

The CarService class is:

@Autowired
private carRepository carRepository;

public Iterable<Car> getAllCars(){
    return carRepository.findAll();
}
public Car getCarById(Long id){
    return carRepository.findById(id).orElse(null);
}

The trace is:

org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
    at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39)

The trace points to the line Assertions.assertTrue(res != null); and i don't know how to solve the error.

Aucun commentaire:

Enregistrer un commentaire