【发布时间】:2021-02-11 23:10:51
【问题描述】:
我是 JUnit 的新手。我尝试编写一个简单的服务类“ProductService”的单元测试。 这是我对“ProductService”的测试代码
public interface TestProductService {
void shouldCreateProduct() throws EntityIsAlreadyCreatedException;
void shouldDeleteProductById() throws EntityNotfoundException;
void shouldUpdateProduct();
void shouldFindProductById() throws EntityNotfoundException;
void shouldFindAllProducts() throws EntityNotfoundException;
void shouldThrowErrorWhenFindAllProductsWhichAreNotInDB();
void shouldThrowErrorWhenFindProductWhichAreNotInDB() throws EntityNotfoundException;
void shouldThrowErrorWhenDeleteProductWhichIsNotInDB() throws EntityNotfoundException;
void shouldThrowErrorWhenCreateProductWhichIsAlreadyInDB() throws EntityIsAlreadyCreatedException;
}
如您所见,有些函数可能会引发错误。此外,其他功能测试简单的“CRUD”操作。我应该将可能向另一个接口抛出错误的函数分开吗? 如果我将它们分开,我可以提供接口隔离吗? 谢谢你的帮助。
【问题讨论】:
标签: java spring exception junit interface-segregation-principle