Cześć. Możecie mi powiedzieć, dlaczego po dodaniu adnotacji @mock wywala mi NPE?
W ten sposób działa, test przechodzi:
@Test
void createNewCustomer() throws SQLException {
CarRentalStorage carRentalStorageMock = mock(CarRentalStorage.class);
CarRentalOptions carRentalOptions = new CarRentalOptions(carRentalStorageMock);
Client client = new Client();
carRentalOptions.createNewCustomer(client);
List<Client> listOfClients = new ArrayList<Client>();
listOfClients.add(client);
verify(carRentalStorageMock).addClient(client);
when(carRentalStorageMock.getAllCustomers()).thenReturn(listOfClients);
assertEquals(1, listOfClients.size());
}
Jeżeli zrobie coś takiego, to wywala mi NPE.
@RunWith(MockitoJUnitRunner.class)
class CarRentalOptionsTest {
@Mock
CarRentalStorage carRentalStorageMock;
@Test
void createNewCustomer() throws SQLException {
//CarRentalStorage carRentalStorageMock = mock(CarRentalStorage.class);
CarRentalOptions carRentalOptions = new CarRentalOptions(carRentalStorageMock);
Client client = new Client();
carRentalOptions.createNewCustomer(client);
List<Client> listOfClients = new ArrayList<Client>();
listOfClients.add(client);
verify(carRentalStorageMock).addClient(client);
when(carRentalStorageMock.getAllCustomers()).thenReturn(listOfClients);
assertEquals(1, listOfClients.size());
}
}
Dodam, ze kierowałem się tym i tam wszystko działa:
Testuję tę metodę z CarRentalOptions
void createNewCustomer(Client client) throws SQLException {
storage.addClient(client);
}