Rodrigo Batista
Rodrigo Batista

Reputation: 383

Spring Boot rest controller test

I'm writing the tests for my controller and I've already been able to find the search endpoint. I tried the insert now, but without success.

My Serivce :

public LegalPerson insert(LegalPerson legalPerson) {
        legalPerson.setId(null);
        try {
            legalPerson = repository.save(legalPerson);
        } catch (DataIntegrityViolationException error) {
            throw new DataIntegrity("Error something***", error);
        }
        return legalPerson;
    }

 public LegalPerson modelToEntity(LegalPersonModel personModel) {
        return LegalPerson.builder()
                .active(personModel.getActive())
                .companyId(personModel.getCompanyId())
                .tradeName(personModel.getTradeName())
                .companyName(personModel.getCompanyName())
                .email(personModel.getEmail())
                .cnpj(personModel.getCnpj())
                .stateRegistration(personModel.getStateRegistration())
                .municipalRegistration(personModel.getMunicipalRegistration())
                .openingDate(personModel.getOpeningDate())
                .address(personModel.getAddress())
                .companyType(personModel.getCompanyType())
                .subsidiaries(personModel.getSubsidiaries())
                .phones(personModel.getPhones())
                .build();
    }

My rest controller

@RestController
@RequestMapping(value = "/api/clients/lp")
public class LegalPersonResource {

/**other methods ***/
 @PostMapping
    public ResponseEntity<Void> insert(@Valid @RequestBody LegalPersonModel legalPersonModel) {
        var legalPerson = service.modelToEntity(legalPersonModel);
        legalPerson = service.insert(legalPerson);
        return ResponseEntity.created(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
                .buildAndExpand(legalPerson.getId()).toUri()).build();
    }

And my test

@Test
void insert() throws Exception {
 var mockLPM = new LegalPersonModel(true, 1L, "mockTrade", "mockCompanyName", "mockTest@com", "63.236.885/0001-42", "123455", "1234566", localDate, 1L, CompanyEnum.HEADOFFICE,subsidiaries, phones);
   var objason = new ObjectMapper();
   objason.writeValueAsString(mockLPM);
   var test = mvc.perform(MockMvcRequestBuilders.post("/api/clients/lp").contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content(String.valueOf(objason))).andReturn();

 int status = test.getResponse().getStatus();
 Assertions.assertEquals(201, status);
}

always returning status 400. What am I doing wrong?

==== UPDATE:====

It worked in parts.

It worked only with the ObjectMapper, and I have to change LocalDate to null, as it tarnsforms the localDate in a huge string:

"openingDate": {"year": 1955, "month": "OCTOBER", "monthValue": 10, "dayOfMonth": 25, "chronology": {"calendarType": "iso8601", "id" "}," era ":" CE "," dayOfYear ": 298," dayOfWeek ":" TUESDAY "," leapYear ": false}

And actually my json should look like this: "openingDate": "2019-04-02" for example.

So when I change localDate by null, it passes that part, but a NullPointerException in that part of my service here:

var legalPerson = service.modelToEntity (legalPersonModel);

it recognizes the legalPersonModel, which is what we just passed with the ObjectMapper, however the legalPerson always stays as null, it is not converted.

I tried some things without success. What should I do?

=== UPDATE 2 ===

If in the rest controller, has:

 return ResponseEntity.created (ServletUriComponentsBuilder.fromCurrentRequest (). path ("/ {id}")
                .buildAndExpand (legalPerson.getId ()). toUri ()). build ();

will return null.

But if I switch to

return ResponseEntity.status (201) .build ();

I have a green test.

Is there any way to solve this?

Upvotes: 0

Views: 139

Answers (1)

Misantorp
Misantorp

Reputation: 2821

You will get a 400 Bad Request status whenever the payload you're posting is not a valid type - in your example you should post a payload that can be deserialized into a LegalPersonModel object.

You can either post the payload directly as a json string

@Test
void insertAsJsonString() throws Exception {
    var mockLPM = new LegalPersonModel(true,
            1L,
            "mockTrade",
            "mockCompanyName",
            "mockTest@com",
            "63.236.885/0001-42",
            "123455",
            "1234566",
            localDate,
            1L,
            CompanyEnum.HEADOFFICE,
            subsidiaries,
            phones);

    var om = new ObjectMapper();

    var test = mvc.perform(
            MockMvcRequestBuilders
                    .post("/api/clients/lp")
                    .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                    .content(om.writeValueAsString(mockLPM))
            .andReturn();

    int status = test.getResponse().getStatus();
    Assertions.assertEquals(201, status);
}

To serialize LocalDate into yyyy-MM-dd format, try configuring the ObjectMapper like this

ObjectMapper om = new ObjectMapper();
om.registerModule(new JavaTimeModule());
om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

You might need to import the jackson-datatype-jsr310. Using maven import it like so

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.9.8</version>
</dependency>

If the var legalPerson = service.modelToEntity (legalPersonModel); always results in legalPerson being null even if the LocalDate is serialized correctly you'll need to update the question with the LegalPerson class including the builder pattern.


I am unable to reproduce the "Update 2" problem.

Given this end point

@PostMapping
public ResponseEntity<Void> insert(@Valid @RequestBody LegalPersonModel legalPersonModel) {
    var legalPerson = service.modelToEntity(legalPersonModel);
    legalPerson = service.insert(legalPerson);
    return ResponseEntity.created(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(legalPerson.getId()).toUri()).build();
}

Will return the following (using the command line tool curl)

$ curl -X POST -d @legalPersonModelMock 'http://localhost:8080/api/clients/lp'
< HTTP/1.1 201 
< Location: http://localhost:8080/api/clients/lp/1
< Content-Length: 0

Content of the legalPersonModelMock file is an json representation of var mockLPM = new LegalPersonModel(...).

Your code for service.insert(...) sets legalPerson.setId(null);, are you sure that's not messing with the response?

Upvotes: 1

Related Questions