使用H2数据库进行JUnit测试-java.lang.AssertionError

时间:2018-04-03 10:29:32

标签: spring-boot junit mockito h2

我试图用h2数据库为Spring启动服务实现编写Junit,但是得到" java.lang.AssertionError",请帮助并让我知道我在哪里做错了

请在下面找到项目文件: 的src /测试/资源/ 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 /测试/资源/架构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文件:     import 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 {         public InlandCY findByHubgeoid(String hubgeoid);     }

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

1 个答案:

答案 0 :(得分:0)

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