Spring JDBC模板操作的jMock测试用例

时间:2018-04-15 21:21:31

标签: unit-testing junit h2 spring-jdbc jmock

我使用Spring和JDBC使用NamedParameterJdbcOperations插入Oracle数据库。当我尝试使用jMock和内存中的h2数据库为此操作编写测试用例时出现错误

CREATE TABLE DIM_ATTRIBUTE
(
  ATTRIBUTE_ID                      VARCHAR2(45)               NOT NULL,
  ATTRIBUTE_NAME                    VARCHAR2(100)              NOT NULL,
  ATTRIBUTE_VALUE                   VARCHAR2(100)              NOT NULL,
  CREATE_TIMESTAMP                  TIMESTAMP(6)         DEFAULT 
  SYSTIMESTAMP          NOT NULL,
  UPDATE_TIMESTAMP                  TIMESTAMP(6)         DEFAULT 
  SYSTIMESTAMP          NOT NULL
);

我使用Spring插入的查询:

 INSERT INTO DIM_ATTRIBUTE(ATTRIBUTE_ID,ATTRIBUTE_NAME,CREATE_TIMESTAMP,
 UPDATE_TIMESTAMP) VALUES('ATTR'||to_char(ATTRIBUTE_ID_SEQ.nextval,'
 'FM000000000'),:attributeName,SYSTIMESTAMP,SYSTIMESTAMP)

Spring Java DAO类:

public boolean addAttribute(AttributeRequest attributeRequest) {
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("attributeName", attributeRequest.getAttributeName());
        int rowsAffected = jdbcOperations.update(DBQueries.INSERT_DIM_ATTRIBUTE, paramMap);
        if (rowsAffected == 0) {
            return false;
        }
        return true;
}

jMock类:

public class AttributeDaoImplTest {
 @Configuration
 static class SpringConfiguration {
    @Bean
    public FactoryBean<DataSource> dataSource() {
        return new InMemoryDatabaseFactoryBean("db/schema.sql", "dao/attribute/AttributeDaoImplTest-data.sql");
    }

    @Bean
    public AttributeDao attributeDao(DataSource dataSource) {
        return new AttributeDaoImpl(dataSource);
    }

    @Bean
    public NamedParameterJdbcOperations jdbcOperations(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }
 }

 @Resource
 private AttributeDao dao;

 @Resource
 private NamedParameterJdbcOperations jdbcOperations;

 @Test
 public void addAttribute() {
    AttributeRequest attributeRequest=new AttributeRequest("test");
    dao.addAttribute(attributeRequest);
 } 
}

堆栈追踪:

  

org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback;错误的SQL语法[INSERT INTO DIM_ATTRIBUTE(ATTRIBUTE_ID,ATTRIBUTE_NAME,CREATE_TIMESTAMP,UPDATE_TIMESTAMP)VALUES(&#39; ATTR&#39; || to_char(ATTRIBUTE_ID_SEQ.nextval,&#39; FM000000000&#39;),?,SYSTIMESTAMP,SYSTIMESTAMP) ]。嵌套异常是org.h2.jdbc.JdbcSQLException:SQL语句中的语法错误&#34; INSERT INTO DIM_ATTRIBUTE(ATTRIBUTE_ID,ATTRIBUTE_NAME,CREATE_TIMESTAMP,UPDATE_TIMESTAMP)值(&#39; ATTR&#39; || TO_CHAR(ATTRIBUTE_ID_SEQ.NEXTVAL, [*]&#39; FM000000000&#39;),?,SYSTIMESTAMP,SYSTIMESTAMP)&#34 ;;预期&#34;标识符&#34 ;; SQL语句:

0 个答案:

没有答案