0

I'm using JUnit to test the routes of an application. The problem is that I am making a PUT request to a route, which I think should return a Status 200, but in reality it returns 404. I have already tried passing a string also and changing the PathVariable to String, using Long.parseLong() but I still get errors. I would like to know why it returns 404 and what am I doing wrong, when in my database exists the Persona with Id 1 and Grupo with Id 1. By the way, I am using JHipster and I am doing these tests in the PersonaResourceIT that is already defined. Thank you very much, here is the code:

PersonaResource:

@PutMapping("/personas/{id}/grupo/{idGrupo}")
public ResponseEntity<Void> vincularGrupo(@PathVariable Long id, @PathVariable Long idGrupo) {
    log.debug("REST request to vincularGrupo : {}", id);
    Optional<Persona> persona = personaRepository.findById(id);
    if (persona.isPresent()) {
        Persona persona2 = persona.get();
        Optional<Grupo> grupo = grupoRepository.findById(idGrupo);
        if (grupo.isPresent()) {
            Grupo grupo2 = grupo.get();
            persona2.setGrupo(grupo2);
            personaRepository.save(persona2);
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    } else {
        return ResponseEntity.notFound().build();
    }
}

PersonaResourceIT:

    @Test
    @Transactional
    void asociarPersonaGrupoSuccess() throws Exception {
        // Long id = (long) 1;
        // Long idGrupo = (long) 1;
        String id = "1";
        String idGrupo = "1";
        this.restPersonaMockMvc.perform(put("/api/personas/{id}/grupo/{idGrupo}", id, idGrupo))
                                .andExpect(status().isOk())
                                .andReturn();
    }

I also tried using Long id = 1L but it did not work. I keep getting a 404 status.

2
  • First you must determine which entity is missing from your database, it should be easy under debugger. Then you should review how and when you create these entities, is it before test execution, inside same transaction, using liquibase? Dec 14, 2021 at 10:30
  • Yes, it is using Liquibase. The problem was that in my application.yml I was not indicating that I wanted to use the csv files for the integration tests, as I indicated to geco17. Thank you very much Gael. Every time I learn something new from JHipster. Dec 14, 2021 at 15:53

1 Answer 1

0

I'd suggest modifying your code with a bit of a refactor.

Move the bulk of the method into a service like so

public int findAndSave(long id, long idGrupo) {
    Optional<Persona> persona = personaRepository.findById(id);
    if (!persona.isPresent()) {
        return 1;
    }
    Optional<Grupo> grupo = grupoRepository.findById(idGrupo);
    if (!grupo.isPresent()) {
        return 2;
    }
    Persona persona2 = persona.get();
    Grupo grupo2 = grupo.get();
    persona2.setGrupo(grupo2);
    personaRepository.save(persona2);
    return 0;
}

Then the caller has

@PutMapping("/personas/{id}/grupo/{idGrupo}")
public ResponseEntity<Void> vincularGrupo(@PathVariable Long id, @PathVariable Long idGrupo) {
    log.debug("REST request to vincularGrupo : {}", id);
    int result = vincularService.findAndSave(id, idGrupo);
    if (result == 0) {
        return ResponseEntity.ok().build();
    }
    // handle 1, 2 or other error codes however you need to
    return ResponseEntity.notFound().build();
}

And the test is simplified: you can inject a mock into it for the vincularService and test that you're hitting the endpoint (i.e. it's not giving you a not found because the url is wrong), and you can test the logic inside vincularService independently of how it's called (rest or some other way).

3
  • Thank you very much geco17. I just created the service but I still get a 404. Is it because I have to pass it as a string instead of Long 1L? However it seems strange to me because other endpoints are working fine with Long. It is this one and 2 others that are having problems. In my database these ids exist so I don't understand why it returns a 404. And the routes are specified correctly, it is very strange. Also, when I test it with Postman it works fine. Dec 13, 2021 at 17:56
  • I'm not sure, could it have to do with the junit test then? The url is defined there as /api/personas/{id}/grupo/{idGrupo} but it's defined in the controller without the /api prefix
    – geco17
    Dec 14, 2021 at 7:54
  • Nono. Only that in my PersonaResource I have the following annotation: @RequestMapping("/api") public class PersonaResource { The problem was that when using JHipster, I have to indicate in the application.yml that I want to use the csv data faker to be able to use in the integration tests. Like this: liquibase: contexts: prod, faker Dec 14, 2021 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.