无法使用 Spring 引导测试来测试自定义存储库

Unable to test custom repositories using Spring Boot Test

我正在使用 Spring Boot 1.4.4。按照 Spring Boot Test 文章进行单元测试。当我有自定义存储库时,测试不工作并且失败并显示错误 UnsatisfiedDependencyException: Error creating bean with name 'com.jay.UserRepositoryTest': Unsatisfied dependency expressed through field 'userRepository';

这是我的代码,

@Repository
@Transactional
public class UserRepository  {

  @Autowired
  private EntityManager entityManager;

  // sample code for custom repo. can be done easily in CrudRepo
  public User findUser(String name){

    TypedQuery<User> q = entityManager.createQuery("Select u from User u Where u.name = :name", User.class);
    q.setParameter("name", name);

    return q.getSingleResult();
  }
}

@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepositoryTest {

  @Autowired
  private TestEntityManager entityManager;

  @Autowired
  private UserRepository userRepository;

  @Test
  public void findUserTest(){
    ...
  }
}

但我可以在 Spring 启动时测试以下 Dao,无需任何配置更改,

@Transactional
public interface UserDao extends CrudRepository<User, Long> {

  User findByEmail(String email);
}

当我使用 @SpringBootTest 时,我可以注入 UserRepository,但不能注入 TestEntityManager

在 Spring 启动测试的 Github 中发布了相同的问题并得到了答复。参考 this