3

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.

@RunWith(MockitoJUnitRunner.class)
@WebMvcTest(value = MovieRestApiController.class, secure = false)
    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);
            }

        }
senham
  • 131
  • 2
  • 4
  • 13

1 Answers1

1

I've had mixed results with mixing Mockito and Spring annotation in unit tests using MockMvc. Here's an approach that I use which makes Mockito, Spring, and MockMvc happy. I'm sure there's a better way to do this, and if someone has a suggestion, I'd love to hear it.

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class MovieRestApiControllerTest {

  // provide a static spring config for this test:
  static class ContextConfiguration {

    // provide Beans that return a Mockito mock object
    @Bean
    public MovieService movieService() {
      return Mockito.mock(MovieService.class);
    }

    ...

  }
    @Autowired
    private MockMvc mockMvc;

    // Autowire your mocks
    @Autowired
    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\"}";

    // make sure your context is loaded correctly
    @Test
    public void testContextLoaded() {
        assertNotNull(movieService);
    }

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

}
Michael Peacock
  • 2,011
  • 1
  • 11
  • 14
  • Sorry, it doesn't work for me, there is a problem with @Autowired "Could not autowire. No beans of 'MockMvc' type found" – senham May 04 '18 at 15:35
  • You don't need to Autowire mockMvc - use the MockMvcBuilders class to provide one. Also - what Controller class are you testing? I don't see one in your test class. For example mockMvc = MockMvcBuilders.standaLoneSetup(ControllerUnderTest.class).defaultRequest(get("/findMovie")).build(); – Michael Peacock May 04 '18 at 15:57