【问题标题】:No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''在名称为 '' 的 DispatcherServlet 中找不到具有 URI [/api/encodedurl] 的 HTTP 请求的映射
【发布时间】:2020-04-20 10:00:11
【问题描述】:

我正在为 API 编写 JUnit 测试用例,但出现 No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name '' 错误
我浪费了超过 2 天没有得到任何解决方案来检查所有可能的事情。 API 已经是免费的。

这是我的代码

编码器休息测试

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;
    }   
}

错误

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

【问题讨论】:

    标签: java spring testing junit


    【解决方案1】:

    您确定您的休息控制器存在于测试期间启动的应用程序上下文中吗?

    尝试@SpringBootTest(classes = TestConfig.class) 而不是@SpringBootTest(classes = EncoderRest.class)

    @Configuration
    @EnableAutoConfiguration
    public class TestConfig {
    
    }
    

    【讨论】:

    • 还是不行,需要在TestConfig中定义什么吗?
    • 没有。所以,这条消息No mapping found for HTTP request 告诉我们这个控制器不存在于ApplicationContext 中。你的应用是 Spring Boot 吗?
    • 我通过 Initializer 创建了一个简单的 Spring Boot 项目,并按照您的方式实现了一些逻辑。测试运行良好。所以我认为问题出在应用程序配置中,有问题的代码没有任何问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2013-11-19
    • 2014-12-23
    • 2016-08-05
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多