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
64be6ae5
Commit
64be6ae5
authored
Sep 06, 2018
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'gh-11077'
parents
a32cd196
bc357225
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
296 additions
and
58 deletions
+296
-58
MockitoPostProcessor.java
...ramework/boot/test/mock/mockito/MockitoPostProcessor.java
+49
-57
MockitoPostProcessorTests.java
...ork/boot/test/mock/mockito/MockitoPostProcessorTests.java
+158
-1
SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java
...estFieldForExistingBeanWithQualifierIntegrationTests.java
+89
-0
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java
View file @
64be6ae5
This diff is collapsed.
Click to expand it.
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java
View file @
64be6ae5
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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.
...
...
@@ -26,9 +26,11 @@ import org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.boot.test.mock.mockito.example.ExampleService
;
import
org.springframework.boot.test.mock.mockito.example.FailingExampleService
;
import
org.springframework.boot.test.mock.mockito.example.RealExampleService
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -84,6 +86,79 @@ public class MockitoPostProcessorTests {
.
isTrue
();
}
@Test
public
void
canMockPrimaryBean
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
MockitoPostProcessor
.
register
(
context
);
context
.
register
(
MockPrimaryBean
.
class
);
context
.
refresh
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
MockPrimaryBean
.
class
).
mock
)
.
isMock
()).
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
ExampleService
.
class
)).
isMock
())
.
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"examplePrimary"
,
ExampleService
.
class
))
.
isMock
()).
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"exampleQualified"
,
ExampleService
.
class
))
.
isMock
()).
isFalse
();
}
@Test
public
void
canMockQualifiedBeanWithPrimaryBeanPresent
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
MockitoPostProcessor
.
register
(
context
);
context
.
register
(
MockQualifiedBean
.
class
);
context
.
refresh
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
MockQualifiedBean
.
class
).
mock
)
.
isMock
()).
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
ExampleService
.
class
)).
isMock
())
.
isFalse
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"examplePrimary"
,
ExampleService
.
class
))
.
isMock
()).
isFalse
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"exampleQualified"
,
ExampleService
.
class
))
.
isMock
()).
isTrue
();
}
@Test
public
void
canSpyPrimaryBean
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
MockitoPostProcessor
.
register
(
context
);
context
.
register
(
SpyPrimaryBean
.
class
);
context
.
refresh
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
SpyPrimaryBean
.
class
).
spy
).
isSpy
())
.
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
ExampleService
.
class
)).
isSpy
())
.
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"examplePrimary"
,
ExampleService
.
class
))
.
isSpy
()).
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"exampleQualified"
,
ExampleService
.
class
))
.
isSpy
()).
isFalse
();
}
@Test
public
void
canSpyQualifiedBeanWithPrimaryBeanPresent
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
MockitoPostProcessor
.
register
(
context
);
context
.
register
(
SpyQualifiedBean
.
class
);
context
.
refresh
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
SpyQualifiedBean
.
class
).
spy
)
.
isSpy
()).
isTrue
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
ExampleService
.
class
)).
isSpy
())
.
isFalse
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"examplePrimary"
,
ExampleService
.
class
))
.
isSpy
()).
isFalse
();
assertThat
(
Mockito
.
mockingDetails
(
context
.
getBean
(
"exampleQualified"
,
ExampleService
.
class
))
.
isSpy
()).
isTrue
();
}
@Configuration
@MockBean
(
SomeInterface
.
class
)
static
class
MockedFactoryBean
{
...
...
@@ -137,6 +212,88 @@ public class MockitoPostProcessorTests {
}
@Configuration
static
class
MockPrimaryBean
{
@MockBean
(
ExampleService
.
class
)
private
ExampleService
mock
;
@Bean
@Qualifier
(
"test"
)
public
ExampleService
exampleQualified
()
{
return
new
RealExampleService
(
"qualified"
);
}
@Bean
@Primary
public
ExampleService
examplePrimary
()
{
return
new
RealExampleService
(
"primary"
);
}
}
@Configuration
static
class
MockQualifiedBean
{
@MockBean
(
ExampleService
.
class
)
@Qualifier
(
"test"
)
private
ExampleService
mock
;
@Bean
@Qualifier
(
"test"
)
public
ExampleService
exampleQualified
()
{
return
new
RealExampleService
(
"qualified"
);
}
@Bean
@Primary
public
ExampleService
examplePrimary
()
{
return
new
RealExampleService
(
"primary"
);
}
}
@Configuration
static
class
SpyPrimaryBean
{
@SpyBean
(
ExampleService
.
class
)
private
ExampleService
spy
;
@Bean
@Qualifier
(
"test"
)
public
ExampleService
exampleQualified
()
{
return
new
RealExampleService
(
"qualified"
);
}
@Bean
@Primary
public
ExampleService
examplePrimary
()
{
return
new
RealExampleService
(
"primary"
);
}
}
@Configuration
static
class
SpyQualifiedBean
{
@SpyBean
(
ExampleService
.
class
)
@Qualifier
(
"test"
)
private
ExampleService
spy
;
@Bean
@Qualifier
(
"test"
)
public
ExampleService
exampleQualified
()
{
return
new
RealExampleService
(
"qualified"
);
}
@Bean
@Primary
public
ExampleService
examplePrimary
()
{
return
new
RealExampleService
(
"primary"
);
}
}
static
class
TestFactoryBean
implements
FactoryBean
<
Object
>
{
@Override
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java
0 → 100644
View file @
64be6ae5
/*
* Copyright 2012-2018 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
.
mock
.
mockito
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.mock.mockito.example.CustomQualifier
;
import
org.springframework.boot.test.mock.mockito.example.CustomQualifierExampleService
;
import
org.springframework.boot.test.mock.mockito.example.ExampleService
;
import
org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller
;
import
org.springframework.boot.test.mock.mockito.example.RealExampleService
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
verify
;
/**
* Test {@link SpyBean} on a test class field can be used to replace existing bean while
* preserving qualifiers.
*
* @author Andreas Neiser
*/
@RunWith
(
SpringRunner
.
class
)
public
class
SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests
{
@SpyBean
@CustomQualifier
private
ExampleService
service
;
@Autowired
private
ExampleServiceCaller
caller
;
@Autowired
private
ApplicationContext
applicationContext
;
@Test
public
void
testMocking
()
throws
Exception
{
this
.
caller
.
sayGreeting
();
verify
(
this
.
service
).
greeting
();
}
@Test
public
void
onlyQualifiedBeanIsReplaced
()
{
assertThat
(
this
.
applicationContext
.
getBean
(
"service"
)).
isSameAs
(
this
.
service
);
ExampleService
anotherService
=
this
.
applicationContext
.
getBean
(
"anotherService"
,
ExampleService
.
class
);
assertThat
(
anotherService
.
greeting
()).
isEqualTo
(
"Another"
);
}
@Configuration
static
class
TestConfig
{
@Bean
public
CustomQualifierExampleService
service
()
{
return
new
CustomQualifierExampleService
();
}
@Bean
public
ExampleService
anotherService
()
{
return
new
RealExampleService
(
"Another"
);
}
@Bean
public
ExampleServiceCaller
controller
(
@CustomQualifier
ExampleService
service
)
{
return
new
ExampleServiceCaller
(
service
);
}
}
}
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