[英]Spring JUnit tests .. autowire not working
Hoping for some help discovering why these tests throw errors.. 希望对发现这些测试为何引发错误的方法有所帮助。
I've written a Spring Boot RESTful app. 我已经编写了一个Spring Boot RESTful应用程序。 I have integration tests written in JUnit which work (reading the configuration from application.properties)
我有用JUnit编写的有效的集成测试(从application.properties中读取配置)
The Controller calls a class which contains the database access Controller调用一个包含数据库访问权限的类
Testing the controller works fine.. when I try to test the database access class directly.. I get a BindException Failed to bind properties under 'spring.datasource' 测试控制器工作正常。.当我尝试直接测试数据库访问类时。.我收到BindException绑定“ 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);
}
The productDb has 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;
}
The JUnit test has 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);
}
May be you are autowiring interface rather than a implementation class. 可能是您正在自动装配接口,而不是实现类。 If you test DAO Layer should mock repository/dao object.
如果您测试DAO,则Layer应该模拟存储库/ dao对象。 No actual database call should be made during testing that layer rather autowiring the actual dao object you should mock the ProductDb and use Mockito when(), thenReturn().
在测试该层期间,不应进行任何实际的数据库调用,而是自动装配实际的dao对象,您应该模拟ProductDb并使用Mockito when()和thenReturn()。
@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 {
@Mock
ProductDb lipperProductDb ;
@Test
public void getJson() throws Exception {
Result resultJson = new Result()//some result object
when(lipperProductDb.getProd("30027978")).thenReturn(resultJson);
Result json =
lipperProductDb.getProd("30027978") ;
Supplier<String> messageSupplier = () -> {
return "Json can't be null";
};
Assert.notNull(json, messageSupplier);
}
我不知道为什么,但是删除@ConfigurationProperties(prefix="spring.datasource")
解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.