spring - '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()));
}
}
解决方案
我现在正在修复相同的错误...出现错误是因为端点必须以/
.
将您的变量端点从更改v1/customers/
为/v1/customers/
。
推荐阅读
- pm2 - 将每个目录请求指向 PM2 服务器上的 index.html
- html - 如何用 bootstrap 5 制作手风琴桌?
- python - 如何将 pytorch 模型转换为核心 ml 模型?
- java - 谓词问题“无效路径:'generatedAlias1.geometrie'”条件生成器
- python - csv文件中存在的重定向链接的解决方案
- javascript - 构造函数映射需要 Next js 中的“新”
- r - R根据另一列中的值将规则应用于一列中的时间戳以促进过滤
- javascript - 在AngularJS中,检查这3个$http.post后做3个$http.pos是成功的,否则没有post
- qt - 从图片中遮罩,在光标后面裁剪图像
- linux - 将自签名证书插入或更新到 Docker 容器中