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
c8c32cfa
Commit
c8c32cfa
authored
Jul 11, 2018
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate tests to ApplicationContextRunner
parent
cced3514
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
100 deletions
+79
-100
MongoDataAutoConfigurationTests.java
...configure/data/mongo/MongoDataAutoConfigurationTests.java
+69
-84
MongoReactiveDataAutoConfigurationTests.java
...e/data/mongo/MongoReactiveDataAutoConfigurationTests.java
+10
-16
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java
View file @
c8c32cfa
...
...
@@ -21,17 +21,17 @@ import java.util.Arrays;
import
java.util.Set
;
import
com.mongodb.MongoClient
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.boot.autoconfigure.AutoConfigurationPackages
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.data.mongo.city.City
;
import
org.springframework.boot.autoconfigure.data.mongo.country.Country
;
import
org.springframework.boot.autoconfigure.domain.EntityScan
;
import
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
;
import
org.springframework.boot.test.
util.TestPropertyValues
;
import
org.springframework.boot.test.
context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -48,7 +48,6 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
/**
* Tests for {@link MongoDataAutoConfiguration}.
...
...
@@ -58,125 +57,111 @@ import static org.junit.Assert.fail;
*/
public
class
MongoDataAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
));
@Test
public
void
templateExists
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeanNamesForType
(
MongoTemplate
.
class
).
length
)
.
isEqualTo
(
1
);
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
MongoTemplate
.
class
));
}
@Test
public
void
gridFsTemplateExists
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.data.mongodb.gridFsDatabase:grid"
)
.
applyTo
(
this
.
context
);
this
.
context
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeanNamesForType
(
GridFsTemplate
.
class
).
length
)
.
isEqualTo
(
1
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.gridFsDatabase:grid"
)
.
run
((
context
)
->
assertThat
(
context
)
.
hasSingleBean
(
GridFsTemplate
.
class
));
}
@Test
public
void
customConversions
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
CustomConversionsConfig
.
class
);
this
.
context
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoTemplate
template
=
this
.
context
.
getBean
(
MongoTemplate
.
class
);
assertThat
(
template
.
getConverter
().
getConversionService
()
.
canConvert
(
MongoClient
.
class
,
Boolean
.
class
)).
isTrue
();
this
.
contextRunner
.
withUserConfiguration
(
CustomConversionsConfig
.
class
)
.
run
((
context
)
->
{
MongoTemplate
template
=
context
.
getBean
(
MongoTemplate
.
class
);
assertThat
(
template
.
getConverter
().
getConversionService
()
.
canConvert
(
MongoClient
.
class
,
Boolean
.
class
)).
isTrue
();
});
}
@Test
public
void
usesAutoConfigurationPackageToPickUpDocumentTypes
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
String
cityPackage
=
City
.
class
.
getPackage
().
getName
();
AutoConfigurationPackages
.
register
(
this
.
context
,
cityPackage
);
this
.
context
.
register
(
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertDomainTypesDiscovered
(
this
.
context
.
getBean
(
MongoMappingContext
.
class
),
City
.
class
);
AutoConfigurationPackages
.
register
(
context
,
cityPackage
);
context
.
register
(
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
try
{
context
.
refresh
();
assertDomainTypesDiscovered
(
context
.
getBean
(
MongoMappingContext
.
class
),
City
.
class
);
}
finally
{
context
.
close
();
}
}
@Test
public
void
defaultFieldNamingStrategy
()
{
testFieldNamingStrategy
(
null
,
PropertyNameFieldNamingStrategy
.
class
);
this
.
contextRunner
.
run
((
context
)
->
{
MongoMappingContext
mappingContext
=
context
.
getBean
(
MongoMappingContext
.
class
);
FieldNamingStrategy
fieldNamingStrategy
=
(
FieldNamingStrategy
)
ReflectionTestUtils
.
getField
(
mappingContext
,
"fieldNamingStrategy"
);
assertThat
(
fieldNamingStrategy
.
getClass
())
.
isEqualTo
(
PropertyNameFieldNamingStrategy
.
class
);
});
}
@Test
public
void
customFieldNamingStrategy
()
{
testFieldNamingStrategy
(
CamelCaseAbbreviatingFieldNamingStrategy
.
class
.
getName
(),
CamelCaseAbbreviatingFieldNamingStrategy
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.field-naming-strategy:"
+
CamelCaseAbbreviatingFieldNamingStrategy
.
class
.
getName
())
.
run
((
context
)
->
{
MongoMappingContext
mappingContext
=
context
.
getBean
(
MongoMappingContext
.
class
);
FieldNamingStrategy
fieldNamingStrategy
=
(
FieldNamingStrategy
)
ReflectionTestUtils
.
getField
(
mappingContext
,
"fieldNamingStrategy"
);
assertThat
(
fieldNamingStrategy
.
getClass
())
.
isEqualTo
(
CamelCaseAbbreviatingFieldNamingStrategy
.
class
);
});
}
@Test
public
void
interfaceFieldNamingStrategy
()
{
try
{
testFieldNamingStrategy
(
FieldNamingStrategy
.
class
.
getName
(),
null
);
fail
(
"Create FieldNamingStrategy interface should fail"
);
}
// We seem to have an inconsistent exception, accept either
catch
(
BeanCreationException
ex
)
{
// Expected
}
this
.
contextRunner
.
withPropertyValues
(
"spring.data.mongodb.field-naming-strategy:"
+
FieldNamingStrategy
.
class
.
getName
())
.
run
((
context
)
->
assertThat
(
context
).
getFailure
()
.
isInstanceOf
(
BeanCreationException
.
class
));
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
entityScanShouldSetInitialEntitySet
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
EntityScanConfig
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoMappingContext
mappingContext
=
this
.
context
.
getBean
(
MongoMappingContext
.
class
);
Set
<
Class
<?>>
initialEntitySet
=
(
Set
<
Class
<?>>)
ReflectionTestUtils
.
getField
(
mappingContext
,
"initialEntitySet"
);
assertThat
(
initialEntitySet
).
containsOnly
(
City
.
class
,
Country
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
EntityScanConfig
.
class
)
.
run
((
context
)
->
{
MongoMappingContext
mappingContext
=
context
.
getBean
(
MongoMappingContext
.
class
);
Set
<
Class
<?>>
initialEntitySet
=
(
Set
<
Class
<?>>)
ReflectionTestUtils
.
getField
(
mappingContext
,
"initialEntitySet"
);
assertThat
(
initialEntitySet
).
containsOnly
(
City
.
class
,
Country
.
class
);
});
}
@Test
public
void
registersDefaultSimpleTypesWithMappingContext
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoMappingContext
context
=
this
.
context
.
getBean
(
MongoMappingContext
.
class
);
BasicMongoPersistentEntity
<?>
entity
=
context
.
getPersistentEntity
(
Sample
.
class
);
MongoPersistentProperty
dateProperty
=
entity
.
getPersistentProperty
(
"date"
);
assertThat
(
dateProperty
.
isEntity
()).
isFalse
();
}
this
.
contextRunner
.
run
((
context
)
->
{
MongoMappingContext
mappingContext
=
context
.
getBean
(
MongoMappingContext
.
class
);
BasicMongoPersistentEntity
<?>
entity
=
mappingContext
.
getPersistentEntity
(
Sample
.
class
);
MongoPersistentProperty
dateProperty
=
entity
.
getPersistentProperty
(
"date"
);
assertThat
(
dateProperty
.
isEntity
()).
isFalse
();
});
public
void
testFieldNamingStrategy
(
String
strategy
,
Class
<?
extends
FieldNamingStrategy
>
expectedType
)
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
if
(
strategy
!=
null
)
{
TestPropertyValues
.
of
(
"spring.data.mongodb.field-naming-strategy:"
+
strategy
)
.
applyTo
(
this
.
context
);
}
this
.
context
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoMappingContext
mappingContext
=
this
.
context
.
getBean
(
MongoMappingContext
.
class
);
FieldNamingStrategy
fieldNamingStrategy
=
(
FieldNamingStrategy
)
ReflectionTestUtils
.
getField
(
mappingContext
,
"fieldNamingStrategy"
);
assertThat
(
fieldNamingStrategy
.
getClass
()).
isEqualTo
(
expectedType
);
}
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java
View file @
c8c32cfa
...
...
@@ -16,13 +16,13 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
data
.
mongo
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
;
import
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration
;
import
org.springframework.
context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.
boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.data.mongodb.core.ReactiveMongoTemplate
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -34,23 +34,17 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public
class
MongoReactiveDataAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
,
MongoReactiveDataAutoConfiguration
.
class
));
@Test
public
void
templateExists
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
,
MongoReactiveAutoConfiguration
.
class
,
MongoReactiveDataAutoConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeanNamesForType
(
ReactiveMongoTemplate
.
class
))
.
hasSize
(
1
);
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
)
.
hasSingleBean
(
ReactiveMongoTemplate
.
class
));
}
}
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