vendredi 4 mai 2018

Mockito thenReturn() returns null value

I'm new at Mockito and I have problem with thenReturn method. I've read tutorial where this kind of solution work well, but in my programme there must be any inconsistency compare to aforementioned example.

public class MovieRestApiControllerTest {

        @Autowired
        private MockMvc mockMvc;

        @MockBean
        private MovieService movieService;

        private ArrayList<Movie> moviesMock;

        @Before
        public void setUp() {
            moviesMock = new ArrayList<>(Arrays.asList(new Movie("Top Gun", "Akcja", "Tony Scott", 15000000, 110)));
        }

        String exampleMovieJson = "{\"title\":\"Top Gun\",\"director\":\"Tony Scott\",\"runtime\":\"110\":\"budget\":\"15000000\":\"genre:\":\"Akcja\"}";

        @Test
        public void retrieveDetailsForMovie() throws Exception {
    //THIS FUNCTION CAUSE NULL POINTER EXCEPTION
            Mockito.when(
                    movieService.findMovies(Mockito.anyString(), Mockito.anyString())).thenReturn(moviesMock);

            RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
                    "/find-movie").accept(
                    MediaType.APPLICATION_JSON);

            MvcResult result = mockMvc.perform(requestBuilder).andReturn();

            System.out.println(result.getResponse());
            String expected = "{title:Top Gun,director:Tony Scott,runtime:110,budget:15000000,genre:Akcja}";

            JSONAssert.assertEquals(expected, result.getResponse()
                    .getContentAsString(), false);
        }

    }

Aucun commentaire:

Enregistrer un commentaire