0

I have a class:

@Component
public class ContractorFormValidator implements Validator {

    Logger logger = LoggerFactory.getLogger(ContractorFormValidator.class);

    @Inject IBusinessDataValidator businessDataValidator;

    @Override
    public boolean supports(Class<?> clazz) {
        return Contractor.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Contractor contractor = (Contractor) target;
        if (!businessDataValidator.isNipValid(contractor.getContractorData().getNip())) {
            errors.rejectValue("contractorData.nip", "invalid");
        }
        if (!businessDataValidator.isRegonValid(contractor.getContractorData().getRegon())) {
            errors.rejectValue("contractorData.regon", "invalid");
        }
    }
}

How can I test it? I have tried this: How to test validation annotations of a class using JUnit? but this doesn't work cause the validate method in my validator requires Errors class passed to it's method signature.

I have no Idea if I can pass this Errors object to the validator. Is there any other way?

hc0re
  • 1,806
  • 2
  • 26
  • 61

2 Answers2

1

Have you tried to write a simple unit test for this?

@RunWith(SpringJUnit4ClassRunner.class)
public class ContractorFormValidatorTest {

  @Autowired
  private ContractorFormValidator validator;

  @Test
  public void testValidation() throws Exception {
    Contractor contractor = new Contractor();
    // Initialise the variables here.

    Errors errors = new BeanPropertyBindingResult(contractor, "contractor");
    validator.validate(contract, errors);

    // If errors are expected.
    Assert.assertTrue(errors.hasErrors());

   for (Error fieldError : errors.getFieldErrors()) {
       Assert.assertEquals("contractorData.nip", fieldError.getCode());
   }
  }
}

If you are going to use the validator in a controller implementation, then you need to use the MockMvc apis

Your set up can be included in the class above.

private MockMvc mockMvc

@Autowired
private MyController controller;

@Before
public void setUp() throws Exception {
  this.mockMvc = MockMvcBuilders.standaloneSetup(this.controller).build();
}

@Test
public void testMethod() {
   MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.post("/yoururl")).
    andExpect(MockMvcResultMatchers.status().isCreated()).andReturn();
}
Kartik
  • 2,541
  • 2
  • 37
  • 59
  • as I was able to solve the first part on my own (with just a few small differences in the code), the solution for MockMvc is a great suggestion and I will use it for sure, thanks! – hc0re Apr 26 '19 at 15:20
0

Use the class org.springframework.validation.BeanPropertyBindingResult,

Errors newErrors = new BeanPropertyBindingResult(validateObject, "objectName");
Bishal Jaiswal
  • 1,684
  • 13
  • 15
  • It is not working. I get: Required: java.util.Set> Found: void – hc0re Apr 25 '19 at 18:53
  • use the following packages:`import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.validation.Errors;` – Bishal Jaiswal Apr 25 '19 at 19:03
  • Those are the packages I am using, still not working :/ – hc0re Apr 25 '19 at 19:07
  • Errors object is been passed but `Contractor` object is giving the error, you can debug and check at which line you are getting the error, probably errors object is passed correctly but some error might be in the `validate class` – Bishal Jaiswal Apr 25 '19 at 19:12