Mockito thenReturn()返回null值

问题描述 投票:1回答:1

我是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);
            }

        }
java unit-testing testing nullpointerexception mockito
1个回答
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);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.