小编Ale*_*ksa的帖子

Spring Boot 服务层:单元测试还是集成测试?

我有一个服务:

@Service
@Transactional
@RequiredArgsConstructor
public class BookService {

    private final BookRepository bookRepository;

    public Book findOne(Long id) {
        return bookRepository.findById(id).orElse(null);
    }

    public Book getOne(Long id) {
        return bookRepository.findById(id)
                .orElseThrow(() -> new BadRequestAlertException("entity-not-found", ("Entity with id: " + id + " not found!")));
    }

    public List<Book> getAll() {
        return bookRepository.findAll();
    }

    public Book save(Book book) {
        return bookRepository.save(book);
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经为数据库(BookRepository)和控制器层(使用 BookService 的 BookController)编写了集成测试。我在任何地方都找不到服务层集成测试的示例。如果我正确编写单元测试,是否有为其编写集成测试的用例?据我所知(这不是规则,而是常见的用例):

  • 控制器 - 集成测试
  • 服务-单元测试
  • 存储库 - 集成测试

java unit-testing spring-boot

2
推荐指数
1
解决办法
2905
查看次数

标签 统计

java ×1

spring-boot ×1

unit-testing ×1