This is my first attempt to do unit test for one of my controllers.
@RunWith(MockitoJUnitRunner.class)
public class AddressControllerUnitTests {
private MockMvc mockMvc;
@Mock
SubmissionService submissionService;
@Mock
MessageSource messageSource;
@InjectMocks
AddressController addressController;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(addressController).build();
}
@Test
public void submitSuccess() throws Exception {
when(submissionService.isEmailAvailable("[email protected]",0)).thenReturn(true);
when(messageSource.getMessage("form.submitted.success", null, Locale.ENGLISH)).thenReturn("Form has been submitted successfully");
this.mockMvc
.perform(post("/address")
.param("address", "my address")
.param("phone", "08123456789")
.param("email", "[email protected]")
.param("userId", "14"))
.andExpect(status().isFound())
.andExpect(flash().attribute("message", "Form has been submitted successfully"))
.andExpect(redirectedUrl("/address"));
}
}
Do I really need to perform expect on the flashAttribute ? Or Should I just remove it completely ? It looks funny that I have to set
when(messageSource.getMessage("form.submitted.success", null, Locale.ENGLISH)).thenReturn("Form has been submitted successfully");
and then
.andExpect(flash().attribute("message", "Form has been submitted successfully"))