在Spring Boot中编写单元测试时,由于转换器(converter)类而出现错误。

huangapple go评论104阅读模式
英文:

Write unit test in spring boot but get error because of converter class

问题

我使用转换器类,单元测试中出现了空指针异常错误。但是,我使用 return accountDto 替代了转换器类,测试通过了。代码在 Postman 中运行正常。请给我一些建议。

@Test
void store() {
    Date date = new Date();
    Account accountMock = mock(Account.class);
    AccountDto accountDto = new AccountDto();
    accountDto.setId(randomUUID);
    accountDto.setName("Test-Name");
    accountDto.setSurname("Test-Lastname");
    accountDto.setEmail("Test-Email");
    accountDto.setBirth_date(date);
    accountDto.setPassword("Test-Email");
    accountDto.setStatus(OPEN);

    when(accountMock.getId())
       .thenReturn(String.valueOf(randomUUID));
    when(accountRepository.save(ArgumentMatchers.any(Account.class)))
       .thenReturn(accountMock);

    AccountDto result = accountService.store(accountDto);

    assertEquals(result.getName(), accountDto.getName());
    assertEquals(result.getId(), String.valueOf(randomUUID));
}

Service 方法 =>

@Transactional
public AccountDto store(AccountDto accountDto) {
    Account account = new Account();
    account.setName(accountDto.getName());
    account.setSurname(accountDto.getSurname());
    account.setEmail(accountDto.getEmail());
    account.setBirth_date(accountDto.getBirth_date());
    account.setPassword(accountDto.getPassword());
    account.setStatus(accountDto.getStatus());
    final Account accountDb = repository.save(account);
    accountDto.setId(accountDb.getId());

    return converter.convertFromEntity(accountDb);
}

转换器类 =>

/**
 * 转换器:从实体到DTO的转换。
 *
 * @param entity 领域实体
 * @return DTO表示形式 - 转换函数在领域实体上的应用结果。
 */
public final T convertFromEntity(final U entity) {
    return fromEntity.apply(entity);
}

错误 =>

java.lang.NullPointerException
	at com.kablanfatih.tddexample.converter.Converter.convertFromEntity(Converter.java:51)
	at com.kablanfatih.tddexample.service.impl.AccountServiceImpl.store(AccountServiceImpl.java:42)
	at com.kablanfatih.tddexample.service.impl.AccountServiceImplTest.store(AccountServiceImplTest.java:87)
英文:

I use converter class,I get null pointer exception error in unit test.
But I use return accountDto instead of converter class
it's running.
Code is running at the Postman.
Please give me some advise.

@Test
void store() {
    Date date = new Date();
    Account accountMock = mock(Account.class);
    AccountDto accountDto = new AccountDto();
    accountDto.setId(randomUUID);
    accountDto.setName("Test-Name");
    accountDto.setSurname("Test-Lastname");
    accountDto.setEmail("Test-Email");
    accountDto.setBirth_date(date);
    accountDto.setPassword("Test-Email");
    accountDto.setStatus(OPEN);
 
    when(accountMock.getId())
       .thenReturn(String.valueOf(randomUUID));
    when(accountRepository.save(ArgumentMatchers.any(Account.class)))
       .thenReturn(accountMock);
    
    AccountDto result = accountService.store(accountDto);
    
    assertEquals(result.getName(), accountDto.getName());
    assertEquals(result.getId(), String.valueOf(randomUUID));
}

Service Method =>

@Transactional
public AccountDto store(AccountDto accountDto) {
    Account account = new Account();
    account.setName(accountDto.getName());
    account.setSurname(accountDto.getSurname());
    account.setEmail(accountDto.getEmail());
    account.setBirth_date(accountDto.getBirth_date());
    account.setPassword(accountDto.getPassword());
    account.setStatus(accountDto.getStatus());
    final Account accountDb = repository.save(account);
    accountDto.setId(accountDb.getId());

    return converter.convertFromEntity(accountDb);
}

Converter class =>

 /**
 * Converts Entity to DTO.
 *
 * @param entity domain entity
 * @return The DTO representation - the result of the converting function application on domain
 * entity.
 */
public final T convertFromEntity(final U entity) {
    return fromEntity.apply(entity);
}

Error =>

java.lang.NullPointerException
	at com.kablanfatih.tddexample.converter.Converter.convertFromEntity(Converter.java:51)
	at com.kablanfatih.tddexample.service.impl.AccountServiceImpl.store(AccountServiceImpl.java:42)
	at com.kablanfatih.tddexample.service.impl.AccountServiceImplTest.store(AccountServiceImplTest.java:87)

答案1

得分: 2

你有两个选择在我看来。首先,你可以对你的Converter类进行模拟,或者第二,你可以正确地初始化你的转换器,并在你的测试中使用它。我更喜欢第一种解决方案,因为我可以在单元测试的上下文中更好地控制Converter的行为。

英文:

You have two options in my opinion. First you can mock your Converter class or second you initialize your converter properly and use it in your test. I prefer the first solution because I can better control the behaviour of the Converter in the context of your unit test.

答案2

得分: 0

我现在理解的是,您是在编写代码之后编写测试,因此您陷入了这个问题中。

推荐的解决方案应该是模拟转换器类,控制响应并单独测试转换器方法。这就是测试驱动开发的工作原理。


@Mock
Converter converter;

when(converter.yourMockedMthord).thenReturn(your_output);

您可以编写ConverterTest.java类来测试方法的行为。

英文:

What I am getting here is that you are writing tests after writing code so you landed in this issue.

Recommended solution should be mocking converter class and control the response and test converter method separately. This is how TDD works.

    @Mock
    Converter converter;

    when(converter.yourMockedMthord).thenReturn(your_output);

And you can write ConverterTest.java class to test behaviour of method.

huangapple
  • 本文由 发表于 2020年3月4日 04:47:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/60515271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定