首页 > 解决方案 > 'url' 应该以路径开头或者是完整的 HTTP URL:

问题描述

我确实编写了一个提取客户信息的应用程序。

当我在邮递员中运行应用程序时,它工作正常。

但是当尝试运行一些初始测试但它给出了bean错误时,

具有相同注释的完全相同的配置在另一个组件中可以正常工作。

提前致谢

'url' 应该以路径开头或者是完整的 HTTP URL:v1/customers/2503427 java.lang.IllegalArgumentException: 'url' 应该以路径开头或者是完整的 HTTP URL:v1/customers/2503427

package az.iba.ms.customer.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
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.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class CustomerControllerTest {

    String endpoint = "v1/customers/";
    String cifs = "2503427";

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private CustomerController customerController;

    @Test
    public void controllerInitializedCorrectly() {
        Assertions.assertThat(customerController).isNotNull();
    }

    @Test
    public void whenValidInput_providedToCustomerQueryThenReturns200() throws Exception {

        mockMvc.perform(get(endpoint + cifs)
                .contentType("application/json"))
                .andExpect(MockMvcResultMatchers.status().is(HttpStatus.OK.value()));
    }

    @Test
    void whenValidNotInput_providedToCustomerQueryThenReturns400() throws Exception {

        mockMvc.perform(get(endpoint)
                .contentType("application/json"))
                .andExpect(MockMvcResultMatchers.status().is(HttpStatus.BAD_REQUEST.value()));
    }

    @Test
    void whenValidNotMethod_providedToCustomerQueryThenReturns405() throws Exception {

        mockMvc.perform(post(endpoint + cifs)
                .contentType("application/json"))
                .andExpect(MockMvcResultMatchers.status().is(HttpStatus.METHOD_NOT_ALLOWED.value()));
    }

}

标签: springunit-testingannotations

解决方案


我现在正在修复相同的错误...出现错误是因为端点必须以/.

将您的变量端点从更改v1/customers//v1/customers/


推荐阅读