java.sql.SQLException:无效的列索引 - 显示在输出中,但测试通过并返回正确的结果

时间:2016-02-19 11:40:55

标签: java spring junit jdbctemplate dbunit

遇到了这个问题。 我使用弹簧 dbunit jdbctemplate c3p0 。我试图为我的dao层创建一些测试。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-test-config.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
                     TransactionalTestExecutionListener.class,
                     DbUnitTestExecutionListener.class})
@DatabaseSetup(value = "classpath:dbunit-testdata.xml", type = DatabaseOperation.CLEAN_INSERT)
public class NotesJdbcTemplateDaoTest {

private static Logger LOG = LoggerFactory.getLogger(NotesJdbcTemplateDaoTest.class);

@Autowired
private NotesDao notesJdbcTemplateDao;

@Test
public void testReceive() throws Exception {

    Note note = notesJdbcTemplateDao.receive(2);

    Assert.assertNotNull(note);
    LOG.debug(note.toString());
}

此测试通过:在DB表中从dbunit数据集生成。我可以从这个表中获取我的测试数据并打印到日志,但我也在日志中收到异常消息。

2016-02-19 08:16:02 DEBUG SqlUtils:85 - Attempted to convert SQLException to SQLException. Leaving it alone. [SQLState: 99999; errorCode: 17003]
java.sql.SQLException: Invalid column index
    at oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:1263)
    at com.mchange.v2.c3p0.impl.NewProxyResultSet.getString(NewProxyResultSet.java:3316)
    at org.dbunit.util.SQLHelper.createColumn(SQLHelper.java:404)
    at org.dbunit.database.DatabaseTableMetaData.getColumns(DatabaseTableMetaData.java:333)
    at org.dbunit.dataset.AbstractTableMetaData.getColumnIndex(AbstractTableMetaData.java:106)
    at org.dbunit.operation.AbstractOperation.getOperationMetaData(AbstractOperation.java:89)
    at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:143)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:194)
    at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:66)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:186)
    ......

可能是什么问题? 还要添加spring配置和数据集

<context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
        <property name="maxStatements" value="${jdbc.maxStatements}"/>
        <property name="testConnectionOnCheckout" value="${jdbc.testConnection}"/>
    </bean>

    <bean id="notesJdbcTemplateDao" class="com.sgleb.springrest.dao.impl.NotesJdbcTemplateDao"/>


<?xml version='1.0' encoding='UTF-8'?>
<dataset>

    <NOTES NOTE_ID="1" TOPIC="TOPIC 1" FULL_TEXT="Full text 1"/>
    <NOTES NOTE_ID="2" TOPIC="Topic two" FULL_TEXT="Fulltext 2"/>
    <NOTES NOTE_ID="3" TOPIC="topic 3three" FULL_TEXT="fulltext 3"/>
    <NOTES NOTE_ID="4" TOPIC="aaaaaaaaaaaa" FULL_TEXT="bcdefg12345"/>
    <NOTES NOTE_ID="5" TOPIC="note five" FULL_TEXT="note 5 full text"/>

</dataset>

也是dao的一部分

@Repository("notesJdbcTemplateDao")
public class NotesJdbcTemplateDao implements NotesDao {

    private final String SQL_RECEIVE_BY_ID = "SELECT * FROM NOTES WHERE NOTE_ID = ?";

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;

    @Override
    public Note receive(long id) {

        jdbcTemplate = new JdbcTemplate(dataSource);
        List<Note> result = jdbcTemplate.query(SQL_RECEIVE_BY_ID, new Object[]{id}, new NotesRowMapper());

        if (result.size() != 0)
            return result.get(0);
        else
            return null;
    }


注意1 :如果我尝试从真实数据库接收数据,它可以完美运行(没有dbunit)
注意2 :如果我清除接收测试方法中的所有行 - 即将其留空,则在调试中仍会出现exeption。所以它似乎是DbUnit的问题

0 个答案:

没有答案