1

I've defined this method in my Spring MVC Controller

@PreAuthorize("isAuthenticated() and hasPermission(#request, 'CREATE_REQUISITION')")
    @RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
    public @ResponseBody
    void createRequisition(@RequestBody CreateRequisitionRO[] request,
            @RequestHeader("validateOnly") boolean validateOnly) {
.....
}

Then in my TestNG test I'd like to call this method and ensure that the PreAuthorize condition is verified. when I call this method in a normal way (not testing), the PreAuthorize is verified.

If it's possible, how to test this annotation in a TestNG test and how to catch the exception if it throws one ?

Best Regards

Java Questions
  • 7,813
  • 41
  • 118
  • 176
  • http://stackoverflow.com/questions/5403818/how-to-junit-tests-a-preauthorize-annotation-and-its-spring-el-specified-by-a-s – gavenkoa Dec 18 '14 at 16:43

2 Answers2

2

In my opinion you should not worry about testing that in a unit test. You should only test your method.

Instead you should write integration or functional tests for this scenario. Since for this the whole application will be initialized you can test all scenarios.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
2

You "only" need to enable spring security in your tests. - The same way you did in your normal application.

BTW: From my own experience, I strongly recommend to split the tests with enables spring security from the tests without spring security.

Ralph
  • 118,862
  • 56
  • 287
  • 383