简体   繁体   中英

Getting error “expected identifier” while using H2 Database for JUNIT Testing

Am trying to write JUNIT Test cases for my SpringBoot Micro service. I used H2 Database. This is my test case -

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DBSetUp.class)
@SpringBootTest(classes = ClassToBeTested.class)
public class ClassToBeTested{

@Autowired
private ClassToBeTested classToBeTested;

@Test
public void saveRandomTableTest(){
    Number number = null;
    RandomTableData randomTableData = new RandomTableData("XYZ", "XYZ WXY");
    number = classToBeTested.saveRandomTable(randomTableData );
    Assert.assertNotNull(number);
}

The class(ClassToBeTested.class) am trying to test looks like this -

private static final String INSERT_RANDOMTABLE_SQL = "INSERT INTO RANDOM_USER.RANDOMTABLE VALUES ( RANDOM_USER.RANDOMTABLE_SEQ.nextval, ?, ?)";
private static final String[] RANDOMTABLE_PRIMARY_KEY = {"RANDOMTABLE_ID"};

public Number saveRandomTable(RandomTableData randomTableData ) {
    Number id = null;
    try{
        PreparedStatementCreator psc = new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStmt = connection.prepareStatement(INSERT_RANDOMTABLE_SQL, RANDOMTABLE_PRIMARY_KEY);
                prepareStatement(preparedStmt, randomTableData);
                return preparedStmt;
            }
        };
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(psc, keyHolder);
        id = keyHolder.getKey();
    }catch(Exception e){
        LOGGER.error("Error is "+ e.getMessage());
    }
    return id;
}

And DBSetUp.class looks like this -

CREATE SCHEMA IF NOT EXISTS "RANDOM_USER";
CREATE SEQUENCE IF NOT EXISTS RANDOMTABLE_SEQ START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS "RANDOM_USER"."RANDOMTABLE"(
RANDOMTABLE_ID  NUMBER PRIMARY KEY,
RANDOMTABLE_CODE VARCHAR2(50),
RANDOMTABLE_NAME VARCHAR2(50)
);  

But whenever am trying to getting like this in this JUNIT test case -

ERROR : bad SQL grammar []; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error 
in SQL statement "INSERT INTO RANDOM_USER.RANDOMTABLE VALUES ( RANDOM_USER.RANDOMTABLE_SEQ.nextval, 
[*] ?, ?)"; expected "identifier"; SQL statement:INSERT INTO RANDOM_USER.RANDOMTABLE VALUES ( 
RANDOM_USER.RANDOMTABLE_SEQ.nextval, ?, ?) [42001-200]

In actual this application is using Oracle DB. All the SQL quarries working fine for the application. Am only getting this error while using H2 DB for JUNIT Testing. Is there any particular syntax I need to add H2 DB? If yes then it needs work with Oracle as well.

You need to change

CREATE SEQUENCE IF NOT EXISTS RANDOMTABLE_SEQ START WITH 1 INCREMENT BY 1;

to

CREATE SEQUENCE IF NOT EXISTS RANDOM_USER.RANDOMTABLE_SEQ START WITH 1 INCREMENT BY 1;

or change the current schema to RANDOM_USER before that command; otherwise the sequence is created in the default PUBLIC schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM