I'm developing an application using Spring 4.1.7 and I'm performing some unit tests using mockMvc with the standalone context configuration. I want to test a controller method where the form is submitted.
The problem is that some fields in the form are annotated with the custom validation annotation and ConstraintValidator responsible for the validation requires additional dependencies which I want to mock. How can I do it?
The code of the validator is provided below:
public class EmailValidator implements ConstraintValidator<CorrectEmail, Object> {
@Autowired
private ConfigurationManager configurationManager;
@Override
public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
String pattern = configurationManager.getPattern();
//performing checks
}
}
Code of the test is provided below:
@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class RegisterPageControllerUnitTest {
private MockMvc mockMvc;
@InjectMocks
private Controller controller;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void sdlkjf() throws Exception {
mockMvc.perform(post("/register")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("email", "email@gmail.com"));
}
}
When I try to run my test I get NullPointerException
because configurationManager
is not injected.