1

I'm writing test cases for my controller layer. One Of my controller method looks like below.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/v1")
public class MyController {

  private static Logger logger = LoggerFactory.getLogger(MyController.class);

  @Autowired
  private MyService service;

  @GetMapping("/{pathVar}/check")
  @MyAuthentication
  public ResponseEntity<Object> check(@PathVariable String pathVar,
                                         @MyAuthContext MyAuthInfo myAuthInfo) throws Exception {
    try {
      logger.info("Check API started by "+myAuthInfo.getUserId()+" and email "+ myAuthInfo.getUserEmail());
      return ResponseEntity
          .status(HttpStatus.OK)
          .body(service.isPartner(pathVar, myAuthInfo));
    } catch (Exception e) {
      logger.info("Check API encountered the error.", e);
      throw e;
    }
  }
}

Can someone please help to mock my auth context while I'm writing test cases?

Gregor Zurowski
  • 2,222
  • 2
  • 9
  • 15
Mahesh Yadav
  • 240
  • 2
  • 3
  • 13
  • 1
    So basically if you are writing the spring boot test, you can use `@MockBean` for the classes where the functionality of the annotation is coming from. Incase of constraint violation exception, which I feel is in case of @MyAuthContext, you can mock the bean of the validator in that case. – Parth Manaktala Jul 05 '21 at 11:08
  • Were you able to solve the problem? I am always getting null for annotated parameter – Avinash Gupta Jul 08 '21 at 11:53
  • No I'm not able to solve the problem. – Mahesh Yadav Jul 09 '21 at 09:09

0 Answers0