Mockito thenReturn()返回null值

时间:2018-05-04 13:48:35

标签: java unit-testing testing nullpointerexception mockito

我是Mockito的新人,我对thenReturn方法有疑问。我已经阅读了这种解决方案运行良好的教程,但在我的程序中,与上述示例相比,必须存在任何不一致。

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

        }

1 个答案:

答案 0 :(得分:0)

我使用MockMvc在单元测试中混合了Mockito和Spring注释,结果好坏参半。这是我使用的一种让Mockito,Spring和MockMvc开心的方法。我确定有更好的方法可以做到这一点,如果有人有建议,我很乐意听到。

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

}