So I'm still having problems using Mockito. Therefore, suppose I have the following class (please ignore the logic or its structure, this is just a short example that I created from another class, with different names, etc.):
public class Restaurant(
@Autowired
private CustomerService customerService;
private CustomerInputData updateCustomer(CustomerInputData inputData){
String customerId = inputData.getID();
Customer customer = customerService.getCustomerById(customerID);
if(customer.getAddress() != null){
inputData.setCustomerName(customer.getCustomerName());
inputData.setCustomerCity(customer.getCustomerCity);
inputData.setCustomerLanguage(customer.getLanguage);
}
return inputData
}
}
So my understanding of Unit-Tests is to isolate all the dependencies. Here I will have the class Customer and Customer-Service.
So, to write a test class, I am currently doing the following:
public class RestaurantTest()
{
@Mock(name="customerService");
private CustomerService customerService;
@InjectMocks
private Restaurant classUnderTest;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void updateCustomer_WithValidInput_ShouldReturnUpdatedInput(){
//Some Mocking first
String customerId = "customerId";
Customer customer = mock(Customer.class);
CustomerInputData = mock(CustomerInputData.class);
doReturn(customer).when(customerService.getCustomerById(any(String.class)));
doReturn(customerId).when(inputData.getId());
doReturn("address").when(customer.getAddress());
doReturn("Name").when(customer.getName());
doReturn("City").when(customer.getCity());
doReturn("Language").when(customer.getLanguage());
doNothing().when(inputData).setCustomerName(any(String.class));
doNothing().when(inputData).setCustomerCity(any(String.class));
doNothing().when(inputData).setCustomerLanguage(any(String.class));
verify(customer.getAddress(), atLeastOnce());
verify(customer.getName(), atLeastOnce());
//and so on...
verify(inputData, atLeastOnce()).setCustomerName(eq("Name"));
verify(inputData, atLeastOnce()).setCustomerCity(eq("City"));
verify(inputData, atLeastOnce()).setCustomerLanguage(eq("Language");
}
}
, Assert, , . , , , Test- setter/getter - . , inputData.setCustomerCity , . , CustomerInputData.
, , ?
Mockito ? mock(), setter , doReturns ..?
, whitebox, , .