jeudi 26 mai 2016

Mockito test throws NullPointerException for no reason [duplicate]

This question already has an answer here:

I'm trying to write some tests in Mockito but for some reason it fails at some point:

@RunWith(MockitoJUnitRunner.class)
public class RentedControllerTest {

    @Mock
    private RentedService rentalService;
    private MovieService movieService;
    private ClientService clientService;

    @InjectMocks
    private RentedController rentalController;
    private MovieController movieController;
    private ClientController clientController;


    @Before
    public void setUp() throws Exception {
        initMocks(this);
    }

    @Test
    public void getMovies() throws Exception {
        List<Movie> movies = new ArrayList<>();
        movies.add(mock(Movie.class));
//        System.out.println(movies.toString());
        when(movieService.findAll()).thenReturn(movies);
//        System.out.println(movieService.findAll().toString());
//        MoviesDto response = movieController.getMovies();
//        assertEquals("should be 1 movie", 1, response.getMovies().size());
    }

It failes at "when..." It throws NullPointerException but it doesn't say anything more. No explanation. The system print from before shows 1 mock in my list, what is happening?

EDIT: The problem was that I added the @Mock and @InjectMocks only for the first Services/Controllers.

Aucun commentaire:

Enregistrer un commentaire