【问题标题】:Spring JUnit tests .. autowire not workingSpring JUnit 测试 .. 自动装配不起作用
【发布时间】: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


    【解决方案1】:

    可能你是自动装配接口而不是实现类。如果你测试 DAO 层应该模拟存储库/dao 对象。在测试该层期间不应进行实际的数据库调用,而是自动装配实际的 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);
        } 
    

    【讨论】:

    • 在这种情况下,我确实想进行数据库调用。这是一个集成测试。但是,我认为我应该在 Controller 测试中利用该建议,因为我的 DAO 测试运行正常
    • 好的,ProductDb 是接口还是具体类
    【解决方案2】:

    我不知道为什么,但删除 @ConfigurationProperties(prefix="spring.datasource") 解决了问题。

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 2011-04-09
      • 1970-01-01
      • 2012-09-13
      • 2013-07-11
      • 2017-11-30
      • 2020-02-09
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多