Java 如何测试仅调用存储库(dao)层的CRUD服务?

Java 如何测试仅调用存储库(dao)层的CRUD服务?,java,spring,hibernate,unit-testing,integration-testing,Java,Spring,Hibernate,Unit Testing,Integration Testing,例如,我们有一个简单调用JpaRepository方法的服务层。常见积垢 public List<User> findAll() { return userRepository.findAll(); } upd: 好的,findAll()是一个简单的例子,当我们使用 when(userRepository.findAll()).thenReturn(usersList); 我们实际上只是在测试覆盖率,测试显而易见的事情。 还有一个问题 我们需要测试这种服务方法吗? 它只在

例如,我们有一个简单调用JpaRepository方法的服务层。常见积垢

public List<User> findAll() {
    return userRepository.findAll();
}
upd:

好的,findAll()是一个简单的例子,当我们使用

when(userRepository.findAll()).thenReturn(usersList);
我们实际上只是在测试覆盖率,测试显而易见的事情。

还有一个问题

我们需要测试这种服务方法吗?


它只在模拟存储库时调用dao层的方法,然后可以执行以下操作:

List<User> users = Collections.singletonList(new User()); // or you can add more
when(userRepository.findAll()).thenReturn(users);

List<User> usersResult = userService.findAll();

assertThat(usersResult).isEqualTo(users); // AssertJ api
List users=Collections.singletonList(new User());//或者你可以添加更多
when(userRepository.findAll())。然后返回(users);
List usersResult=userService.findAll();
assertThat(usersResult).isEqualTo(users);//AssertJ api
我的做法是

class UserRepository {
  public List<User> findAll(){
    /*
    connection with DB to find and return all users.
    */
  }
} 

class UserService {
  private UserRepository userRepository;

  public UserService(UserRepository userRepository){
    this.userRepository = userRepository;
  }

  public List<User> findAll(){
    return this.userRepository.findAll();
  }
}   

class UserServiceTests {
    /* Mock the Repository */
    private UserRepository userRepository = mock(UserRepository.class);
    /* Provide the mock to the Service you want to test */
    private UserService userService = new UserService(userRepository);
    private User user = new User();

    @Test
    public void TestfindAllInvokeUserServiceMethod(){
      /* this will replace the real call of the repository with a return of your own list created in this test */
      when(userRepository.findAll()).thenReturn(Arrays.asList(user));
      /* Call the service method */
      List<User> users = userService.findAll();

      /*
      Now you can do some Assertions on the users
      */
    }
}
类用户存储库{
公共列表findAll(){
/*
与数据库连接以查找并返回所有用户。
*/
}
} 
类用户服务{
私有用户存储库用户存储库;
公共用户服务(用户存储库用户存储库){
this.userRepository=userRepository;
}
公共列表findAll(){
返回此.userRepository.findAll();
}
}   
类UserServiceTests{
/*模拟存储库*/
私有UserRepository UserRepository=mock(UserRepository.class);
/*为要测试的服务提供模拟*/
private UserService UserService=new UserService(userRepository);
私有用户=新用户();
@试验
public void TestfindAllInvokeUserServiceMethod(){
/*这将用您自己在该测试中创建的列表的返回来替换存储库的实际调用*/
when(userRepository.findAll()).thenReturn(Arrays.asList(user));
/*调用服务方法*/
List users=userService.findAll();
/*
现在您可以对用户执行一些断言
*/
}
}

是否要在不访问数据库的情况下测试服务层,还是要在连接到数据库的情况下测试存储库层?如果没有数据库,请使用mock
class UserRepository {
  public List<User> findAll(){
    /*
    connection with DB to find and return all users.
    */
  }
} 

class UserService {
  private UserRepository userRepository;

  public UserService(UserRepository userRepository){
    this.userRepository = userRepository;
  }

  public List<User> findAll(){
    return this.userRepository.findAll();
  }
}   

class UserServiceTests {
    /* Mock the Repository */
    private UserRepository userRepository = mock(UserRepository.class);
    /* Provide the mock to the Service you want to test */
    private UserService userService = new UserService(userRepository);
    private User user = new User();

    @Test
    public void TestfindAllInvokeUserServiceMethod(){
      /* this will replace the real call of the repository with a return of your own list created in this test */
      when(userRepository.findAll()).thenReturn(Arrays.asList(user));
      /* Call the service method */
      List<User> users = userService.findAll();

      /*
      Now you can do some Assertions on the users
      */
    }
}