我創建了使用 jdbc 處理 DB 的 Dao 存盤庫。
我在我的服務類中自動裝配了這個存盤庫。
然后我嘗試在我的測驗類中自動連接我的服務類。
@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。
我該如何解決我的問題?
uj5u.com熱心網友回復:
由于您注入DaoImpl了服務類,因此您可能打算模擬DaoImpl而不是Dao:
@SpringBootTest
public class ServiceTest {
@MockBean
private DaoImpl daoImpl;
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/414768.html
標籤:
上一篇:以Maven的方式呼叫javac
