lundi 20 avril 2020

No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''

I am writing a junit test case for an api but getting error as No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''
Wasted more than 2 days not getting any solution checked every possible thing for this. API is token free already. I am writing a junit test case for an api but getting error as No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''
Wasted more than 2 days not getting any solution checked every possible thing for this. API is token free already.

Here is my code

**Encoder Rest Test**

    package com.zoylo.admin.web.rest;

    import static org.junit.Assert.assertEquals;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.mock.web.MockHttpServletResponse;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.MvcResult;
    import org.springframework.test.web.servlet.RequestBuilder;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;



    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = EncoderRest.class)
    @AutoConfigureMockMvc
    public class EncoderRestTest {
        @Autowired
        private MockMvc mockMvc;

        @MockBean
        private EncoderBll encoderBll;

        ObjectMapper mapper = new ObjectMapper();

        @Test
        public void save() throws Exception {
            // mock output
            EncoderMV mv = new EncoderMV();
            mv.setPaymentUrl("www.paytm.com");
            Mockito.when(encoderBll.createPaymentUrl(Mockito.any(EncoderVM.class))).thenReturn(mv);

            EncoderVM vm = new EncoderVM();
            vm.setBookingId("ICICI090");
            vm.setType("Payment");

            String requestBody = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(vm);
            RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/api/encodedurl")
                    .accept(MediaType.APPLICATION_JSON).content(requestBody).contentType(MediaType.APPLICATION_JSON);
            MvcResult result = mockMvc.perform(requestBuilder).andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(HttpStatus.CREATED.value(), response.getStatus());

        }

    }


****EncoderRest****

    @RestController
    @RequestMapping("/api")
    public class EncoderRest {

        private final Logger logger = LoggerFactory.getLogger(this.getClass());

        @Autowired
        private EncoderBll encoderBll;


        @PostMapping("/encodedurl")
        public ResponseEntity<?> generateEncodedUrl(@RequestBody EncoderVM encoderVM) {
            ......
        }
    }


**EncoderBllImpl**

    public class EncoderBllImpl implements EncoderBll{


        @Override
        public EncoderMV createPaymentUrl(EncoderVM encoderVM) {
            EncoderMV encoderMV = null;
            .
            .
            .

            return encoderMV;
        }

    }

**Error**

    No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''  

Aucun commentaire:

Enregistrer un commentaire