【发布时间】:2020-11-27 11:52:29
【问题描述】:
这里有一点 Spring Boot 新手,非常感谢任何帮助!
我已经使用 JDBCTemplate 构建了一个 Spring Boot 应用程序,该应用程序可以正常运行,没有错误或异常。
我已经制作了一个测试类,想使用@JdbcTest 来测试我的Dao 对象。但是,每次我运行测试时,我都会得到java.lang.IllegalStateException: Failed to load ApplicationContext。我的 Controller 类似乎有问题。此 IllegalStateException 是由以下原因引起的:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetApiController' defined in file [/PATH/TO/FILE]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
我的测试课:
@JdbcTest
@Sql({"schema.sql", "test-data.sql"})
class AssetApiControllerTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
AssetDao assetDao;
@Test
void delete() throws DataAccessException {
assetDao.setJdbcTemplate(jdbcTemplate);
assetDao.deleteByPk(new AssetKey(1), null);
assertEquals(0, assetDao.selectAll(null).size());
}
}
我的道:
@Repository("assetDao")
public class AssetDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void deleteByPk (AssetKey assetKey, Connection con) throws DataAccessException {
jdbcTemplate.update("DELETE FROM Asset WHERE id = ? ", assetKey.getId());
}
}
我的控制器类:
@Controller
public class AssetApiController implements AssetApi {
private static final Logger log = LoggerFactory.getLogger(AssetApiController.class);
private final ObjectMapper objectMapper;
private final HttpServletRequest request;
@Autowired
private AssetDao assetDao;
@org.springframework.beans.factory.annotation.Autowired
public AssetApiController(ObjectMapper objectMapper, HttpServletRequest request) {
this.objectMapper = objectMapper;
this.request = request;
}
@Override
public ResponseEntity<Void> delete(@RequestBody AssetKey assetKey) {
try{
assetDao.deleteByPk(assetKey, null);
} catch (ApplicationException e) {
log.warn(e.getFormattedMessage(), e);
return new ResponseEntity<Void>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<Void>(HttpStatus.OK);
}
}
我的杰克逊配置:
@Configuration
public class JacksonConfiguration {
@Bean
@ConditionalOnMissingBean(ThreeTenModule.class)
ThreeTenModule threeTenModule() {
ThreeTenModule module = new ThreeTenModule();
module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
return module;
}
}
另外两点可以帮助诊断:
-
我遵循了这个答案:https://stackoverflow.com/a/32842962/11853066。它解决了这个问题,但后来我得到了 HttpServletRequest 的等效错误:
No qualifying bean found for dependency [javax.servlet.http.HttpServletRequest]。所以根本问题没有得到解决。 -
当我尝试为 Asset 以外的实体(例如,带有 SupplierController、SupplierDao 的“供应商”)生成测试时,我得到了同样的错误:
Error creating bean with name 'assetApiController' defined in file。这一定是因为assetApiController是第一个被扫描的控制器,因为字母顺序?
【问题讨论】:
标签: java spring-boot junit jdbctemplate