#Unit Testing Problem Spring

3 messages · Page 1 of 1 (latest)

half grove
#

Hello, I have problem with database or smth like that with testing springboot
I get error that user is not found but this user should exists with specified id
On normal api testing with postman it works correctly

    @Test
    final void testAddRental() {
        User user = new User();
        user.setId(1);
        user.setName("as");
        user.setSurname("sa");
        user.setEmail("@");
        userRepository.save(user);
        Worker worker = new Worker();
        worker.setId(1);
        worker.setName("as");
        worker.setSurname("sa");
        worker.setEmail("@");
        workerRepository.save(worker);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(1);
        vehicle.setMake("sa");
        vehicle.setModel("as");
        vehicle.setYear(1990);
        vehicle.setPlate("GWE 1111");
        vehicle.setCategory("sa");

        vehicleRepository.save(vehicle);

        when(userRepository.findById(1)).thenReturn(Optional.of(user));
        when(vehicleRepository.findById(1)).thenReturn(Optional.of(vehicle));
        when(workerRepository.findById(1)).thenReturn(Optional.of(worker));

        Rental res = rentalService.CreateRental(1,1,1,1683703611000L,1715326005000L);
        assertNotNull(res);
    }

Tysm for help!

quasi lodgeBOT
#

This post has been reserved for your question.

Hey @half grove! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.