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
615cf63a
Commit
615cf63a
authored
May 19, 2020
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make MockBean resolve right type for abstract test class
Fixes gh-20916
parent
58974ab9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
11 deletions
+122
-11
DefinitionsParser.java
...ngframework/boot/test/mock/mockito/DefinitionsParser.java
+11
-11
AbstractMockBeanOnGenericExtensionTests.java
...mock/mockito/AbstractMockBeanOnGenericExtensionTests.java
+27
-0
AbstractMockBeanOnGenericTests.java
...oot/test/mock/mockito/AbstractMockBeanOnGenericTests.java
+84
-0
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/DefinitionsParser.java
View file @
615cf63a
...
@@ -60,20 +60,20 @@ class DefinitionsParser {
...
@@ -60,20 +60,20 @@ class DefinitionsParser {
}
}
void
parse
(
Class
<?>
source
)
{
void
parse
(
Class
<?>
source
)
{
parseElement
(
source
);
parseElement
(
source
,
null
);
ReflectionUtils
.
doWithFields
(
source
,
this
::
parseElement
);
ReflectionUtils
.
doWithFields
(
source
,
(
element
)
->
parseElement
(
element
,
source
)
);
}
}
private
void
parseElement
(
AnnotatedElement
element
)
{
private
void
parseElement
(
AnnotatedElement
element
,
Class
<?>
source
)
{
MergedAnnotations
annotations
=
MergedAnnotations
.
from
(
element
,
SearchStrategy
.
SUPERCLASS
);
MergedAnnotations
annotations
=
MergedAnnotations
.
from
(
element
,
SearchStrategy
.
SUPERCLASS
);
annotations
.
stream
(
MockBean
.
class
).
map
(
MergedAnnotation:
:
synthesize
)
annotations
.
stream
(
MockBean
.
class
).
map
(
MergedAnnotation:
:
synthesize
)
.
forEach
((
annotation
)
->
parseMockBeanAnnotation
(
annotation
,
element
));
.
forEach
((
annotation
)
->
parseMockBeanAnnotation
(
annotation
,
element
,
source
));
annotations
.
stream
(
SpyBean
.
class
).
map
(
MergedAnnotation:
:
synthesize
)
annotations
.
stream
(
SpyBean
.
class
).
map
(
MergedAnnotation:
:
synthesize
)
.
forEach
((
annotation
)
->
parseSpyBeanAnnotation
(
annotation
,
element
));
.
forEach
((
annotation
)
->
parseSpyBeanAnnotation
(
annotation
,
element
,
source
));
}
}
private
void
parseMockBeanAnnotation
(
MockBean
annotation
,
AnnotatedElement
element
)
{
private
void
parseMockBeanAnnotation
(
MockBean
annotation
,
AnnotatedElement
element
,
Class
<?>
source
)
{
Set
<
ResolvableType
>
typesToMock
=
getOrDeduceTypes
(
element
,
annotation
.
value
());
Set
<
ResolvableType
>
typesToMock
=
getOrDeduceTypes
(
element
,
annotation
.
value
()
,
source
);
Assert
.
state
(!
typesToMock
.
isEmpty
(),
()
->
"Unable to deduce type to mock from "
+
element
);
Assert
.
state
(!
typesToMock
.
isEmpty
(),
()
->
"Unable to deduce type to mock from "
+
element
);
if
(
StringUtils
.
hasLength
(
annotation
.
name
()))
{
if
(
StringUtils
.
hasLength
(
annotation
.
name
()))
{
Assert
.
state
(
typesToMock
.
size
()
==
1
,
"The name attribute can only be used when mocking a single class"
);
Assert
.
state
(
typesToMock
.
size
()
==
1
,
"The name attribute can only be used when mocking a single class"
);
...
@@ -86,8 +86,8 @@ class DefinitionsParser {
...
@@ -86,8 +86,8 @@ class DefinitionsParser {
}
}
}
}
private
void
parseSpyBeanAnnotation
(
SpyBean
annotation
,
AnnotatedElement
element
)
{
private
void
parseSpyBeanAnnotation
(
SpyBean
annotation
,
AnnotatedElement
element
,
Class
<?>
source
)
{
Set
<
ResolvableType
>
typesToSpy
=
getOrDeduceTypes
(
element
,
annotation
.
value
());
Set
<
ResolvableType
>
typesToSpy
=
getOrDeduceTypes
(
element
,
annotation
.
value
()
,
source
);
Assert
.
state
(!
typesToSpy
.
isEmpty
(),
()
->
"Unable to deduce type to spy from "
+
element
);
Assert
.
state
(!
typesToSpy
.
isEmpty
(),
()
->
"Unable to deduce type to spy from "
+
element
);
if
(
StringUtils
.
hasLength
(
annotation
.
name
()))
{
if
(
StringUtils
.
hasLength
(
annotation
.
name
()))
{
Assert
.
state
(
typesToSpy
.
size
()
==
1
,
"The name attribute can only be used when spying a single class"
);
Assert
.
state
(
typesToSpy
.
size
()
==
1
,
"The name attribute can only be used when spying a single class"
);
...
@@ -108,13 +108,13 @@ class DefinitionsParser {
...
@@ -108,13 +108,13 @@ class DefinitionsParser {
}
}
}
}
private
Set
<
ResolvableType
>
getOrDeduceTypes
(
AnnotatedElement
element
,
Class
<?>[]
value
)
{
private
Set
<
ResolvableType
>
getOrDeduceTypes
(
AnnotatedElement
element
,
Class
<?>[]
value
,
Class
<?>
source
)
{
Set
<
ResolvableType
>
types
=
new
LinkedHashSet
<>();
Set
<
ResolvableType
>
types
=
new
LinkedHashSet
<>();
for
(
Class
<?>
clazz
:
value
)
{
for
(
Class
<?>
clazz
:
value
)
{
types
.
add
(
ResolvableType
.
forClass
(
clazz
));
types
.
add
(
ResolvableType
.
forClass
(
clazz
));
}
}
if
(
types
.
isEmpty
()
&&
element
instanceof
Field
)
{
if
(
types
.
isEmpty
()
&&
element
instanceof
Field
)
{
types
.
add
(
ResolvableType
.
forField
((
Field
)
element
));
types
.
add
(
ResolvableType
.
forField
((
Field
)
element
,
source
));
}
}
return
types
;
return
types
;
}
}
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/AbstractMockBeanOnGenericExtensionTests.java
0 → 100644
View file @
615cf63a
/*
* 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
;
/**
* Concrete implementation of {@link AbstractMockBeanOnGenericTests}.
*
* @author Madhura Bhave
*/
class
AbstractMockBeanOnGenericExtensionTests
extends
AbstractMockBeanOnGenericTests
<
AbstractMockBeanOnGenericTests
.
ThingImpl
,
AbstractMockBeanOnGenericTests
.
SomethingImpl
>
{
}
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/AbstractMockBeanOnGenericTests.java
0 → 100644
View file @
615cf63a
/*
* 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
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Tests for {@link MockBean} with abstract class and generics.
*
* @author Madhura Bhave
*/
@SpringBootTest
(
classes
=
AbstractMockBeanOnGenericTests
.
TestConfiguration
.
class
)
abstract
class
AbstractMockBeanOnGenericTests
<
T
extends
AbstractMockBeanOnGenericTests
.
Thing
<
U
>,
U
extends
AbstractMockBeanOnGenericTests
.
Something
>
{
@Autowired
private
T
thing
;
@MockBean
private
U
something
;
@Test
void
mockBeanShouldResolveConcreteType
()
{
assertThat
(
this
.
something
).
isInstanceOf
(
SomethingImpl
.
class
);
}
abstract
static
class
Thing
<
T
extends
AbstractMockBeanOnGenericTests
.
Something
>
{
@Autowired
private
T
something
;
T
getSomething
()
{
return
this
.
something
;
}
void
setSomething
(
T
something
)
{
this
.
something
=
something
;
}
}
static
class
SomethingImpl
extends
Something
{
}
static
class
ThingImpl
extends
Thing
<
SomethingImpl
>
{
}
static
class
Something
{
}
@Configuration
static
class
TestConfiguration
{
@Bean
ThingImpl
thing
()
{
return
new
ThingImpl
();
}
}
}
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