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);
}
}
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.