【发布时间】:2022-01-17 12:34:48
【问题描述】:
我创建了使用 jdbc 处理 DB 的 Dao Repository。
我在我的服务类中自动装配了这个存储库。
然后我尝试在我的测试类中自动装配我的服务类。
@SpringBootTest
public class ServiceTest {
@MockBean
private Dao dao;
@Autowired
private Service service;
@Test
void whenSomething_thanSomething() {
when(Dao.getStatus(anyString())).thenReturn("green");
assertEquals(0, service.getStatus(""));
}
//other tests...
}
@Service
public class Service {
private DaoImpl daoImpl;
@Autowired
public Service(DaoImpl daoImpl) {
this.daoImpl = daoImpl;
}
//...
}
@Repository
public class DaoImpl omplements Dao {
private NamedParameterJdbcOperations jdbc;
@Autowired
public DaoImpl(NamedParametedJdbcOperations jdbc) {
this.jdbc = jdbc;
}
//...
}
当我开始测试时,我得到下一个错误:
Service中构造函数的参数0需要一个找不到的DaoImpl类型的bean。
我该如何解决我的问题?
【问题讨论】:
标签: spring-boot mockito autowired spring-boot-test springmockito