Merge branch '1.5.x'

pull/7491/merge
Stephane Nicoll 8 years ago
commit 2577d79ff1

@ -5294,7 +5294,7 @@ Data JPA repositories. Regular `@Component` beans will not be loaded into the
`ApplicationContext`. `ApplicationContext`.
Data JPA tests are transactional and rollback at the end of each test by default, Data JPA tests are transactional and rollback at the end of each test by default,
see the {spring-reference}#testcontext-tx-enabling-transactions [relevant section] in the see the {spring-reference}#testcontext-tx-enabling-transactions[relevant section] in the
Spring Reference Documentation for more details. If that's not what you want, you can Spring Reference Documentation for more details. If that's not what you want, you can
disable transaction management for a test or for the whole class as follows: disable transaction management for a test or for the whole class as follows:
@ -5372,6 +5372,42 @@ A list of the auto-configuration that is enabled by `@DataJpaTest` can be
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test]]
==== Auto-configured JDBC tests
`@JdbcTest` is similar to `@DataJpaTest` but for pure jdbc-related tests. By default it
will also configure an in-memory embedded database and a `JdbcTemplate`. Regular
`@Component` beans will not be loaded into the `ApplicationContext`.
JDBC tests are transactional and rollback at the end of each test by default,
see the {spring-reference}#testcontext-tx-enabling-transactions[relevant section] in the
Spring Reference Documentation for more details. If that's not what you want, you can
disable transaction management for a test or for the whole class as follows:
[source,java,indent=0]
----
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringRunner.class)
@JdbcTest
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class ExampleNonTransactionalTests {
}
----
If you prefer your test to run against a real database, you can use the
`@AutoConfigureTestDatabase` annotation the same way as for `DataJpaTest`.
A list of the auto-configuration that is enabled by `@JdbcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]]
==== Auto-configured REST clients ==== Auto-configured REST clients
The `@RestClientTest` annotation can be used if you want to test REST clients. By default The `@RestClientTest` annotation can be used if you want to test REST clients. By default

@ -24,7 +24,7 @@ import sample.test.service.VehicleDetails;
import sample.test.service.VehicleDetailsService; import sample.test.service.VehicleDetailsService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;

@ -22,7 +22,7 @@ import sample.test.WelcomeCommandLineRunner;
import sample.test.service.VehicleDetails; import sample.test.service.VehicleDetails;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;

@ -0,0 +1,41 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
/**
* {@link ImportAutoConfiguration Auto-configuration imports} for typical jdbc tests.
* Most tests should consider using {@link JdbcTest @JdbcTest} rather than using
* this annotation directly.
*
* @author Stephane Nicoll
* @since 1.5.0
* @see JdbcTest
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ImportAutoConfiguration
public @interface AutoConfigureJdbc {
}

@ -0,0 +1,82 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
/**
* Annotation that can be applied to a test class to configure a test database to use
* instead of any application defined or auto-configured {@link DataSource}.
*
* @author Phillip Webb
* @see TestDatabaseAutoConfiguration
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ImportAutoConfiguration
@PropertyMapping("spring.test.database")
public @interface AutoConfigureTestDatabase {
/**
* Determines what type of existing DataSource beans can be replaced.
* @return the type of existing DataSource to replace
*/
Replace replace() default Replace.ANY;
/**
* The type of connection to be established when {@link #replace() replacing} the data
* source. By default will attempt to detect the connection based on the classpath.
* @return the type of connection to use
*/
EmbeddedDatabaseConnection connection() default EmbeddedDatabaseConnection.NONE;
/**
* What the test database can replace.
*/
enum Replace {
/**
* Replace any DataSource bean (auto-configured or manually defined).
*/
ANY,
/**
* Only replace auto-configured DataSource.
*/
AUTO_CONFIGURED,
/**
* Don't replace the application default DataSource.
*/
NONE
}
}

@ -0,0 +1,98 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.BootstrapWith;
import org.springframework.transaction.annotation.Transactional;
/**
* Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)}
* for a typical jdbc test. Can be used when a test focuses <strong>only</strong> on
* jdbc-based components.
* <p>
* Using this annotation will disable full auto-configuration and instead apply only
* configuration relevant to jdbc tests.
* <p>
* By default, tests annotated with {@code @JdbcTest} will use an embedded in-memory
* database (replacing any explicit or usually auto-configured DataSource). The
* {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation can be used to
* override these settings.
* <p>
* If you are looking to load your full application configuration, but use an embedded
* database, you should consider {@link SpringBootTest @SpringBootTest} combined with
* {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} rather than this
* annotation.
*
* @author Stephane Nicoll
* @see AutoConfigureJdbc
* @see AutoConfigureTestDatabase
* @see AutoConfigureCache
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@OverrideAutoConfiguration(enabled = false)
@TypeExcludeFilters(JdbcTypeExcludeFilter.class)
@Transactional
@AutoConfigureCache
@AutoConfigureJdbc
@AutoConfigureTestDatabase
@ImportAutoConfiguration
public @interface JdbcTest {
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are
* included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used
*/
boolean useDefaultFilters() default true;
/**
* A set of include filters which can be used to add otherwise filtered beans to the
* application context.
* @return include filters to apply
*/
ComponentScan.Filter[] includeFilters() default {};
/**
* A set of exclude filters which can be used to filter beans that would otherwise be
* added to the application context.
* @return exclude filters to apply
*/
ComponentScan.Filter[] excludeFilters() default {};
}

@ -0,0 +1,76 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
/**
* {@link TypeExcludeFilter} for {@link JdbcTest @Jdbctest}.
*
* @author Stephane Nicoll
*/
class JdbcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
private final JdbcTest annotation;
JdbcTypeExcludeFilter(Class<?> testClass) {
this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass,
JdbcTest.class);
}
@Override
protected boolean hasAnnotation() {
return this.annotation != null;
}
@Override
protected ComponentScan.Filter[] getFilters(FilterType type) {
switch (type) {
case INCLUDE:
return this.annotation.includeFilters();
case EXCLUDE:
return this.annotation.excludeFilters();
}
throw new IllegalStateException("Unsupported type " + type);
}
@Override
protected boolean isUseDefaultFilters() {
return this.annotation.useDefaultFilters();
}
@Override
protected boolean defaultInclude(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) throws IOException {
return false;
}
@Override
protected Set<Class<?>> getDefaultIncludes() {
return Collections.emptySet();
}
}

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.test.autoconfigure.orm.jpa; package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;

@ -33,8 +33,8 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
* Annotation that can be applied to a test class to configure a test database to use * Annotation that can be applied to a test class to configure a test database to use
* instead of any application defined or auto-configured {@link DataSource}. * instead of any application defined or auto-configured {@link DataSource}.
* *
* @author Phillip Webb * @author Stephane Nicoll
* @see TestDatabaseAutoConfiguration * @deprecated since 1.5 in favour of {@link org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase}
*/ */
@Target({ ElementType.TYPE, ElementType.METHOD }) @Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@ -42,6 +42,7 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
@Inherited @Inherited
@ImportAutoConfiguration @ImportAutoConfiguration
@PropertyMapping("spring.test.database") @PropertyMapping("spring.test.database")
@Deprecated
public @interface AutoConfigureTestDatabase { public @interface AutoConfigureTestDatabase {
/** /**
@ -60,6 +61,7 @@ public @interface AutoConfigureTestDatabase {
/** /**
* What the test database can replace. * What the test database can replace.
*/ */
@Deprecated
enum Replace { enum Replace {
/** /**

@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;

@ -13,6 +13,20 @@ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
# AutoConfigureJdbc auto-configuration imports
org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
# AutoConfigureTestDatabase auto-configuration imports
org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase=\
org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
# AutoConfigureJson auto-configuration imports # AutoConfigureJson auto-configuration imports
org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=\ org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
@ -39,7 +53,7 @@ org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration
# AutoConfigureTestDatabase auto-configuration imports # AutoConfigureTestDatabase auto-configuration imports
org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase=\ org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase=\
org.springframework.boot.test.autoconfigure.orm.jpa.TestDatabaseAutoConfiguration,\ org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
# AutoConfigureTestEntityManager auto-configuration imports # AutoConfigureTestEntityManager auto-configuration imports

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.test.autoconfigure.orm.jpa; package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -39,27 +39,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Greg Potter * @author Greg Potter
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @JdbcTest
@AutoConfigureTestDatabase @AutoConfigureTestDatabase
public class AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests { public class AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests {
@Autowired
private TestEntityManager entities;
@Autowired
private ExampleRepository repository;
@Autowired @Autowired
private DataSource dataSource; private DataSource dataSource;
@Test
public void testRepository() throws Exception {
this.entities.persist(new ExampleEntity("boot", "124"));
this.entities.flush();
ExampleEntity found = this.repository.findByReference("124");
assertThat(found.getName()).isEqualTo("boot");
}
@Test @Test
public void replacesDefinedDataSourceWithExplicit() throws Exception { public void replacesDefinedDataSourceWithExplicit() throws Exception {
// Look that the datasource is replaced with an H2 DB. // Look that the datasource is replaced with an H2 DB.

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.test.autoconfigure.orm.jpa; package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;

@ -0,0 +1,51 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
/**
* Example entity used with {@link JdbcTest} tests.
*
* @author Stephane Nicoll
*/
public class ExampleEntity {
private final int id;
private String name;
public ExampleEntity(int id, String name) {
this.id = id;
this.name = name;
}
public ExampleEntity(int id) {
this(id, null);
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,41 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
/**
* Example {@link SpringBootApplication} used with {@link JdbcTest} tests.
*
* @author Phillip Webb
*/
@SpringBootApplication
public class ExampleJdbcApplication {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL);
return builder.build();
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import javax.transaction.Transactional;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
/**
* Example repository used with {@link JdbcTest} tests.
*
* @author Stephane Nicoll
*/
@Repository
public class ExampleRepository {
private static final ExampleEntityRowMapper ROW_MAPPER = new ExampleEntityRowMapper();
private final JdbcTemplate jdbcTemplate;
public ExampleRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public void save(ExampleEntity entity) {
this.jdbcTemplate.update("insert into example (id, name) values (?, ?)",
entity.getId(), entity.getName());
}
public ExampleEntity findById(int id) {
return this.jdbcTemplate.queryForObject("select id, name from example where id =?",
new Object[] { id }, ROW_MAPPER);
}
public Collection<ExampleEntity> findAll() {
return this.jdbcTemplate.query("select id, name from example", ROW_MAPPER);
}
private static class ExampleEntityRowMapper implements RowMapper<ExampleEntity> {
@Override
public ExampleEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
int id = rs.getInt("id");
String name = rs.getString("name");
return new ExampleEntity(id, name);
}
}
}

@ -0,0 +1,98 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import java.util.Collection;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Integration tests for {@link JdbcTest}.
*
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@JdbcTest
@TestPropertySource(properties = "spring.datasource.schema=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql")
public class JdbcTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private DataSource dataSource;
@Autowired
private ApplicationContext applicationContext;
@Test
public void testJdbcTemplate() {
ExampleRepository repository = new ExampleRepository(this.jdbcTemplate);
repository.save(new ExampleEntity(1, "John"));
Collection<ExampleEntity> entities = repository.findAll();
assertThat(entities).hasSize(1);
ExampleEntity entity = entities.iterator().next();
assertThat(entity.getId()).isEqualTo(1);
assertThat(entity.getName()).isEqualTo("John");
}
@Test
public void replacesDefinedDataSourceWithEmbeddedDefault() throws Exception {
String product = this.dataSource.getConnection().getMetaData()
.getDatabaseProductName();
assertThat(product).isEqualTo("H2");
}
@Test
public void didNotInjectExampleRepository() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleRepository.class);
}
@Test
public void flywayAutoConfigurationWasImported() {
assertThat(this.applicationContext)
.has(importedAutoConfiguration(FlywayAutoConfiguration.class));
}
@Test
public void liquibaseAutoConfigurationWasImported() {
assertThat(this.applicationContext)
.has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
}
}

@ -0,0 +1,59 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link JdbcTest}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@JdbcTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED, connection = EmbeddedDatabaseConnection.HSQL)
public class JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests {
@Autowired
private DataSource dataSource;
@Test
public void replacesAutoConfiguredDataSource() throws Exception {
String product = this.dataSource.getConnection().getMetaData()
.getDatabaseProductName();
assertThat(product).startsWith("HSQL");
}
@Configuration
@EnableAutoConfiguration // Will auto-configure H2
static class Config {
}
}

@ -0,0 +1,51 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link JdbcTest}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@JdbcTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)
public class JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests {
@Autowired
private DataSource dataSource;
@Test
public void usesDefaultEmbeddedDatabase() throws Exception {
String product = this.dataSource.getConnection().getMetaData()
.getDatabaseProductName();
// @AutoConfigureTestDatabase would use H2 but HSQL is manually defined
assertThat(product).startsWith("HSQL");
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link JdbcTest}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@JdbcTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
public class JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests {
@Autowired
private DataSource dataSource;
@Test
public void replacesDefinedDataSourceWithExplicit() throws Exception {
// H2 is explicitly defined but HSQL is the override.
String product = this.dataSource.getConnection().getMetaData()
.getDatabaseProductName();
assertThat(product).startsWith("HSQL");
}
@Configuration
@EnableAutoConfiguration
static class Config {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2);
return builder.build();
}
}
}

@ -0,0 +1,51 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link JdbcTest}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@JdbcTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class JdbcTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests {
@Autowired
private DataSource dataSource;
@Test
public void usesDefaultEmbeddedDatabase() throws Exception {
// HSQL is explicitly defined and should not be replaced
String product = this.dataSource.getConnection().getMetaData()
.getDatabaseProductName();
assertThat(product).startsWith("HSQL");
}
}

@ -0,0 +1,51 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.jdbc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test with custom include filter for {@link JdbcTest}.
*
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@JdbcTest(includeFilters = @Filter(Repository.class))
@TestPropertySource(properties = "spring.datasource.schema=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql")
public class JdbcTestWithIncludeFilterIntegrationTests {
@Autowired
private ExampleRepository repository;
@Test
public void testRepository() {
this.repository.save(new ExampleEntity(42, "Smith"));
ExampleEntity entity = this.repository.findById(42);
assertThat(entity).isNotNull();
assertThat(entity.getName()).isEqualTo("Smith");
}
}

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.test.autoconfigure.orm.jpa; package org.springframework.boot.test.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;

@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.AUTO_CONFIGURED, connection = EmbeddedDatabaseConnection.HSQL) @AutoConfigureTestDatabase(replace = Replace.AUTO_CONFIGURED, connection = EmbeddedDatabaseConnection.HSQL)
@Deprecated
public class DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests { public class DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests {
@Autowired @Autowired

@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.AUTO_CONFIGURED) @AutoConfigureTestDatabase(replace = Replace.AUTO_CONFIGURED)
@Deprecated
public class DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests { public class DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests {
@Autowired @Autowired

@ -40,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL) @AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
@Deprecated
public class DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests { public class DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests {
@Autowired @Autowired

@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE) @AutoConfigureTestDatabase(replace = Replace.NONE)
@Deprecated
public class DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests { public class DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests {
@Autowired @Autowired

Loading…
Cancel
Save