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
d16838e4
Commit
d16838e4
authored
Jul 27, 2020
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.3.x'
Closes gh-22584
parents
4827e84a
39cb9128
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
1 deletion
+136
-1
DefinitionsParser.java
...ngframework/boot/test/mock/mockito/DefinitionsParser.java
+8
-1
MockBeanContextCachingTests.java
...k/boot/test/mock/mockito/MockBeanContextCachingTests.java
+128
-0
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/DefinitionsParser.java
View file @
d16838e4
...
...
@@ -18,6 +18,7 @@ package org.springframework.boot.test.mock.mockito;
import
java.lang.reflect.AnnotatedElement
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.TypeVariable
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
...
...
@@ -114,7 +115,13 @@ class DefinitionsParser {
types
.
add
(
ResolvableType
.
forClass
(
clazz
));
}
if
(
types
.
isEmpty
()
&&
element
instanceof
Field
)
{
types
.
add
(
ResolvableType
.
forField
((
Field
)
element
,
source
));
Field
field
=
(
Field
)
element
;
if
(
field
.
getGenericType
()
instanceof
TypeVariable
)
{
types
.
add
(
ResolvableType
.
forField
(
field
,
source
));
}
else
{
types
.
add
(
ResolvableType
.
forField
(
field
));
}
}
return
types
;
}
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockBeanContextCachingTests.java
0 → 100644
View file @
d16838e4
/*
* Copyright 2012-2020 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
*
* https://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
.
mock
.
mockito
;
import
java.util.Map
;
import
org.junit.jupiter.api.AfterEach
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTestContextBootstrapper
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.context.BootstrapContext
;
import
org.springframework.test.context.MergedContextConfiguration
;
import
org.springframework.test.context.TestContext
;
import
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
;
import
org.springframework.test.context.cache.DefaultContextCache
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Tests for application context caching when using {@link MockBean @MockBean}.
*
* @author Andy Wilkinson
*/
class
MockBeanContextCachingTests
{
private
final
DefaultContextCache
contextCache
=
new
DefaultContextCache
();
private
final
DefaultCacheAwareContextLoaderDelegate
delegate
=
new
DefaultCacheAwareContextLoaderDelegate
(
this
.
contextCache
);
@AfterEach
@SuppressWarnings
(
"unchecked"
)
void
clearCache
()
{
Map
<
MergedContextConfiguration
,
ApplicationContext
>
contexts
=
(
Map
<
MergedContextConfiguration
,
ApplicationContext
>)
ReflectionTestUtils
.
getField
(
this
.
contextCache
,
"contextMap"
);
for
(
ApplicationContext
context
:
contexts
.
values
())
{
if
(
context
instanceof
ConfigurableApplicationContext
)
{
((
ConfigurableApplicationContext
)
context
).
close
();
}
}
this
.
contextCache
.
clear
();
}
@Test
void
whenThereIsANormalBeanAndAMockBeanThenTwoContextsAreCreated
()
{
bootstrapContext
(
TestClass
.
class
);
assertThat
(
this
.
contextCache
.
size
()).
isEqualTo
(
1
);
bootstrapContext
(
MockedBeanTestClass
.
class
);
assertThat
(
this
.
contextCache
.
size
()).
isEqualTo
(
2
);
}
@Test
void
whenThereIsTheSameMockedBeanInEachTestClassThenOneContextIsCreated
()
{
bootstrapContext
(
MockedBeanTestClass
.
class
);
assertThat
(
this
.
contextCache
.
size
()).
isEqualTo
(
1
);
bootstrapContext
(
AnotherMockedBeanTestClass
.
class
);
assertThat
(
this
.
contextCache
.
size
()).
isEqualTo
(
1
);
}
@SuppressWarnings
(
"rawtypes"
)
private
void
bootstrapContext
(
Class
<?>
testClass
)
{
SpringBootTestContextBootstrapper
bootstrapper
=
new
SpringBootTestContextBootstrapper
();
BootstrapContext
bootstrapContext
=
mock
(
BootstrapContext
.
class
);
given
((
Class
)
bootstrapContext
.
getTestClass
()).
willReturn
(
testClass
);
bootstrapper
.
setBootstrapContext
(
bootstrapContext
);
given
(
bootstrapContext
.
getCacheAwareContextLoaderDelegate
()).
willReturn
(
this
.
delegate
);
TestContext
testContext
=
bootstrapper
.
buildTestContext
();
testContext
.
getApplicationContext
();
}
@SpringBootTest
(
classes
=
TestConfiguration
.
class
)
static
class
TestClass
{
}
@SpringBootTest
(
classes
=
TestConfiguration
.
class
)
static
class
MockedBeanTestClass
{
@MockBean
private
TestBean
testBean
;
}
@SpringBootTest
(
classes
=
TestConfiguration
.
class
)
static
class
AnotherMockedBeanTestClass
{
@MockBean
private
TestBean
testBean
;
}
@Configuration
static
class
TestConfiguration
{
@Bean
TestBean
testBean
()
{
return
new
TestBean
();
}
}
static
class
TestBean
{
}
}
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