Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
5fe9ef69
Commit
5fe9ef69
authored
Oct 02, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add SpringApplicationContextLoader
parent
eabb1e70
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
158 additions
and
59 deletions
+158
-59
HibernateJpaAutoConfiguration.java
.../autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java
+0
-2
pom.xml
spring-boot-samples/spring-boot-sample-data-jpa/pom.xml
+6
-0
AbstractIntegrationTests.java
...mework/boot/sample/data/jpa/AbstractIntegrationTests.java
+0
-39
SampleDataJpaApplicationTests.java
...k/boot/sample/data/jpa/SampleDataJpaApplicationTests.java
+15
-5
CityRepositoryIntegrationTests.java
...mple/data/jpa/service/CityRepositoryIntegrationTests.java
+13
-6
HotelRepositoryIntegrationTests.java
...ple/data/jpa/service/HotelRepositoryIntegrationTests.java
+14
-7
application-scratch.properties
...ata-jpa/src/test/resources/application-scratch.properties
+1
-0
SpringApplicationContextLoader.java
...ngframework/boot/test/SpringApplicationContextLoader.java
+109
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java
View file @
5fe9ef69
...
...
@@ -65,8 +65,6 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
protected
void
configure
(
LocalContainerEntityManagerFactoryBean
entityManagerFactoryBean
)
{
Map
<
String
,
Object
>
properties
=
entityManagerFactoryBean
.
getJpaPropertyMap
();
properties
.
put
(
"hibernate.cache.provider_class"
,
this
.
environment
.
getProperty
(
"cache-provider"
,
"org.hibernate.cache.HashtableCacheProvider"
));
properties
.
put
(
"hibernate.ejb.naming_strategy"
,
this
.
environment
.
getProperty
(
"naming-strategy"
,
ImprovedNamingStrategy
.
class
.
getName
()));
String
ddlAuto
=
this
.
environment
.
getProperty
(
"ddl-auto"
,
"none"
);
...
...
spring-boot-samples/spring-boot-sample-data-jpa/pom.xml
View file @
5fe9ef69
...
...
@@ -22,6 +22,12 @@
<groupId>
${project.groupId}
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
${project.groupId}
</groupId>
<artifactId>
spring-boot
</artifactId>
<classifier>
tests
</classifier>
<version>
${spring-boot.version}
</version>
</dependency>
<dependency>
<groupId>
org.hsqldb
</groupId>
<artifactId>
hsqldb
</artifactId>
...
...
spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/AbstractIntegrationTests.java
deleted
100644 → 0
View file @
eabb1e70
/*
* Copyright 2013 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
.
sample
.
data
.
jpa
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer
;
import
org.springframework.boot.context.initializer.LoggingApplicationContextInitializer
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
/**
* Base class for integration tests. Mimics the behaviour of
* {@link SpringApplication#run(String...)}.
*
* @author Oliver Gierke
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@WebAppConfiguration
@ContextConfiguration
(
classes
=
SampleDataJpaApplication
.
class
,
initializers
=
{
ConfigFileApplicationContextInitializer
.
class
,
LoggingApplicationContextInitializer
.
class
})
public
abstract
class
AbstractIntegrationTests
{
}
spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java
View file @
5fe9ef69
package
org
.
springframework
.
boot
.
sample
.
data
.
jpa
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
content
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.SpringApplicationContextLoader
;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.context.WebApplicationContext
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
content
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
/**
* Integration test to run the application.
*
* @author Oliver Gierke
*/
public
class
SampleDataJpaApplicationTests
extends
AbstractIntegrationTests
{
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
classes
=
SampleDataJpaApplication
.
class
,
loader
=
SpringApplicationContextLoader
.
class
)
@WebAppConfiguration
@ActiveProfiles
(
"scratch"
)
// Separate profile for web tests to avoid clashing databases
public
class
SampleDataJpaApplicationTests
{
@Autowired
private
WebApplicationContext
context
;
...
...
spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java
View file @
5fe9ef69
...
...
@@ -15,22 +15,29 @@
*/
package
org
.
springframework
.
boot
.
sample
.
data
.
jpa
.
service
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
greaterThan
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.sample.data.jpa.
AbstractIntegrationTests
;
import
org.springframework.boot.sample.data.jpa.
SampleDataJpaApplication
;
import
org.springframework.boot.sample.data.jpa.domain.City
;
import
org.springframework.boot.test.SpringApplicationContextLoader
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
/**
* Integration tests for {@link CityRepository}.
*
* @author Oliver Gierke
*/
public
class
CityRepositoryIntegrationTests
extends
AbstractIntegrationTests
{
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
classes
=
SampleDataJpaApplication
.
class
,
loader
=
SpringApplicationContextLoader
.
class
)
public
class
CityRepositoryIntegrationTests
{
@Autowired
CityRepository
repository
;
...
...
@@ -39,6 +46,6 @@ public class CityRepositoryIntegrationTests extends AbstractIntegrationTests {
public
void
findsFirstPageOfCities
()
{
Page
<
City
>
cities
=
this
.
repository
.
findAll
(
new
PageRequest
(
0
,
10
));
assertThat
(
cities
.
getTotalElements
(),
is
(
21L
));
assertThat
(
cities
.
getTotalElements
(),
is
(
greaterThan
(
20L
)
));
}
}
spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java
View file @
5fe9ef69
...
...
@@ -15,30 +15,37 @@
*/
package
org
.
springframework
.
boot
.
sample
.
data
.
jpa
.
service
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
greaterThan
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
java.util.List
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.sample.data.jpa.
AbstractIntegrationTests
;
import
org.springframework.boot.sample.data.jpa.
SampleDataJpaApplication
;
import
org.springframework.boot.sample.data.jpa.domain.City
;
import
org.springframework.boot.sample.data.jpa.domain.Hotel
;
import
org.springframework.boot.sample.data.jpa.domain.HotelSummary
;
import
org.springframework.boot.sample.data.jpa.domain.Rating
;
import
org.springframework.boot.sample.data.jpa.domain.RatingCount
;
import
org.springframework.boot.test.SpringApplicationContextLoader
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Sort.Direction
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
/**
* Integration tests for {@link HotelRepository}.
*
* @author Oliver Gierke
*/
public
class
HotelRepositoryIntegrationTests
extends
AbstractIntegrationTests
{
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
classes
=
SampleDataJpaApplication
.
class
,
loader
=
SpringApplicationContextLoader
.
class
)
public
class
HotelRepositoryIntegrationTests
{
@Autowired
CityRepository
cityRepository
;
...
...
@@ -61,6 +68,6 @@ public class HotelRepositoryIntegrationTests extends AbstractIntegrationTests {
List
<
RatingCount
>
counts
=
this
.
repository
.
findRatingCounts
(
hotel
);
assertThat
(
counts
,
hasSize
(
1
));
assertThat
(
counts
.
get
(
0
).
getRating
(),
is
(
Rating
.
AVERAGE
));
assertThat
(
counts
.
get
(
0
).
getCount
(),
is
(
2L
));
assertThat
(
counts
.
get
(
0
).
getCount
(),
is
(
greaterThan
(
1L
)
));
}
}
spring-boot-samples/spring-boot-sample-data-jpa/src/test/resources/application-scratch.properties
0 → 100644
View file @
5fe9ef69
spring.datasource.url
:
jdbc:hsqldb:mem:scratchdb
spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationContextLoader.java
0 → 100644
View file @
5fe9ef69
/*
* Copyright 2012-2013 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
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Set
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.context.initializer.ServletContextApplicationContextInitializer
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextInitializer
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.test.context.ContextLoader
;
import
org.springframework.test.context.MergedContextConfiguration
;
import
org.springframework.test.context.support.AbstractContextLoader
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.context.web.WebMergedContextConfiguration
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.context.support.GenericWebApplicationContext
;
/**
* A {@link ContextLoader} that can be used to test Spring Boot applications (those that
* normally startup using {@link SpringApplication}). Never starts an embedded web server,
* but detects the {@link WebAppConfiguration @WebAppConfiguration} annotation on the test
* class and only creates a web application context if it is present. Non-web features,
* like a repository layer, can be tested cleanly by simply <em>not</em> marking the test
* class <code>@WebAppConfiguration</code>.
*
* <p>
* If <code>@ActiveProfiles</code> are provided in the test class they will be used to
* create the application context.
*
* @author Dave Syer
*
*/
public
class
SpringApplicationContextLoader
extends
AbstractContextLoader
{
@Override
public
ApplicationContext
loadContext
(
MergedContextConfiguration
mergedConfig
)
throws
Exception
{
Set
<
Object
>
sources
=
new
LinkedHashSet
<
Object
>();
sources
.
addAll
(
Arrays
.
asList
(
mergedConfig
.
getClasses
()));
sources
.
addAll
(
Arrays
.
asList
(
mergedConfig
.
getLocations
()));
SpringApplication
application
=
new
SpringApplication
();
application
.
setSources
(
sources
);
Set
<
String
>
args
=
new
LinkedHashSet
<
String
>();
if
(!
ObjectUtils
.
isEmpty
(
mergedConfig
.
getActiveProfiles
()))
{
args
.
add
(
"--spring.profiles.active="
+
StringUtils
.
arrayToCommaDelimitedString
(
mergedConfig
.
getActiveProfiles
()));
}
// Not running an embedded server, just setting up web context
args
.
add
(
"--server.port=0"
);
args
.
add
(
"--management.port=0"
);
application
.
setDefaultArgs
(
args
.
toArray
(
new
String
[
args
.
size
()]));
List
<
ApplicationContextInitializer
<?>>
initializers
=
new
ArrayList
<
ApplicationContextInitializer
<?>>(
application
.
getInitializers
());
for
(
Class
<?
extends
ApplicationContextInitializer
<?>>
type
:
mergedConfig
.
getContextInitializerClasses
())
{
initializers
.
add
(
BeanUtils
.
instantiate
(
type
));
}
if
(
mergedConfig
instanceof
WebMergedContextConfiguration
)
{
WebMergedContextConfiguration
webConfig
=
(
WebMergedContextConfiguration
)
mergedConfig
;
MockServletContext
servletContext
=
new
MockServletContext
(
webConfig
.
getResourceBasePath
());
initializers
.
add
(
0
,
new
ServletContextApplicationContextInitializer
(
servletContext
));
application
.
setApplicationContextClass
(
GenericWebApplicationContext
.
class
);
}
else
{
application
.
setWebEnvironment
(
false
);
}
application
.
setInitializers
(
initializers
);
return
application
.
run
();
}
@Override
public
ApplicationContext
loadContext
(
String
...
locations
)
throws
Exception
{
throw
new
UnsupportedOperationException
(
"SpringApplicationContextLoader does not support the loadContext(String...) method"
);
}
@Override
protected
String
getResourceSuffix
()
{
return
"-context.xml"
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment