如何使用 spring 验证 junit 中的方法调用 typsafe?
How to verify method call typsafe in junit with spring?
如何用类型安全的表达式替换 methodName("getEmployeeDetailsById")
?以某种方式直接链接到 class 的方法。这可能吗?
@RunWith(SpringRunner.class)
@WebMvcTest
public class MyTest {
@Test
public void test() {
mockMvc
.perform(get("/employee/details/9816"))
.andExpect(handler().handlerType(EmployeeController.class))
.andExpect(handler().methodName("getEmployeeDetailsById")); //TODO typesafe?
}
您可以在您的 Controller 中找到该方法,该方法具有 @GetMapping 注释(我假设您在这里使用的注释,根据需要进行调整),其值为“/employee/details/{id}”在 class:
中的所有方法中搜索它
private String findMethodName() {
List<Method> methods =
new ArrayList<>(Arrays.asList(EmployeeController.class.getMethods());
for (Method method : methods) {
if (method.isAnnotationPresent(GetMapping.class)) {
GetMapping annotation = method.getAnnotation(GetMapping.class);
if(Arrays.asList(annotation.value())
.contains("/employee/details/{id}") {
return method.getName();
}
}
}
}
然后你可以在你的mvcTest中调用这个方法:
.andExpect(handler().methodName(findMethodName()));
如果我没有误解你的意图,你想以静态方式建立期望。
您可以使用spring HandlerResultMatchers#methodCall & MvcUriComponentsBuilder#on来实现您的方式,例如:
mockMvc.perform(get("/employee/details/9816")).andExpect(
handler().methodCall(on(EmployeeController.class).getEmployeeDetailsById(args))
// args is any of the arbitrary value just to make the code to compile ---^
)
但是你需要注意的一件事是MvcUriComponentsBuilder#on will create a proxy to be able to inspect the previous invocations。当您将处理程序方法设为 return 一个 String
视图名称时,您应该将 return 类型的处理程序方法与其超类型(super 接口或 superclass) 的 String
class,因为它是 final 并且不能被 cglib 代理。例如:
@RequestMapping("/foo")
public Object handlerReturnViewName() {
// ^--- use the super type instead
return "bar";
}
如何用类型安全的表达式替换 methodName("getEmployeeDetailsById")
?以某种方式直接链接到 class 的方法。这可能吗?
@RunWith(SpringRunner.class)
@WebMvcTest
public class MyTest {
@Test
public void test() {
mockMvc
.perform(get("/employee/details/9816"))
.andExpect(handler().handlerType(EmployeeController.class))
.andExpect(handler().methodName("getEmployeeDetailsById")); //TODO typesafe?
}
您可以在您的 Controller 中找到该方法,该方法具有 @GetMapping 注释(我假设您在这里使用的注释,根据需要进行调整),其值为“/employee/details/{id}”在 class:
中的所有方法中搜索它private String findMethodName() {
List<Method> methods =
new ArrayList<>(Arrays.asList(EmployeeController.class.getMethods());
for (Method method : methods) {
if (method.isAnnotationPresent(GetMapping.class)) {
GetMapping annotation = method.getAnnotation(GetMapping.class);
if(Arrays.asList(annotation.value())
.contains("/employee/details/{id}") {
return method.getName();
}
}
}
}
然后你可以在你的mvcTest中调用这个方法:
.andExpect(handler().methodName(findMethodName()));
如果我没有误解你的意图,你想以静态方式建立期望。
您可以使用spring HandlerResultMatchers#methodCall & MvcUriComponentsBuilder#on来实现您的方式,例如:
mockMvc.perform(get("/employee/details/9816")).andExpect(
handler().methodCall(on(EmployeeController.class).getEmployeeDetailsById(args))
// args is any of the arbitrary value just to make the code to compile ---^
)
但是你需要注意的一件事是MvcUriComponentsBuilder#on will create a proxy to be able to inspect the previous invocations。当您将处理程序方法设为 return 一个 String
视图名称时,您应该将 return 类型的处理程序方法与其超类型(super 接口或 superclass) 的 String
class,因为它是 final 并且不能被 cglib 代理。例如:
@RequestMapping("/foo")
public Object handlerReturnViewName() {
// ^--- use the super type instead
return "bar";
}