Home > Blockchain >  IllegalArgumentException: null source, while testing spring controller with MockMvc and Mockito
IllegalArgumentException: null source, while testing spring controller with MockMvc and Mockito

Time:08-12

Basically, I am trying to test a POST endpoint of my Spring-Boot API, using the MockMvc standalone setup and Mockito. The endpoint is publishing an event and on creating the event, on the super() call I am getting an “java.lang.IllegalArgumentException: null source” exception.

I suspect it is because the mocked event publisher but can not get it to work. Actually, I am new to Spring, Mockito, etc. Hope someone can help.

Edit: Because I have been trying to fix this issue, I had a lot of unnecessary code, which I deleted for this post. I marked the relevant lines in the code snippets with comments.

Controller:

import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationEventPublisher;
import com.s4timuen.springsecurityclient.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.s4timuen.springsecurityclient.model.UserModel;
import com.s4timuen.springsecurityclient.entity.User;
import org.springframework.web.bind.annotation.*;

/**
 * Controller for user registration.
 */
@SuppressWarnings("unused")
@RestController
@RequestMapping(path = "/api/v1")
public class UserRegistrationController {

    private static final String MESSAGE_USER_REGISTRATION_SUCCESS
            = "User registration successful.";

    @Autowired
    private UserService userService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @PostMapping(path = "/registerUser")
    public String registerUser(@RequestBody UserModel userModel, final HttpServletRequest request) {

        User user = userService.registerUser(userModel);
        eventPublisher.publishEvent(new UserRegistrationEvent(user, buildApplicationUrl(request))); // line 65

        return MESSAGE_USER_REGISTRATION_SUCCESS;
    }
}

Event:

import com.s4timuen.springsecurityclient.entity.User;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;

/**
 * Event for user registration.
 */
@Getter
@Setter
public class UserRegistrationEvent extends ApplicationEvent {

    private User user;
    private String applicationUrl;

    /**
     * Constructor for class UserRegistrationEvent.
     *
     * @param user           User object.
     * @param applicationUrl The application URL.
     */
    public UserRegistrationEvent(User user, String applicationUrl) {
        super(user); // line 25
        this.user = user;
        this.applicationUrl = applicationUrl;
    }
}

Test:

import com.s4timuen.springsecurityclient.event.UserRegistrationEvent;
import com.s4timuen.springsecurityclient.model.UserModel;
import com.s4timuen.springsecurityclient.service.UserService;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * User registration controller tests.
 */
@SpringBootTest
@ExtendWith(MockitoExtension.class)
@DisplayName("User Registration Controller")
public class UserRegistrationControllerTest {

    private static final String MESSAGE_USER_REGISTRATION_SUCCESS
            = "User registration successful.";

    @Mock
    private UserService userService;
    @Mock
    private ApplicationEventPublisher eventPublisher;
    @InjectMocks
    private UserRegistrationController userRegistrationController;

    private MockMvc mockMvc;

    @BeforeEach
    public void setUp() {

        mockMvc = MockMvcBuilders
                .standaloneSetup(userRegistrationController)
                .build();
    }

    @AfterEach
    public void tearDown() {
        mockMvc = null;
    }

    /**
     * (POST) Register user endpoint test.
     */
    @Test
    @DisplayName("POST Register User")
    public void registerUser() throws Exception {
        // given
        String requestBody = "{"  
                "\"nickname\":"   "\"nick\","  
                "\"firstName\":"   "\"first\","  
                "\"lastName\":"   "\"last\","  
                "\"email\":"   "\"[email protected]\","  
                "\"password\":"   "\"123\""  
                "}";

        // when
        mockMvc.perform(post("/api/v1/registerUser") // line 86
                .contentType(MediaType.APPLICATION_JSON)
                .content(requestBody))
        // then
                .andExpect(status().isOk())
                .andExpect(result -> assertEquals(MESSAGE_USER_REGISTRATION_SUCCESS, result.getAsyncResult()));
        verify(userService).registerUser(any(UserModel.class));
        verify(eventPublisher).publishEvent(any(UserRegistrationEvent.class));
    }
}

Log:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: null source

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
    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:764)
    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:199)
    at com.s4timuen.springsecurityclient.controller.UserRegistrationControllerTest.registerUser(UserRegistrationControllerTest.java:86)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    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:1511)
    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:1511)
    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:71)
    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:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.IllegalArgumentException: null source
    at java.base/java.util.EventObject.<init>(EventObject.java:57)
    at org.springframework.context.ApplicationEvent.<init>(ApplicationEvent.java:48)
    at com.s4timuen.springsecurityclient.event.UserRegistrationEvent.<init>(UserRegistrationEvent.java:25)
    at com.s4timuen.springsecurityclient.controller.UserRegistrationController.registerUser(UserRegistrationController.java:65)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    ... 78 more


CodePudding user response:

You are mixing concerns which is why it doesn't work.

  1. You only want to test a single controller you are better of using @WebMvcTest.
  2. Let Spring manage and inject the mocks use @MockBean instead of @Mock and use @Autowired instead of @InjectMocks.
  3. You didn't register any behavior for the mocks, leading to a null being returned (the default behavior).
  4. The @SpringBootTest currently is useless as you are using plain Mockito, which only delays testing. (See also 1).

Where number 3 is actually the main culprit!

With all that in mind your test should look somethi ng like this.

@WebMvcTest(UserRegistrationController.class)
@AutoConfigureMockMvc
@DisplayName("User Registration Controller")
public class UserRegistrationControllerTest {

    private static final String MESSAGE_USER_REGISTRATION_SUCCESS
            = "User registration successful.";

    @MockBean
    private UserService userService;
    @MockBean
    private ApplicationEventPublisher eventPublisher;
    @Autowired
    private UserRegistrationController userRegistrationController;
    @Autowired
    private MockMvc mockMvc;

    /**
     * (POST) Register user endpoint test.
     */
    @Test
    @DisplayName("POST Register User")
    public void registerUser() throws Exception {
        // given
        String requestBody = "{"  
                "\"nickname\":"   "\"nick\","  
                "\"firstName\":"   "\"first\","  
                "\"lastName\":"   "\"last\","  
                "\"email\":"   "\"[email protected]\","  
                "\"password\":"   "\"123\""  
                "}";

        when(userService.registerUser(any(UserModel.class)).thenReturn(new User()); // Return a user instead of null (the default)

        // when
        mockMvc.perform(post("/api/v1/registerUser") // line 86
                .contentType(MediaType.APPLICATION_JSON)
                .content(requestBody))
        // then
                .andExpect(status().isOk())
                .andExpect(result -> assertEquals(MESSAGE_USER_REGISTRATION_SUCCESS, result.getAsyncResult()));
        verify(userService).registerUser(any(UserModel.class));
        verify(eventPublisher).publishEvent(any(UserRegistrationEvent.class));
    }
}
  • Related