【问题标题】:JUnit testing with H2 database -java.lang.AssertionError使用 H2 数据库进行 JUnit 测试 -java.lang.AssertionError
【发布时间】:2018-09-12 15:30:03
【问题描述】:

我正在尝试使用 h2 数据库为 Spring Boot 服务实现编写 Junit,但得到“java.lang.AssertionError”,请帮助并让我知道我在哪里做错了

请在下面找到项目文件: src/test/resources/test.properties

spring.datasource.initialize=true
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;sql.syntax_ora=true;
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.platform=h2
spring.datasource.continueOnError=true

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.continue-on-error=true
spring.batch.initializer.enabled=true

src/test/resources/schema-h2.sql:

SET DATABASE SQL SYNTAX ORA TRUE;

CREATE TABLE INLAND_CY (
    HUB_GEOID VARCHAR2(13) NOT NULL PRIMARY KEY,
    EFFECTIVE_DT TIMESTAMP,
    EXPIRY_DT TIMESTAMP,
    CREATED_DT TIMESTAMP,
    CREATED_BY  VARCHAR2(20) DEFAULT 'SYSTEM',
    LAST_UPDATED_DT TIMESTAMP,
    LAST_UPDATED_BY VARCHAR2(10)
    );


Insert into INLAND_CY (HUB_GEOID,EFFECTIVE_DT,EXPIRY_DT,CREATED_DT,LAST_UPDATED_DT,LAST_UPDATED_BY) values ('12345',to_date('01-01-2001','DD-MM-YYYY'),to_date('01-01-2099','DD-MM-YYYY'),to_date('01-01-2018','DD-MM-YYYY'),to_date('01-01-2018','DD-MM-YYYY'),'UPSTREAM');
Insert into INLAND_CY (HUB_GEOID,EFFECTIVE_DT,EXPIRY_DT,CREATED_DT,LAST_UPDATED_DT,LAST_UPDATED_BY) values ('12346',to_date('01-01-2001','DD-MM-YYYY'),to_date('01-01-2099','DD-MM-YYYY'),to_date('01-01-2018','DD-MM-YYYY'),to_date('01-01-2018','DD-MM-YYYY'),'UPSTREAM');
Insert into INLAND_CY (HUB_GEOID,EFFECTIVE_DT,EXPIRY_DT,CREATED_DT,LAST_UPDATED_DT,LAST_UPDATED_BY) values ('12347',to_date('01-01-2001','DD-MM-YYYY'),to_date('01-01-2099','DD-MM-YYYY'),to_date('01-01-2018','DD-MM-YYYY'),to_date('01-01-2018','DD-MM-YYYY'),'UPSTREAM');

TestConfig.java 文件: 导入 javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ActiveProfiles;

@TestConfiguration
@ActiveProfiles("test")
public class TestConfig {

    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource readDataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        EmbeddedDatabase db = builder
                .setType(EmbeddedDatabaseType.HSQL) //.H2 or .DERBY
                .addScript("/schema-h2.sql")
                .build();
        return db;
    }

    @Bean
    public JdbcTemplate readJdbcTemplate(DataSource readDataSource) {
        return new JdbcTemplate(readDataSource);
    }
}

InlandPricingDAOImplTest.class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { TestConfig.class })
@TestPropertySource(locations = "classpath:test.properties")
@ActiveProfiles("test")
public class InlandPricingDAOImplTest {

    @Autowired
    InlandPricingDAOImpl inlandPricingDAOImpl;

    @Test
    public void testdfindgeotest1() throws IOException{
        InlandCY inlandCY=inlandPricingDAOImpl.findByHubgeoid("12346");
        System.out.println(inlandCY);
        assertNotNull(inlandCY);

    }

}

InlandPricingDAOImpl.class:

@Service
public class InlandPricingDAOImpl implements InlandPricingDAO {
    @Autowired
    private DataSource datasource;

    @Autowired
    InlandPricingRepository inlandPricingRepository;

    private JdbcTemplate jdbcTemplate;
    private InlandCY inlandCY;

    public void setDatasource(DataSource datasource) {
        this.datasource = datasource;
        this.jdbcTemplate = new JdbcTemplate(datasource);
    }

    @Override
    public InlandCY findByHubgeoid(String hubgeoid) {
        return inlandPricingRepository.findByHubgeoid(hubgeoid);
    }
}

InlandPricingRepository 接口: @Repository 公共接口 InlandPricingRepository 扩展 CrudRepository { 公共 InlandCY findByHubgeoid(字符串 hubgeoid); }

我从服务中获取空值,我认为 sql 脚本没有运行并在 h2 表中添加数据。请帮助和协助。

【问题讨论】:

    标签: spring-boot junit mockito h2


    【解决方案1】:

    我猜你没有在 TestConfiguration 类中定义你的服务 Bean。如果 Bean 未定义,则无法进行自动装配。

    【讨论】:

    • 感谢您的回复,但如果可能的话,请您详细介绍一下,因为我对 Spring Boot 很陌生。
    • 多看看你的实际实现会很有帮助。我想给出了以下先决条件:InlandPricingDAOImpl 类有一个@Component 注释。然后你应该在你的 testConfiguration 类中添加一个组件扫描,比如@ComponentScan("your.package"),这样就可以找到 Bean。
    • 嗨克里斯蒂安,我已经用 impl 类更新了这个问题。请检查我是否正在正确编写测试配置文件,因为在 h2 表中数据未插入。谢谢
    • 所以您的服务本身可以自动连接,但是数据库中的数据丢失了?如果您需要在测试之前运行 sql 脚本,请将 @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:schema-h2.sql") 添加到您的 InlandPricingDAOImplTest 类
    猜你喜欢
    • 2015-11-20
    • 2020-03-20
    • 2013-07-13
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2019-05-17
    相关资源
    最近更新 更多