提问者:小点点

MockMvc测试控制器不抛出异常


我正在使用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,因为我的测试用例中有这个语句。

当(用户详细信息服务。查找App User By Id(any()))。然后抛出(资源未找到异常。类);

但是,当我运行测试时,它失败了,我得到以下错误

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请求,问题更新为以下内容。

资源未找到异常

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));
    }

 }

异常处理程序

@ControllerAdvice
public class AppExceptionHandler {
    @ExceptionHandler(value = {ApplicationException.class})
    public ResponseEntity<ApplicationException> handleApplicationException(ApplicationException applicationException) {
        return new ResponseEntity<>(applicationException, HttpStatus.valueOf(applicationException.getHttpStatusCode()));
    }
}

应用用户控制器测试

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个答案

匿名用户

我认为在您的Test类中您应该更新setUp方法,上面的错误可能是由您的异常处理程序类未工作引起的

@Before
public void setup() {
 this.mockMvc = MockMvcBuilders.standaloneSetup(underTest)
     .setControllerAdvice(new AppExceptionHandler())
    .build();
}

希望能帮到你。

匿名用户

(假设:当前spring-boot)

>

  • useDefaultFilters=false"导致@WebMvcTest广告荒谬"/使它(有点)没用。

    嘲笑/注入是有问题的

    要将thenThrow()与异常类(重载!)一起使用,异常类应该有一个“默认构造函数”,它/似乎不适用于给定的(自定义)ResourceNotFoundException

    @Exentwith(SpringExtension. class)并不多(从“spring-boot-tools”(vs code)给出了“不必要的SpringExency”警告)

    >

  • 使用了启动器

    一个简化的控制器:

    @RestController
    class MyController {
       @Autowired
       private MyService userDetailsService;
    
       @GetMapping("/my/{id}")
       public ResponseEntity<String> myGet(@PathVariable UUID id) throws ResourceNotFoundException {
         return new ResponseEntity<>(userDetailsService.doSomething(id), HttpStatus.OK);
       }
    }
    

    简化服务:

    @Service
    public class MyService {
       @SuppressWarnings("java:S106")
       public String doSomething(UUID id) {
         System.err.println(id.toString() + " done.");
         // potentially throws RuntimeExceptions...
         return id.toString();
       }
    }
    

    Exception /-处理程序类

    package com.example.demo;
    
    import static org.mockito.Mockito.verify;
    import static org.mockito.Mockito.when;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import java.util.UUID;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.test.web.servlet.MockMvc;
    // don't @ExtendWith(SpringExtension.class)
    @WebMvcTest(MyController.class) // useDefaultFilters = false seems "badong" here!
    class MyWebTest {
    
        @MockBean // use rather spring-boot extension than "plain ole" mockito!;)
        private MyService myService;
    
        @Autowired // as usual
        private MockMvc mockMvc;
    
        // no @Before/-All !!! ;)
    
        @Test
        void testMyGet_success() throws Exception {
            // given
            UUID uid = UUID.randomUUID();
            when(myService.doSomething(uid)).thenCallRealMethod();
    
            // when
            mockMvc.perform(get("/my/" + uid))
            
            // then
            .andExpectAll(
              status().isOk(),
              content().string(uid.toString())
            );
    
            verify(myService).doSomething(uid);
        }
    
        @Test
        void testMyGet_notFound() throws Exception {
            // given
            UUID uuid = UUID.fromString("ac0a1859-8e59-4ede-8def-043c32aa3208");
            when(myService.doSomething(uuid)).thenThrow(
              /* better, here: exception instance!*/
              new ResourceNotFoundException("mockba")
            );
    
            // when
            mockMvc.perform(get("/my/" + uuid))
                    
            // then
            .andExpect(status().isNotFound()); // expect more, if you like 
    
            verify(myService).doSomething(uuid);
        }
    
    }
    

    主参考:https://spring.io/guides/gs/testing-web/