Problem performing a test with POST method

1

I'm trying to run a unit test of the post method, but the system returns the expected status but was < 404 & gt ;. If I use postaman with Json that I'm creating in Java, it returns the status 200 (OK)

Test Class

@RunWith(SpringRunner.class)

@WebMvcTest(ComprasController.class)

public class ComprasControllerTest {

    @Autowired
    private MockMvc mockMvc;    

    private WebApplicationContext wac;

    public void setup () {
        DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
        this.mockMvc = builder.build();
    }


    @Test
    public void testUserController () throws Exception {


                this.mockMvc.perform(MockMvcRequestBuilders.post("/compras/efetivarCompras")
                        .contentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8)
                        .content(Json.JsonPost)
                        ).andExpect(status().isOk());

    }

Test class compilation result

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /compras/efetivarCompras
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}
             Body = { "carrinho":{"idCarrinho":"1"}, "dadosPagamento":{ "cartao": { "numeroCartao": 12345678, "nomeCliente": "Guilherme Antunes", "validade": "02/22", "cvv":"666"}, "valorTotalCompra":20.00 }} 
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
2018-05-15 11:14:56.361  INFO 3834 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@48e1f6c7: startup date [Tue May 15 11:14:52 BRT 2018]; root of context hierarchy

JSON file that is called in the test class

{
"carrinho":{"idCarrinho":"1"},

    "dadosPagamento":{
    "cartao": {
        "numeroCartao": 12345678,
        "nomeCliente": "Guilherme Antunes",
        "validade": "02/22",
        "cvv":"666"},
        "valorTotalCompra":20.00
}
  }

Controller

@Controller
public class ComprasController {


    @Autowired ComprasRepository comprasRepository;


    @RequestMapping (path="/compras/efetivarCompras", method=RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public Compras efetivarCompras(@RequestBody EntradaPagamento entradaPagamento) {

        Compras compras = new Compras();
        Cliente cliente = new Cliente();

        cliente.setIdCliente("1");



        compras.setIdCarrinho(entradaPagamento.getCarrinho().getIdCarrinho());
        compras.setIdCliente(cliente.getIdCliente());


        try {

            DadosPagamento dadosPagamento = new DadosPagamento();

            dadosPagamento.setCartao(entradaPagamento.getDadosPagamento().getCartao());
            dadosPagamento.setValorTotalCompra(entradaPagamento.getDadosPagamento().getValorTotalCompra());

            RestTemplate rt = new RestTemplate();

            String retorno = "";

            rt.postForObject("http://localhost/ECommerce/autorizarCompra", dadosPagamento, Object.class);

            compras = comprasRepository.save(compras);


        }catch(Exception e) {
            e.printStackTrace();
        }



        return compras;

    }

}
    
asked by anonymous 15.05.2018 / 16:27

0 answers