Spring 单元测试弹簧座控制器
我有一个Spring引导控制器,其方法如下:Spring 单元测试弹簧座控制器,spring,junit,Spring,Junit,我有一个Spring引导控制器,其方法如下: // CREATE @RequestMapping(method=RequestMethod.POST, value="/accounts") public ResponseEntity<Account> createAccount(@RequestBody Account account, @RequestHeader(value = "Author
// CREATE
@RequestMapping(method=RequestMethod.POST, value="/accounts")
public ResponseEntity<Account> createAccount(@RequestBody Account account,
@RequestHeader(value = "Authorization") String authorizationHeader,
UriComponentsBuilder uriBuilder) {
if (!account.getEmail().equalsIgnoreCase("")) {
account = accountService.createAccount(account);
HttpHeaders headers = new HttpHeaders();
System.out.println( "Account is null = " + (null == account)); //For debugging
headers.setLocation(uriBuilder.path("/accounts/{id}").buildAndExpand(account.getId()).toUri());
return new ResponseEntity<>(account, headers, HttpStatus.CREATED);
}
return new ResponseEntity<>(null, null, HttpStatus.BAD_REQUEST);
}
@Test
public void givenValidAccount_whenCreateAccount_thenSuccessed() throws Exception {
/// Arrange
AccountService accountService = mock(AccountService.class);
UriComponentsBuilder uriBuilder = mock(UriComponentsBuilder.class);
AccountController accountController = new AccountController(accountService);
Account account = new Account("someone@domain.com");
/// Act
ResponseEntity<?> createdAccount = accountController.createAccount(account, "", uriBuilder);
/// Assert
assertNotNull(createdAccount);
//assertEquals(HttpStatus.CREATED, createdAccount.getStatusCode());
}
//创建
@RequestMapping(method=RequestMethod.POST,value=“/accounts”)
public ResponseEntity createAccount(@RequestBody Account,
@RequestHeader(value=“Authorization”)字符串authorizationHeader,
UriComponents构建器(uriBuilder){
如果(!account.getEmail().equalsIgnoreCase(“”)){
account=accountService.createAccount(account);
HttpHeaders=新的HttpHeaders();
System.out.println(“Account is null=“+(null==Account));//用于调试
headers.setLocation(uriBuilder.path(“/accounts/{id}”).buildAndExpand(account.getId()).toUri());
返回新的响应属性(帐户、标题、HttpStatus.CREATED);
}
返回新的ResponseEntity(null,null,HttpStatus.BAD_请求);
}
我试着像这样进行单元测试:
// CREATE
@RequestMapping(method=RequestMethod.POST, value="/accounts")
public ResponseEntity<Account> createAccount(@RequestBody Account account,
@RequestHeader(value = "Authorization") String authorizationHeader,
UriComponentsBuilder uriBuilder) {
if (!account.getEmail().equalsIgnoreCase("")) {
account = accountService.createAccount(account);
HttpHeaders headers = new HttpHeaders();
System.out.println( "Account is null = " + (null == account)); //For debugging
headers.setLocation(uriBuilder.path("/accounts/{id}").buildAndExpand(account.getId()).toUri());
return new ResponseEntity<>(account, headers, HttpStatus.CREATED);
}
return new ResponseEntity<>(null, null, HttpStatus.BAD_REQUEST);
}
@Test
public void givenValidAccount_whenCreateAccount_thenSuccessed() throws Exception {
/// Arrange
AccountService accountService = mock(AccountService.class);
UriComponentsBuilder uriBuilder = mock(UriComponentsBuilder.class);
AccountController accountController = new AccountController(accountService);
Account account = new Account("someone@domain.com");
/// Act
ResponseEntity<?> createdAccount = accountController.createAccount(account, "", uriBuilder);
/// Assert
assertNotNull(createdAccount);
//assertEquals(HttpStatus.CREATED, createdAccount.getStatusCode());
}
@测试
当CreateCount\u ThenSucessed()引发异常时,public void给予验证帐户\u{
///安排
AccountService=mock(AccountService.class);
UriComponentsBuilder uriBuilder=mock(UriComponentsBuilder.class);
AccountController=新的AccountController(accountService);
帐户=新帐户(“someone@domain.com");
///表演
ResponseEntity createdAccount=accountController.createAccount(account,”,uriBuilder);
///断言
assertNotNull(createdAccount);
//assertEquals(HttpStatus.CREATED,createdAccount.getStatusCode());
}
但是帐户总是空的。你知道为什么吗?我想你首先需要知道
when(accountController.createAccount(account, "", uriBuilder)).then(createAccount);
ResponseEntity<?> createdAccount = accountController.createAccount(account, "", uriBuilder);
when(accountController.createAccount(account,”,uriBuilder))。然后(createAccount);
ResponseEntity createdAccount=accountController.createAccount(account,”,uriBuilder);
我想你首先需要考虑when子句
when(accountController.createAccount(account, "", uriBuilder)).then(createAccount);
ResponseEntity<?> createdAccount = accountController.createAccount(account, "", uriBuilder);
when(accountController.createAccount(account,”,uriBuilder))。然后(createAccount);
ResponseEntity createdAccount=accountController.createAccount(account,”,uriBuilder);
您可以在中查看我的答案
您不仅可以找到单元测试控制器的答案,还可以找到如何在测试中包含过滤器、处理程序和拦截器
希望这有帮助
杰克你可以把我的答案填进去 您不仅可以找到单元测试控制器的答案,还可以找到如何在测试中包含过滤器、处理程序和拦截器 希望这有帮助 Jake
给定(accountService.save(account))。将返回(account)代码>?给定(accountService.save(account))。将返回(account)代码>?