我是 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);
}
}
最佳答案
我在使用 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);
}
}
关于java - Mockito thenReturn() 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176488/