i have this method in my rest controller (I can not use @RequestBody or @RequestParam for a couple of complicated reasons, so I need it this way):
@PostMapping(value="/crear")
public Resultado<Candidato> crear(Candidato candidato) throws IOException {
...Just code...
return resultado;
}
and i am trying to test it with MockMVC, but my "Candidato" is always Null in every attribute, this is my test until now:
@Test
public void crear() throws Exception {
String nombre = "Candidato Test2";
candidato.setNombre(nombre);
gsonBuilder.setDateFormat(EntitiesBuilderTestUtil.DATE_FORMAT);
when(procesoSeleccionService.findByToken(anyString())).thenReturn(EntitiesBuilderTestUtil.obtenerProcesoSeleccion());
when(candidatoValidator.validate(any(Candidato.class))).thenReturn(new Notificacion());
this.mockMvc.perform(post("/api/candidato/crear")
.content(gsonBuilder.create().toJson(candidato).getBytes())
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andExpect(status().isOk())
.andExpect(content().contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8));
}
In my ajax request i do the next:
crearCandidato: function(){
let formulario = app.funciones.obtenerFormulario();
$.ajax({
url:baseURL+"/api/candidato/crear",
type:"POST",
contentType: false,
processData: false,
data:formulario,
dataType:"JSON"
}).done(function(data){
... do something
}).fail(function(e){
... do something
});
}
my obtenerFormulario() function:
obtenerFormulario: function(){
let formdata = new FormData();
formdata.append("nombre",app.funciones.removerTagsHtml(app.$inpNombre.val()));
...more code
return formdata;
},
How could I test the method correctly? Thanks for your help :D
-->Update I already have other tests running correctly and as for the "Candidato", it is created correctly but the jackson creates it with null attributes