0

The first block of code is the Mockito test code and the actual code is next. I have checked the code it works fine but while checking with Mockito test it throws me an error of null pointer exception. Could anyone please help me with this error?

    @Test
    public void showImage() throws Exception {
        User user = new User();
        UserProfile userProfile = new UserProfile();
        userProfile.setId(1);
        userProfile.setEmailAddress("a@gmail.com");
        userProfile.setFullName("Abhi Mahajan");
        userProfile.setMobileNumber("9876543210");
        user.setProfile(userProfile);
        user.setId(1);
        user.setUsername("Abhi");
        user.setPassword("password1@");

        session = new MockHttpSession();
        session.setAttribute("loggeduser", user);

        Image image = new Image();
        image.setId(1);
        image.setTitle("new");
        image.setDescription("This image is for testing purpose");
        image.setUser(user);

        List<Image> images = new ArrayList<>();
        Tag tag = new Tag();
        tag.setId(1);
        tag.setName("dog");
        images.add(image);
        tag.setImages(images);

        List<Tag> tags = new ArrayList<>();
        tags.add(tag);
        image.setTags(tags);

        Mockito.when(imageService.getImage(Mockito.anyInt())).thenReturn(image);

        this.mockMvc.perform(get("/images/1/new")
                .param("imageId","1")
                .session(session))
                .andExpect(view().name("images/image"))
                .andExpect(content().string(containsString("Welcome User. This is the image")));
    @RequestMapping("/images/{imageId}/{title}")
    public String showImage(@PathVariable("imageId") Integer imageId, Model model) {
        Image image = imageService.getImageByImageId(imageId);

        //Calls the comment Service to get all the comments related to an image & return a list.
        List <Comment> comments = commentService.getCommentsByImageId(imageId);
        model.addAttribute("image", image);
        model.addAttribute("tags",image.getTags());

        //Comment list is added to the model so that it is displayed in the images.html. Here the key is "comments"
        model.addAttribute("comments",comments);
        return "images/image";
  • 1
    Welcome to Stack Overflow! Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. Please include the actual stack trace, so that people can understand WHERE exactly that NPE gets thrown. Beyond that: please study https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it+ carefully... – GhostCat Apr 03 '19 at 14:52
  • In other words: we have no idea what exactly your test is doing, and `mockMvc` somehow implies that the whole production/test code is run within some larger framework ... such details might matter, too. If you want us to help you, you have to enable us to do so. – GhostCat Apr 03 '19 at 14:54

0 Answers0