jMock test case for Spring JDBC template operations

143 views Asked by At

I am using Spring and JDBC to insert into an Oracle Database using NamedParameterJdbcOperations. I got an error when I tried to write a test case for this operation using jMock and in-memory h2 database

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
);

My query to insert using 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 class:

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 class:

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);
 } 
}

Stack trace:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO DIM_ATTRIBUTE(ATTRIBUTE_ID,ATTRIBUTE_NAME,CREATE_TIMESTAMP,UPDATE_TIMESTAMP) VALUES ('ATTR'||to_char(ATTRIBUTE_ID_SEQ.nextval,'FM000000000'),?,SYSTIMESTAMP,SYSTIMESTAMP)]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO DIM_ATTRIBUTE(ATTRIBUTE_ID,ATTRIBUTE_NAME,CREATE_TIMESTAMP,UPDATE_TIMESTAMP) VALUES ('ATTR'||TO_CHAR(ATTRIBUTE_ID_SEQ.NEXTVAL,[*]'FM000000000'),?,SYSTIMESTAMP,SYSTIMESTAMP) "; expected "identifier"; SQL statement:

0

There are 0 answers