I have this simple controller in spring boot with parameter validation:
@ApiOperation(value = "Test")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 400, message = "Bad request")
})
@PutMapping(value = "/test")
@ResponseStatus(value = HttpStatus.OK)
public void test(
@ApiParam(value = "Plus", required = true)
@Pattern(regexp = "^\\+$")
@RequestParam(value = "plus") String plus) {
System.out.println("Validation passed: " + plus);
}
As you can see Plus field only accepts "+" symbol. When I try to run it and fire request on localhost swagger it works. Validation passes, I get HTTP 200 status.
Generated swagger file looks good, validation is there: pattern: "^\\+$"
If I try to run a jUnit test, validation fails, I get 400 Bad Request. In test I use controller that is generated from swagger file:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
@DirtiesContext
public class CeCommissioningBffControllerTest {
@MockBean
private controllerApi api; //This is generated class from swagger file
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test(){
api.test("+");
}
}
Why does it fail in jUnit? I tried to replace '+' with "$" symbol everywhere and amazingly it works then.