java.sql.SQLException:列索引无效-显示在输出中,但测试通过并返回正确的结果
面对这个问题。 我将spring与dbunit、jdbctemplate和c3p0一起使用。我正在尝试为我的dao层创建一些测试java.sql.SQLException:列索引无效-显示在输出中,但测试通过并返回正确的结果,java,spring,junit,jdbctemplate,dbunit,Java,Spring,Junit,Jdbctemplate,Dbunit,面对这个问题。 我将spring与dbunit、jdbctemplate和c3p0一起使用。我正在尝试为我的dao层创建一些测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-test-config.xml"}) @TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
@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());
}
这个测试通过得很好:indb表是从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"/>
还有一把刀
@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;
}
@Repository(“notesJdbcTemplateDao”)
公共类NotesJdbcTemplateDao实现NotesDao{
私有最终字符串SQL_RECEIVE_BY_ID=“SELECT*FROM NOTES,其中NOTE_ID=?”;
@自动连线
私有数据源;
私有JdbcTemplate JdbcTemplate;
@凌驾
公共票据接收(长id){
jdbcTemplate=新的jdbcTemplate(数据源);
List result=jdbcTemplate.query(SQL_RECEIVE_BY_ID,new Object[]{ID},new NotesRowMapper());
如果(result.size()!=0)
返回结果get(0);
其他的
返回null;
}
注意1:如果我试图从real DB接收数据,它工作得非常完美(没有dbunit)
注2:如果我清除了接收测试方法中的所有行,即保留为空,则调试中仍会出现exeption。因此,DbUnit似乎有问题。索引的值是多少?@RomanC,2,它显示在代码段中。我在数据集DB中有此项,并在结果中获得它。但也得到了我关于例外的消息)
@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;
}