JdbcTemplate's queryForObject allows for specifying primitive types as well

Issue: SPR-13220
This commit is contained in:
Juergen Hoeller
2015-07-13 21:30:57 +02:00
parent 8fdbf4285b
commit e0329306df
3 changed files with 51 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ import static org.mockito.BDDMockito.*;
/**
* @author Juergen Hoeller
* @author Phillip Webb
* @author Rob Winch
* @since 19.12.2004
*/
public class JdbcTemplateQueryTests {
@@ -49,13 +50,20 @@ public class JdbcTemplateQueryTests {
public ExpectedException thrown = ExpectedException.none();
private Connection connection;
private DataSource dataSource;
private Statement statement;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
private ResultSetMetaData resultSetMetaData;
private JdbcTemplate template;
@Before
public void setUp() throws Exception {
this.connection = mock(Connection.class);
@@ -75,6 +83,7 @@ public class JdbcTemplateQueryTests {
given(this.statement.executeQuery(anyString())).willReturn(this.resultSet);
}
@Test
public void testQueryForList() throws Exception {
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
@@ -219,7 +228,18 @@ public class JdbcTemplateQueryTests {
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
given(this.resultSet.next()).willReturn(true, false);
given(this.resultSet.getInt(1)).willReturn(22);
int i = this.template.queryForObject(sql,Integer.class).intValue();
int i = this.template.queryForObject(sql, Integer.class).intValue();
assertEquals("Return of an int", 22, i);
verify(this.resultSet).close();
verify(this.statement).close();
}
@Test
public void testQueryForIntPrimitive() throws Exception {
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
given(this.resultSet.next()).willReturn(true, false);
given(this.resultSet.getInt(1)).willReturn(22);
int i = this.template.queryForObject(sql, int.class);
assertEquals("Return of an int", 22, i);
verify(this.resultSet).close();
verify(this.statement).close();
@@ -236,6 +256,17 @@ public class JdbcTemplateQueryTests {
verify(this.statement).close();
}
@Test
public void testQueryForLongPrimitive() throws Exception {
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
given(this.resultSet.next()).willReturn(true, false);
given(this.resultSet.getLong(1)).willReturn(87L);
long l = this.template.queryForObject(sql, long.class);
assertEquals("Return of a long", 87, l);
verify(this.resultSet).close();
verify(this.statement).close();
}
@Test
public void testQueryForListWithArgs() throws Exception {
doTestQueryForListWithArgs("SELECT AGE FROM CUSTMR WHERE ID < ?");