• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

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

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.

@RestController
@RequestMapping("/user")
public class UserRestController {

@Autowired
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


@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

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

@WebMvcTest(UserRestController.class)
public class UserRestControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@MockBean
UserService userService;

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

when(userService.createUser(newUser)).thenReturn(savedUser);

String url = "/user/save";
this.mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(newUser)))
.andExpect(status().isOk())
.andExpect(content().string("1")
);
}
}


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)


 
Bartender
Posts: 2447
13
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using @AutoConfigureMockMvc but still getting NullPointerException.
 
Himai Minh
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to make it as @SpringBootTest instead of @WebMvcTest to see if you have that issue.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am facing same issue, when mocked service bean is null.
Was this issue solved ? If so, could you please provide the solution that worked.

Thanks
 
Himai Minh
Bartender
Posts: 2447
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when mocked service bean is null


Are you sure your service bean is annotated as a component? Try to use @SpringBootTest first to see if your service bean can be mocked.
Also, do you have an implementation of UserService imported to your classpath  ? Is that UserService's implementation is defined as a bean?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic