我确实写了一个应用程序来提取客户信息。
当我在Postman中运行应用程序时,它工作正常。
但是当尝试运行一些初始测试时,它会给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()));
}
}
2条答案
按热度按时间7tofc5zh1#
我现在正在修复同样的错误。..出现错误,因为endpoint必须以
/
开头。将您的变量endpoint从
v1/customers/
更改为/v1/customers/
。gmol16392#
如果你正在使用一个模式,请确保它以“/”开头(模式本身,而不是它被解析成的)。