拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 我无法在SpringBootTest中自动装配服务类

我无法在SpringBootTest中自动装配服务类

白鹭 - 2022-01-25 1936 0 0

我创建了使用 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;
   }

   //...

}

当我开始测验时,我得到下一个错误:

我该如何解决我的问题?

uj5u.com热心网友回复:

由于您注入DaoImpl了服务类,因此您可能打算模拟DaoImpl而不是Dao

@SpringBootTest
public class ServiceTest {
   @MockBean
   private DaoImpl daoImpl;
   ...
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *