Browse Source

Polish RowMapper tests

pull/29928/head
Sam Brannen 3 years ago
parent
commit
84bc1dfa89
  1. 14
      spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java
  2. 18
      spring-jdbc/src/test/java/org/springframework/jdbc/core/DataClassRowMapperTests.java

14
spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java

@ -51,8 +51,8 @@ class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
void overridingDifferentClassDefinedForMapping() { void overridingDifferentClassDefinedForMapping() {
BeanPropertyRowMapper mapper = new BeanPropertyRowMapper(Person.class); BeanPropertyRowMapper mapper = new BeanPropertyRowMapper(Person.class);
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
mapper.setMappedClass(Long.class)); .isThrownBy(() -> mapper.setMappedClass(Long.class));
} }
@Test @Test
@ -107,18 +107,18 @@ class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
@Test @Test
void mappingWithUnpopulatedFieldsNotAccepted() throws Exception { void mappingWithUnpopulatedFieldsNotAccepted() throws Exception {
BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(ExtendedPerson.class, true);
Mock mock = new Mock(); Mock mock = new Mock();
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
mock.getJdbcTemplate().query("select name, age, birth_date, balance from people", .isThrownBy(() -> mock.getJdbcTemplate().query("select name, age, birth_date, balance from people", mapper));
new BeanPropertyRowMapper<>(ExtendedPerson.class, true)));
} }
@Test @Test
void mappingNullValue() throws Exception { void mappingNullValue() throws Exception {
BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class); BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
Mock mock = new Mock(MockType.TWO); Mock mock = new Mock(MockType.TWO);
assertThatExceptionOfType(TypeMismatchException.class).isThrownBy(() -> assertThatExceptionOfType(TypeMismatchException.class)
mock.getJdbcTemplate().query(SELECT_NULL_AS_AGE, mapper)); .isThrownBy(() -> mock.getJdbcTemplate().query(SELECT_NULL_AS_AGE, mapper));
} }
@Test @Test

18
spring-jdbc/src/test/java/org/springframework/jdbc/core/DataClassRowMapperTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,10 +33,10 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 5.3 * @since 5.3
*/ */
public class DataClassRowMapperTests extends AbstractRowMapperTests { class DataClassRowMapperTests extends AbstractRowMapperTests {
@Test @Test
public void testStaticQueryWithDataClass() throws Exception { void staticQueryWithDataClass() throws Exception {
Mock mock = new Mock(); Mock mock = new Mock();
List<ConstructorPerson> result = mock.getJdbcTemplate().query( List<ConstructorPerson> result = mock.getJdbcTemplate().query(
"select name, age, birth_date, balance from people", "select name, age, birth_date, balance from people",
@ -48,7 +48,7 @@ public class DataClassRowMapperTests extends AbstractRowMapperTests {
} }
@Test @Test
public void testStaticQueryWithDataClassAndGenerics() throws Exception { void staticQueryWithDataClassAndGenerics() throws Exception {
Mock mock = new Mock(); Mock mock = new Mock();
List<ConstructorPersonWithGenerics> result = mock.getJdbcTemplate().query( List<ConstructorPersonWithGenerics> result = mock.getJdbcTemplate().query(
"select name, age, birth_date, balance from people", "select name, age, birth_date, balance from people",
@ -57,14 +57,14 @@ public class DataClassRowMapperTests extends AbstractRowMapperTests {
ConstructorPersonWithGenerics person = result.get(0); ConstructorPersonWithGenerics person = result.get(0);
assertThat(person.name()).isEqualTo("Bubba"); assertThat(person.name()).isEqualTo("Bubba");
assertThat(person.age()).isEqualTo(22L); assertThat(person.age()).isEqualTo(22L);
assertThat(person.birthDate()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L)); assertThat(person.birthDate()).usingComparator(Date::compareTo).isEqualTo(new Date(1221222L));
assertThat(person.balance()).isEqualTo(Collections.singletonList(new BigDecimal("1234.56"))); assertThat(person.balance()).isEqualTo(Collections.singletonList(new BigDecimal("1234.56")));
mock.verifyClosed(); mock.verifyClosed();
} }
@Test @Test
public void testStaticQueryWithDataClassAndSetters() throws Exception { void staticQueryWithDataClassAndSetters() throws Exception {
Mock mock = new Mock(MockType.FOUR); Mock mock = new Mock(MockType.FOUR);
List<ConstructorPersonWithSetters> result = mock.getJdbcTemplate().query( List<ConstructorPersonWithSetters> result = mock.getJdbcTemplate().query(
"select name, age, birthdate, balance from people", "select name, age, birthdate, balance from people",
@ -73,14 +73,14 @@ public class DataClassRowMapperTests extends AbstractRowMapperTests {
ConstructorPersonWithSetters person = result.get(0); ConstructorPersonWithSetters person = result.get(0);
assertThat(person.name()).isEqualTo("BUBBA"); assertThat(person.name()).isEqualTo("BUBBA");
assertThat(person.age()).isEqualTo(22L); assertThat(person.age()).isEqualTo(22L);
assertThat(person.birthDate()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L)); assertThat(person.birthDate()).usingComparator(Date::compareTo).isEqualTo(new Date(1221222L));
assertThat(person.balance()).isEqualTo(new BigDecimal("1234.56")); assertThat(person.balance()).isEqualTo(new BigDecimal("1234.56"));
mock.verifyClosed(); mock.verifyClosed();
} }
@Test @Test
public void testStaticQueryWithDataRecord() throws Exception { void staticQueryWithDataRecord() throws Exception {
Mock mock = new Mock(); Mock mock = new Mock();
List<RecordPerson> result = mock.getJdbcTemplate().query( List<RecordPerson> result = mock.getJdbcTemplate().query(
"select name, age, birth_date, balance from people", "select name, age, birth_date, balance from people",
@ -94,7 +94,7 @@ public class DataClassRowMapperTests extends AbstractRowMapperTests {
protected void verifyPerson(RecordPerson person) { protected void verifyPerson(RecordPerson person) {
assertThat(person.name()).isEqualTo("Bubba"); assertThat(person.name()).isEqualTo("Bubba");
assertThat(person.age()).isEqualTo(22L); assertThat(person.age()).isEqualTo(22L);
assertThat(person.birth_date()).usingComparator(Date::compareTo).isEqualTo(new java.util.Date(1221222L)); assertThat(person.birth_date()).usingComparator(Date::compareTo).isEqualTo(new Date(1221222L));
assertThat(person.balance()).isEqualTo(new BigDecimal("1234.56")); assertThat(person.balance()).isEqualTo(new BigDecimal("1234.56"));
verifyPersonViaBeanWrapper(person); verifyPersonViaBeanWrapper(person);
} }

Loading…
Cancel
Save