我正在用MockMvc测试我的AppUserController
。情况是这样的;
如果找不到用户,那么我希望抛出ResourceNotFoundException
。
这是我的考验
@Test
void findAppUserById_throwsNotFound() throws Exception {
//given
UUID uuid = UUID.fromString("ac0a1859-8e59-4ede-8def-043c32aa3208");
//when
when(userDetailsService.findAppUserById(any())).thenThrow(ResourceNotFoundException.class);
MvcResult mvcResult = mockMvc
.perform(get("/app-users/" + uuid))
.andExpect(status().isNotFound())
.andReturn();
String resultContent = mvcResult.getResponse().getContentAsString();
ResourceNotFoundException response = objectMapper.readValue(resultContent, ResourceNotFoundException.class);
//then
assertNotNull(response);
assertEquals(404,response.getHttpStatusCode());
}
这是要测试的目标
@GetMapping("/{id}")
public ResponseEntity<AppUserResponse> findAppUserById(@PathVariable UUID id) throws ResourceNotFoundException {
AppUser user = userDetailsService.findAppUserById(id);
return new ResponseEntity<>(appUserMapper.appUserToResponse(user),HttpStatus.OK);
}
这是引发异常的服务方法。
public AppUser findAppUserById(UUID id) throws ResourceNotFoundException {
AppUser appUser = appUserRepository.findById(id).orElseThrow(
() -> new ResourceNotFoundException("Kullanıcı bulunamadı")
);
if (!appUser.isEnabled()) {
throw new InvalidRequestException(PASSIVE_USER_MESSAGE);
}
return appUser;
}
由于我使用Mockito
来模拟服务类,因此我预计方法findAppUserById
会抛出ResourceNotFoundException
,因为在我的测试用例中有这样的语句。when(userDetailsService.findAppUserById(any())).thenThrow(ResourceNotFoundException.class);
然而,当我运行测试时,它失败了,我得到了以下错误
rg.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.napsam.wo.exception.ResourceNotFoundException
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:670)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:201)
at com.napsam.wo.controller.AppUserControllerTest.findAppUserById_throwsNotFound(AppUserControllerTest.java:96)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: com.napsam.wo.exception.ResourceNotFoundException
根据@grekier的要求,问题更新了以下内容。
ResourceNotFoundException
public class ResourceNotFoundException extends ApplicationException {
public ResourceNotFoundException(String message) {
super(-1,message,HttpStatus.NOT_FOUND);
}
}
ApplicationException(异常基类)
@Getter
@Setter
@JsonIgnoreProperties({"stackTrace", "cause", "suppressed", "localizedMessage"})
public class ApplicationException extends RuntimeException {
private final int resultCode;
private final String message;
private final int httpStatusCode;
private final ZonedDateTime thrownAt;
private static final String ZONE_ID = "Europe/Istanbul";
public ApplicationException(int resultCode, String message, HttpStatus httpStatus) {
super();
this.resultCode = resultCode;
this.message = message;
this.httpStatusCode = httpStatus.value();
this.thrownAt = ZonedDateTime.now(ZoneId.of(ZONE_ID));
}
}
ExceptionHandler
@ControllerAdvice
public class AppExceptionHandler {
@ExceptionHandler(value = {ApplicationException.class})
public ResponseEntity<ApplicationException> handleApplicationException(ApplicationException applicationException) {
return new ResponseEntity<>(applicationException, HttpStatus.valueOf(applicationException.getHttpStatusCode()));
}
}
AppUserControllerTest
package com.napsam.wo.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.napsam.wo.dtos.AppUserResponse;
import com.napsam.wo.dtos.RegisterRequest;
import com.napsam.wo.entities.AppUser;
import com.napsam.wo.entities.Gender;
import com.napsam.wo.exception.ResourceNotFoundException;
import com.napsam.wo.mapper.AppUserMapper;
import com.napsam.wo.service.AppUserService;
import com.napsam.wo.service.UserDetailsServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(SpringExtension.class)
@WebMvcTest(useDefaultFilters = false)
class AppUserControllerTest {
@Mock
private UserDetailsServiceImpl userDetailsService;
@Mock
private AppUserService appUserService;
@Mock
private AppUserMapper appUserMapper;
ObjectMapper objectMapper = new ObjectMapper();
@Autowired
private MockMvc mockMvc;
@InjectMocks
AppUserController underTest;
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(underTest).build();
}
@Test
void findAppUserById_success() throws Exception {
//given
UUID appUserId = UUID.randomUUID();
AppUser appUser = new AppUser();
appUser.setAppUserId(appUserId);
AppUserResponse appUserResponse = new AppUserResponse();
appUserResponse.setAppUserId(appUserId);
//when
when(userDetailsService.findAppUserById(appUserId)).thenReturn(appUser);
when(appUserMapper.appUserToResponse(appUser)).thenReturn(appUserResponse);
MvcResult mvcResult = mockMvc
.perform(get("/app-users/" + appUserId))
.andExpect(status().isOk())
.andReturn();
String resultContent = mvcResult.getResponse().getContentAsString();
AppUserResponse response = objectMapper.readValue(resultContent, AppUserResponse.class);
//then
assertNotNull(response);
assertEquals(appUserId, response.getAppUserId());
}
@Test
void findAppUserById_throwsNotFound() throws Exception {
//fixme
//given
UUID uuid = UUID.fromString("ac0a1859-8e59-4ede-8def-043c32aa3208");
//when
when(userDetailsService.findAppUserById(any())).thenThrow(ResourceNotFoundException.class);
MvcResult mvcResult = mockMvc
.perform(get("/app-users/" + uuid))
.andExpect(status().isNotFound())
.andReturn();
String resultContent = mvcResult.getResponse().getContentAsString();
ResourceNotFoundException response = objectMapper.readValue(resultContent, ResourceNotFoundException.class);
//then
assertNotNull(response);
assertEquals(404, response.getHttpStatusCode());
}
@Test
void registerDetails_success() throws Exception {
//given
RegisterRequest request = new RegisterRequest();
request.setAvatar("a");
request.setBiography("b");
request.setFullName("f");
request.setGender(Gender.KADIN);
String json = objectMapper.writeValueAsString(request);
//when
when(appUserService.registerDetails(any(), any())).thenReturn(new AppUserResponse());
MvcResult mvcResult = mockMvc.perform(post("/app-users/register-details/" + UUID.randomUUID())
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isCreated())
.andReturn();
String resultContent = mvcResult.getResponse().getContentAsString();
AppUserResponse response = objectMapper.readValue(resultContent, AppUserResponse.class);
//then
assertNotNull(response);
}
}
2条答案
按热度按时间kqhtkvqz1#
我认为在你的Test类中你应该更新
setUp
方法,上面的错误可能是由于你的异常处理程序类没有工作而导致的。希望能对你有所帮助。
sdnqo3pr2#
(假设:电流Spring启动)
几个问题
useDefaultFilters = false
“leads@WebMvcTest
ad absurdum”/使它(有点)无用。thenThrow()
与异常类(重载!),异常类应该有一个“默认构造函数”,这是/似乎不适用于给定的(自定义)ResourceNotFoundException
。小问题:
@ExtendWith(SpringExtension.class)
没有太多(从“spring-boot-tools”(vs code)给出“unneccessary SpringExtension”警告)复制
Exception
+ /-Handler
类工作测试
主参考:https://spring.io/guides/gs/testing-web/