Junit 测试:Repository 的 findById 方法
Junit Test: findById method of the Repository
我是 Junit 测试的新手,对此有疑问。在这里你可以在我的服务class:
中看到方法findById
@Service
public class DefaultQuarterService implements QuarterService {
private final QuarterRepository quarterRepository;
public DefaultQuarterService(QuarterRepository quarterRepository) {
this.quarterRepository = quarterRepository;
}
@Override
public QuarterEntity findById(int id) {
return quarterRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(String.format("Quarter does not exist for id = %s!", id)));
}
}
这是我的 QuarterRepository:
@Repository
public interface QuarterRepository extends CrudRepository<QuarterEntity, Integer> {
}
这是我对此方法的 Junit 实现:
@MockBean
private QuarterRepository quarterRepository;
@Test
public void throwExceptionWhenQuarterIdNotFound() {
int id = anyInt();
when(quarterRepository.findById(id))
.thenReturn(Optional.empty());
assertThatAnExceptionWasThrown(String.format("Quarter does not exist for id = %s!", id));
}
public void assertThatAnExceptionWasThrown(
String errorMsg
) {
expectException.expect(RuntimeException.class);
expectException.expectMessage(errorMsg);
}
不幸的是,测试没有通过。这里是终端错误:
java.lang.AssertionError: Expected test to throw (an instance of
java.lang.RuntimeException and exception with message a string
containing "Quarter does not exist for id = 0!")
也许就是这么简单,但我看不出我错过了什么。如果你能指导我,我会很高兴。非常感谢!
当你模拟你的存储库时,它会 return 和 Optional.empty()
正确,我认为你应该调用你的服务(自动连接)的 findById
方法。它实际上会抛出异常。
第一期
在 assertThatAnExceptionWasThrown
方法中你期望 RuntimeException
但是 在服务 class 中你抛出 EntityNotFoundException,所以我想你应该期望 EntityNotFoundException
在你的测试用例中。
第二期
这部分代码之后。
when(quarterRepository.findById(id))
.thenReturn(Optional.empty());
你为什么不调用你的服务方法(findById)
?
当您返回空值时,您应该使用您想要测试它的服务方法来验证您的条件。
应该是这样的。
assertThatThrownBy(() -> defaultQuarterService.findById(id))
.isInstanceOf(ApiRequestException.class)
.hasMessageContaining("PUT_YOUR_EXCEPTION_MESSAGE_HERE");
这是一个很好的 spring 引导单元测试示例。你可以检查一下。 Link
尝试上述解决方案,让我知道它是否已修复。祝你好运
我是 Junit 测试的新手,对此有疑问。在这里你可以在我的服务class:
中看到方法findById@Service
public class DefaultQuarterService implements QuarterService {
private final QuarterRepository quarterRepository;
public DefaultQuarterService(QuarterRepository quarterRepository) {
this.quarterRepository = quarterRepository;
}
@Override
public QuarterEntity findById(int id) {
return quarterRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(String.format("Quarter does not exist for id = %s!", id)));
}
}
这是我的 QuarterRepository:
@Repository
public interface QuarterRepository extends CrudRepository<QuarterEntity, Integer> {
}
这是我对此方法的 Junit 实现:
@MockBean
private QuarterRepository quarterRepository;
@Test
public void throwExceptionWhenQuarterIdNotFound() {
int id = anyInt();
when(quarterRepository.findById(id))
.thenReturn(Optional.empty());
assertThatAnExceptionWasThrown(String.format("Quarter does not exist for id = %s!", id));
}
public void assertThatAnExceptionWasThrown(
String errorMsg
) {
expectException.expect(RuntimeException.class);
expectException.expectMessage(errorMsg);
}
不幸的是,测试没有通过。这里是终端错误:
java.lang.AssertionError: Expected test to throw (an instance of java.lang.RuntimeException and exception with message a string containing "Quarter does not exist for id = 0!")
也许就是这么简单,但我看不出我错过了什么。如果你能指导我,我会很高兴。非常感谢!
当你模拟你的存储库时,它会 return 和 Optional.empty()
正确,我认为你应该调用你的服务(自动连接)的 findById
方法。它实际上会抛出异常。
第一期
在 assertThatAnExceptionWasThrown
方法中你期望 RuntimeException
但是 在服务 class 中你抛出 EntityNotFoundException,所以我想你应该期望 EntityNotFoundException
在你的测试用例中。
第二期
这部分代码之后。
when(quarterRepository.findById(id))
.thenReturn(Optional.empty());
你为什么不调用你的服务方法(findById)
?
当您返回空值时,您应该使用您想要测试它的服务方法来验证您的条件。
应该是这样的。
assertThatThrownBy(() -> defaultQuarterService.findById(id))
.isInstanceOf(ApiRequestException.class)
.hasMessageContaining("PUT_YOUR_EXCEPTION_MESSAGE_HERE");
这是一个很好的 spring 引导单元测试示例。你可以检查一下。 Link
尝试上述解决方案,让我知道它是否已修复。祝你好运