@RunWith(SpringRunner.class)
@WebMvcTest(ContactsManagementController.class)
public class ContactsManagementControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ContactsManagementService contactsManagementService;
@InjectMocks
private ContactsManagementController contactsManagementController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testAddContactHappyPath() throws Exception {
// setup mock Contact returned the mock service component
CustomerContact mockCustomerContact = new CustomerContact();
mockCustomerContact.setFirstName("Fred");
when(contactsManagementService.add(any(CustomerContact.class)))
.thenReturn(mockCustomerContact);
// simulate the form bean that would POST from the web page
CustomerContact aContact = new CustomerContact();
aContact.setFirstName("Fred");
aContact.setEmail("fredj@myemail.com");
// simulate the form submit (POST)
mockMvc
.perform(post("/addContact", aContact))
.andExpect(status().isOk())
.andReturn();
}
@Test
public void testAddContactBizServiceRuleNotSatisfied() throws Exception {
// setup a mock response of NULL object returned from the mock service component
when(contactsManagementService.add(any(CustomerContact.class)))
.thenReturn(null);
// simulate the form bean that would POST from the web page
CustomerContact aContact = new CustomerContact();
aContact.setLastName("Johnson");
// simulate the form submit (POST)
mockMvc
.perform(post("/addContact", aContact))
.andExpect(status().is(302))
.andReturn();
}
@Test
public void testAddContactOccasionHappyPath() throws Exception {
// implement this
}
}