8 spring unit-testing annotations
我确实编写了一个提取客户信息的应用程序。
当我在邮递员中运行该应用程序时,它工作正常。
但是当尝试运行一些初始测试但它给出了 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()));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7054 次 |
| 最近记录: |