Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 单元测试弹簧座控制器_Spring_Junit - Fatal编程技术网

Spring 单元测试弹簧座控制器

Spring 单元测试弹簧座控制器,spring,junit,Spring,Junit,我有一个Spring引导控制器,其方法如下: // CREATE @RequestMapping(method=RequestMethod.POST, value="/accounts") public ResponseEntity<Account> createAccount(@RequestBody Account account, @RequestHeader(value = "Author

我有一个Spring引导控制器,其方法如下:

// 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)