0

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

jaxonjma
  • 177
  • 3
  • 15

1 Answers1

0

You can test REST services by using POSTman (https://www.getpostman.com/) or SOAPUI (https://www.soapui.org/).

However, I suspect the issue is with your javascript function, which you can verify by using the debug function of the chrome developer tools.

  • Hi, thanks for answer, but my code run well, and the application is complitly functional, i am doing unit test over my application, then i need test that function but when i emulate the call i am giving always null attributes :/ – jaxonjma Feb 27 '18 at 20:30