tengo un pequeño problema y es que no se como pasar un usuario al controlador, el cual se obtiene a partir de otro usuario.
El método del controlador que estoy testeando te muestra una sugerencia de amistad:
@GetMapping("/suggestion")
public final String showSuggestions(HttpServletRequest request, final ModelMap model)
throws SuggestionNotFoundException {
User user;
User userSuggested;
HttpSession misession;
List<Image> userImages;
ImageOutputData defaultOutput;
misession = request.getSession(false);
if (misession == null) {
return ErrorViewConstants.NOT_LOGIN;
}
user = (User) misession.getAttribute(UserViewConstants.BEAN_USER_LOGGED);
//ERROR
userSuggested = getFriendshipService().userSugestion(user);
// lista de imagenes del usuario
userImages = getImageService().getAllUserImages(userSuggested.getId());
// imagen por defecto
defaultOutput = new ImageOutputData();
// si no hay elementos, mostramos imagen por defecto
if (userImages.isEmpty()) {
defaultOutput.setId(-1);
defaultOutput.setPath(UtilConstants.DEFAULT_IMAGE);
defaultOutput.setNoImage(true);
defaultOutput.setUserAge(userSuggested.userAge());
defaultOutput.setUserCity(userSuggested.getCity());
defaultOutput.setUserDescription(userSuggested.getDescription());
defaultOutput.setUserSex(userSuggested.getSex().toString());
} else {
Image defaultImage = userImages.remove(0);
defaultOutput.setPath(defaultImage.getPath());
defaultOutput.setId(defaultImage.getId());
defaultOutput.setUserAge(defaultImage.getUser().userAge());
defaultOutput.setUserCity(defaultImage.getUser().getCity());
defaultOutput.setUserDescription(defaultImage.getUser().getDescription());
defaultOutput.setNoImage(false);
defaultOutput.setUserSex(defaultImage.getUser().getSex().toString());
}
// lista de fotos obtenidas
UserSuggestionOutputForm suggestion = new UserSuggestionOutputForm();
suggestion.setId(userSuggested.getId());
suggestion.setUserAge(userSuggested.userAge());
suggestion.setUserCity(userSuggested.getCity());
suggestion.setUserSex(userSuggested.getSex().toString());
suggestion.setDescription(userSuggested.getDescription());
suggestion.setUsername(userSuggested.getUsername());
suggestion.setAverageRating(getUserService().getAverageRating(userSuggested.getId()));
// foto que mostramos por defecto
model.put(UserViewConstants.DEFAULT_IMAGE, defaultOutput);
model.put(ImageViewConstants.IMAGES_CAROUSEL, ControllerMethods.outputList(userImages));
model.put(UserViewConstants.BEAN_USER_SUGGESTED, suggestion);
model.put(UserViewConstants.BEAN_USER_LOGGED, user);
return FriendshipViewConstants.SUGGESTION;
}
Donde pone la etiqueta //ERROR es donde surge el problema, ya que paso un mock de user y por lo tanto userSuggested es null. Entonces, quiero pasar por el test del controlador userSuggested para que no me lanze la excepcion y poder probar el método.
Adjunto el código del test del controlador:
@RunWith(JUnitPlatform.class)
public class TestSuggestionFriendshipController {
private MockMvc mockMvc;
FriendshipService service;
public TestSuggestionFriendshipController() {
super();
}
@BeforeEach
public final void setUpMockContext() throws InstanceNotFoundException {
mockMvc = MockMvcBuilders.standaloneSetup(showSuggestionsController())
.alwaysExpect(MockMvcResultMatchers.status().isOk()).build();
}
@Test
public final void testShowSuggestion() throws Exception {
mockMvc.perform(getSuggestionsViewRequest());
}
private final FriendshipSuggestionController showSuggestionsController() throws InstanceNotFoundException {
final ImageService imageService;
final UserService userService;
this.service = Mockito.mock(FriendshipService.class);
imageService = Mockito.mock(ImageService.class);
userService = Mockito.mock(UserService.class);
return new FriendshipSuggestionController(service, imageService, userService);
}
private final RequestBuilder getSuggestionsViewRequest() throws SuggestionNotFoundException {
User user = new User();
User userSuggested = new User();
this.service = Mockito.mock(FriendshipService.class);
user.setId(1);
userSuggested.setId(2);
Mockito.when(service.userSugestion(user)).thenReturn(userSuggested);
return MockMvcRequestBuilders.get(UrlFriendshipConfig.SUGGESTIONS).sessionAttr("userLogged", user);
}
}
Al ejecutar los test me está lanzando la siguiente error : NullPointerException
Un saludo y muchas gracias