1

I am testing my AppUserController with MockMvc. Here is the case;

If the user is not found then I expect a ResourceNotFoundException to be thrown.

This is my test

   @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());

    }

This is the target to be tested

  @GetMapping("/{id}")
    public ResponseEntity<AppUserResponse> findAppUserById(@PathVariable UUID id) throws ResourceNotFoundException {
        AppUser user = userDetailsService.findAppUserById(id);
        return new ResponseEntity<>(appUserMapper.appUserToResponse(user),HttpStatus.OK);
    }

This is the service method that throws the exception.

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

Since I use Mockito to mock the service class, I expect the method findAppUserById throws ResourceNotFoundException because of this statement in my test case.

when(userDetailsService.findAppUserById(any())).thenThrow(ResourceNotFoundException.class);

However, when I run the test, it fails and I get the following error

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

upon @grekier request, the question updated with the following content.

ResourceNotFoundException

public class ResourceNotFoundException extends ApplicationException {
    public ResourceNotFoundException(String message) {
        super(-1,message,HttpStatus.NOT_FOUND);
    }

}

ApplicationException (base exception class)

@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);
    }
}
Demir
  • 799
  • 3
  • 16

2 Answers2

0

I think in your Test class you should update the setUp method, the error above might happened caused by your exception handler class was not worked

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

Hope it can help you.

  • I am getting another error right now. The server interestingly throws an error with status code 0. `No matching constant for [0]`. Because my handler method (`handleApplicationException`)tries to set the HTTP status value as 0. How is that even possible? – Demir Apr 24 '23 at 10:06
0

(Assumptions: current spring-boot)

Several problems here

  • useDefaultFilters = false "leads @WebMvcTest ad absurdum"/makes it (sort of) useless.

  • the mocking/injection is problematic

  • to use thenThrow() with exception class (overloads!), the exception class should have a "default constructor", which is/seems not applicable with the given (custom) ResourceNotFoundException.

Minor problem:

@ExtendWith(SpringExtension.class) doesn't much (gives an "unneccessary SpringExtension" warning from "spring-boot-tools" (vs code))

Reproduce

  • starter used

  • a simplified controller:

    @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);
       }
    }
    
  • with simplified service:

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

Working Test

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

}

Main Ref: https://spring.io/guides/gs/testing-web/

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • Since I use Spring Security, if I remove `useDefaultFilters = false `, then I get `java.lang.IllegalStateException: Failed to load ApplicationContext` the main cause of the exception is `Error creating bean with name 'webSecurityConfiguration' defined in file WebSecurityConfiguration.class: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'JwtAuthenticationEntryPoint' available: expected at least 1 bean which qualifies as autowire candidate`. – Demir Apr 24 '23 at 10:37
  • for the last problem: add (your) `JwtAuthenticationEntryPoint` to your test configuration (`@WebMvcTest({MyController.class/*, more classes here*/})`) ..or mock(bean) it. (assuming something [comparable to this](https://github.com/szerhusenBC/jwt-spring-security-demo/blob/master/src/main/java/org/zerhusen/security/JwtAuthenticationEntryPoint.java)) – xerx593 Apr 24 '23 at 10:46
  • Now I had to mock all the dependencies in the `WebSecurityConfiguration` (I have 4 dependencies). However, we are not finished yet. It requires mocking the dependencies of dependencies. Each dependency has at least 2 more dependencies, which means I need to mock at least 10 more dependencies to test a single class that is not relevant to the current class(`AppUserController`). Those dependencies are coming from security. I am not sure if that is the right way to test my single controller. Is there a better way? – Demir Apr 24 '23 at 11:06
  • ..ok i get you, ..but hard to reproduce... and ["controversial question"](https://stackoverflow.com/q/31169720/592355) (whether/how to deactivate security for tests) – xerx593 Apr 24 '23 at 11:50
  • i (enriched with security and) set up a [(public) repo](https://github.com/xerx593/soq76090017), when you can reproduce/"patch the issue", i can fix! ;) – xerx593 Apr 24 '23 at 11:58