0
我正在使用Spring Boot 1.4.4。跟隨Spring Boot Test文章進行單元測試。當我有自定義庫,測試沒有工作,失敗,錯誤UnsatisfiedDependencyException: Error creating bean with name 'com.jay.UserRepositoryTest': Unsatisfied dependency expressed through field 'userRepository';
無法使用Spring Boot測試自定義存儲庫
這裏是我的代碼,
@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引導下道無任何配置變化,
@Transactional
public interface UserDao extends CrudRepository<User, Long> {
User findByEmail(String email);
}
當我使用@SpringBootTest
時,我能夠注入UserRepository
而不是TestEntityManager
。
我認爲這也是相關http://stackoverflow.com/questions/41021902/excluding-spring-integration-from-spring-boot-test-with-spring-boot-1-4?rq=1 – jaks