【问题标题】:Junit test Assertion Error expected 3 but came with 0Junit 测试断言错误预期为 3,但附带 0
【发布时间】:2019-11-15 07:03:34
【问题描述】:

我正在使用带有单元测试的微服务。我有 Dal 服务,它保存在数据库上,另一个帐户服务应该保存在 Dal 服务上。当我测试我的代码时,看起来我的代码可以正常工作,即:出现蓝色标志,但表示我的逻辑不正确。所以在这个场景中,我期望得到 3 的大小,但是我收到的大小是 0

有什么建议吗?

感谢回答

    public class AccountEntity {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column(nullable = false)
        private String Address;

        @Column(nullable = false)
        private String Password;

        @Column(nullable = false)
        private String authorizedPersonCode;

        @Column(nullable = false)
        private LocalDateTime creationDate;

        @Column(nullable = false)
        private boolean isActive;
    private static final String SAVE_ACCOUNT_URL = "http://localhost:8443/api/v1/accounts";

        @Before
        public void AccountInit() {

            AccountModel AccountModel1 = AccountModel.builder()
                    .Address("f.k@account.com.tr").Password("Try2017")
                    .authorizedPersonCode("").creationDate(LocalDateTime.now()).isActive(true).build();


            HttpEntity<AccountModel> httpEntity1 = new HttpEntity<AccountModel>(AccountModel1);

            restTemplate.postForEntity(SAVE_ACCOUNT_URL, httpEntity1, AccountModel.class);


            AccountModel AccountModel2 = AccountModel.builder()
                    .Address("fk@tr").Password("2017")
                    .authorizedPersonCode("").creationDate(LocalDateTime.now()).isActive(false).build();

            HttpEntity<AccountModel> httpEntity2 = new HttpEntity<AccountModel>(AccountModel2);

            restTemplate.postForEntity(SAVE_ACCOUNT_URL, httpEntity2, AccountModel.class);


            AccountModel AccountModel3 = AccountModel.builder()
                    .Address("fu@..com.tr").Password("Ve7")
                    .authorizedPersonCode("").creationDate(LocalDateTime.now()).isActive(true).build();

            HttpEntity<AccountModel> httpEntity3 = new HttpEntity<AccountModel>(AccountModel3);

            restTemplate.postForEntity(SAVE_ACCOUNT_URL, httpEntity3, AccountModel.class);
        }

        @Test
        public void testGetAccountsFromDalSuccess() {

            PagedResources<AccountModel> resultAccounts = DelegatorService.getAccounts();

            Assert.assertEquals(3, resultAccounts.getContent().size());

        }
public PagedResources<AccountModel> getAccounts() { 
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(AccountDalURL);
    ResponseEntity<PagedResources<AccountModel>> accountModel = restTemplate.exchange(
                                  uriComponentsBuilder.build().toUri(), 
                                  HttpMethod.GET, 
                                  null, 
                                  new ParameterizedTypeReference<PagedResources<AccountModel>>() { }
                              ); 

    if (accountModel.getStatusCode().equals(HttpStatus.OK)) { 
        return accountModel.getBody(); 
    } else { 
        return null; 
    } 
}

【问题讨论】:

    标签: java rest spring-boot junit geojson


    【解决方案1】:

    您的单元测试似乎在进行 RESTful Web 服务调用。这可能会失败,因为它实际上是一个集成测试,并且需要一个测试工具,以便容器(或其他)可以引导和启动您的 web 服务(例如,如果使用 Spring,则使用 SpringRunner)并正确创建/注入您的 restTemplate 等。

    如果您尝试进行单元测试,则不应依赖外部服务(数据库、Web 服务、文件系统等)进行测试。

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多