• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Mike London

Getting NullPointerException when run unit test using Spring boots, JUnit 5 and Mockito

Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am new Spring boots, JUnit 5 and Mockito.
I am trying to learn this technolgy.
I have simple RestController with one method createUser.
When I test this controller method through Postman, it work fine.
But when run unit test (Shown below), I am getting NullPointerException (marked inside method).
It is not creating userResponse. Here is the code;

Please can someone advice me where I am going wrong.

public class UserRestController {

UserService userService;

@PostMapping(path="save", consumes= {MediaType.APPLICATION_JSON_VALUE}, produces= {MediaType.APPLICATION_JSON_VALUE})
public String createUser(@RequestBody User user) {
User userResponse = userService.createUser(user);
System.out.println("userResponse ..: " + userResponse );  // These is null when Test run

return String.valueOf(userResponse.getId());
} //  end UserController

public class UserServiceImpl implements UserService {

private UserRepository userRepository;

public User createUser(User user) {
User userResponse = userRepository.save(user);
return userResponse;

public class UserRestControllerTest {

private MockMvc mockMvc;

private ObjectMapper objectMapper;

UserService userService;

@DisplayName("UserRestController createUser Test")
public void createUserTest() throws JsonProcessingException, Exception {

User newUser = new User("Mr", "Dabby", "Wall", 27);
User savedUser = new User(1, "Mr", "Dabby", "Wall", 27);


String url = "/user/save";

Here is error I am getting;
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
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:652)
Caused by: java.lang.NullPointerException
at com.ics.restController.UserRestController.createUser(UserRestController.java:37)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Posts: 2266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Ish,
Try to annotate @AutoConfigureMvc to your WebMvcTest class. I guess your MockMvc is null.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic