【问题标题】:Unable to test custom repositories using Spring Boot Test无法使用 Spring Boot Test 测试自定义存储库
【发布时间】:2017-07-17 16:47:32
【问题描述】:

我使用的是 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 Boot 测试以下 Dao,

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

  User findByEmail(String email);
}

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

【问题讨论】:

标签: spring-boot spring-data junit4 spring-boot-test


【解决方案1】:

在 Spring Boot Test 的 Github 上发布了同样的问题并得到了回复。参考this

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 2017-12-03
    • 2015-08-17
    • 2019-12-13
    • 2018-06-26
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多