Junit测试断言错误预期为3,但附带0

时间:2019-07-04 14:01:06

标签: java rest spring-boot junit geojson

我正在使用带有单元测试的微服务。我有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; 
    } 
}

1 个答案:

答案 0 :(得分:0)

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

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