【发布时间】:2018-12-01 01:42:49
【问题描述】:
希望能帮助找出这些测试引发错误的原因..
我编写了一个 Spring Boot RESTful 应用程序。我有使用 JUnit 编写的集成测试(从 application.properties 读取配置)
控制器调用一个包含数据库访问的类
测试控制器工作正常..当我尝试直接测试数据库访问类时..我得到一个 BindException Failed to bind properties under 'spring.datasource'
@RequestMapping("/{source}/product")
Result prodBySource(@PathVariable String source, @RequestParam String prodId) throws Exception {
logger.debug(String.format("GET Product %s from %s. ", source, prodId)) ;
return productDb.getProd(prodId);
}
productDb 有
protected String getJson(String prodId) {
MapSqlParameterSource params = new MapSqlParameterSource()
.addValue("product_id", prodId);
String jsonString = "{}" ;
String jsonString = jdbcTemplate.queryForObject(query, params,
(rs, rowNum) -> {
return rs.getString("json_data") ;
});
return jsonString;
}
public Result getProd(String prodId) {
String jsonString = getJson(prodId);
Object jsonNode = JsonPath.parse(jsonString).json();
Result res = new Result(prodId, jsonNode) ;
return res;
}
JUnit 测试有
@RunWith(SpringRunner.class)
@SpringBootTest(classes= {ProductDb.class, AppConfig.class})
@TestPropertySource(locations =
"classpath:/com/broadridge/adc/fasttests/product/application-
fasttests.properties")
@ConfigurationProperties(prefix="spring.datasource")
@Category(com.broadridge.adc.product.FastTests.class)
public class ProductDbTest implements FastTests {
@Autowired
ProductDb lipperProductDb ;
@Test
public void getJson() throws Exception {
Result json =
lipperProductDb.getProd("30027978") ;
Supplier<String> messageSupplier = () -> {
return "Json can't be null";
};
Assert.notNull(json, messageSupplier);
}
【问题讨论】:
标签: java spring spring-boot junit