我正在尝试在Junit 5测试中注入spring数据存储库,但是我得到
No qualifying bean of type 'com.xxx.core.security.datastore.AccountsRepository' available: expected at least 1 bean which qualifies as autowire candidate
这是测试
package com.xxx.core.security.datastore;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
class AccountsRepositoryTest {
@Autowired
AccountsRepository accountsRepository;
@BeforeEach
void setUp() {
Assumptions.assumeTrue(true); // how to get spring profile !
}
@Test
public void name() {
}
}
这是我的仓库
package com.xxx.core.security.datastore;
import com.checkit.core.security.datastore.model.Account;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountsRepository extends CrudRepository<Account,Long> {
Account findByName(String name);
}
当我运行应用程序时,spring似乎可以连接一切
答案 0 :(得分:0)
在Junit中,我通过在注释中包含一个配置类和一个测试属性源来解决了这个问题,据我所知,这是不需要的,因为默认情况下是测试配置。
@SpringJUnitConfig(CoreApplication.class)
@TestPropertySource("/application.properties")
class AccountsRepositoryTest {
@Autowired
AccountsRepository accountsRepository;
@BeforeEach
void setUp() {
Assumptions.assumeTrue(true); // how to get spring profile !
}
@Test
public void name() {
}
}