일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- reactor
- reactive
- 서버운영
- Spring Batch
- 웹앱
- spring reactive
- ipTIME
- reactor core
- Spring Framework
- 웹 커리큘럼
- 웹 스터디
- 공유기 서버
- Today
- Total
Hello World
[펌]How to mock Spring bean (version 2) 본문
About a year ago, I wrote a blog post how to mock Spring Bean. Patterns described there were little bit invasive to the production code. As one of the readers Colin correctly pointed out in comment, there is better alternative to spy/mock Spring bean based on annotation. This blog post is going to describe this technique. I used this approach with success at work and also in my side projects.
Note that widespread mocking in your application is often considered as design smell.
Introducing production code
First of all we need code under test to demonstrate mocking. We will use these simple classes:
Of course this code doesn’t make much sense, but will be good to demonstrate how to mock Spring bean.
just returns string and thus simulates read from some data source. It is autowired into . This bean is autowired into , which is used to construct string with user name and address.Notice that we are using constructor injection as field injection is considered as bad practice. If you want to enforce constructor injection for your application, Oliver Gierke (Spring ecosystem developer and Spring Data lead) recently created very nice project Ninjector.
Configuration which scans all these beans is pretty standard Spring Boot main class:
Mock Spring bean (without AOP)
Let’s test the
class where we mock . We can create this mock via Spring’ and@Primary annotations this way:This test configuration will be applied only when Spring profile
is active. When it’s applied, it registers bean of type , which is mock instance created by Mockito. annotation tells Spring to use this instance instead of real one when somebody autowire bean.Test class is using JUnit framework:
We activate profile
to enable mocking. Annotation is needed for Spring integration tests and defines which Spring configuration will be used to construct context for testing. Before the test, we autowire instance of under test and mock.Subsequent testing method should be clear if you are using Mockito. In
phase, we record desired behavior into mock instance. In phase, we execute testing code and in phase, we verify if testing code returned value we expect.Spy on Spring Bean (without AOP)
For spying example, will be spying on
instance:This Spring configuration will be component scanned only if profile
will be active. It defines primary bean of type . tells Spring to use this instance in case two beans of this type are present in Spring context. During construction of this bean we autowire existing instance of from Spring context and use Mockito’s spying feature. The bean we are registering is effectively delegating all the calls to original instance, but Mockito spying allows us to verify interactions on spied instance.We will test behavior of
this way:For testing we activate
profile so our spying configuration will be applied. We autowire which is under test and , which is being spied via Mockito.We don’t need to prepare any behavior for testing in
phase. phase is obviously executing code under test. In phase we verify if testing code returned value we expect and also if call was executed with correct parameter.Problems with Mockito and Spring AOP
Let say now we want to use Spring AOP module to handle some cross-cutting concerns. For example to log calls on our Spring beans this way:
This AOP Aspect is applied before call on Spring beans from package
. It is using Lombok’s annotation to log signature of called method. Notice that this bean is created only when profile is defined. We are using this profile to separate AOP and non-AOP testing examples. In a real application you wouldn’t want to use such profile.We also need to enable AspectJ for our application, therefore all the following examples will be using this Spring Boot main class:
AOP constructs are enabled by
.But such AOP constructs may be problematic if we combine Mockito for mocking with Spring AOP. It is because both use CGLIB to proxy real instances and when Mockito proxy is wrapped into Spring proxy, we can experience type mismatch problems. These can be mitigated by configuring bean’s scope with
, but Mockito calls still fail with . Such problems can be seen if we enable profile for .Mock Spring bean proxied by Spring AOP
To overcome these problems, we will wrap mock into this Spring bean:
annotation makes sure that this bean will take precedence before real bean during injection. To make sure it will be applied only for specific test, we define profile for this bean. It inherits class, so that it can act as full replacement of that type.
In order to fake behavior, we define mock instance of type
, which is exposed via getter defined by Lombok’s annotation. We also implement method which is expected to be called during test. This method just delegates the call to mock instance.The test where this mock is used can look like this:
In the test we define
profile to activate and profile to activate AOP aspect. For testing, we autowire testing bean and its faked dependency . As we know, will be of type , because this bean was marked as . Therefore we can cast it and record behavior into .When we call testing method, recorded behavior should be used because we expect testing method to use
dependency.Spy on Spring bean proxied by Spring AOP
Similar pattern can be used for spying the real implementation. This is how our spy can look like:
As we can see this spy is very similar to
. But in this case real bean is using constructor injection to autowire its dependency. Therefore we’ll need to define non-default constructor and do constructor injection also. But we wouldn’t pass injected dependency into parent constructor.To enable spying on real object, we construct new instance with all the dependencies, wrap it into Mockito spy instance and store it into
property. We expect call of method during test, therefore we delegate this call to . This property can be accessed in test via getter defined by Lombok’s annotation.Test itself would look like this:
It is very straight forward. Profile
ensures that will be scanned. Profile ensures same for aspect. When we autowire testing object and its dependency , we know that we can cast it to and verify the call on its property after calling the testing method.Fake Spring bean proxied by Spring AOP
It is obvious that delegating calls into Mockito mocks or spies complicates the testing. These patterns are often overkill if we simply need to fake the logic. We can use such fake in that case:
and used it for testing this way:
I don’t think this test needs explanation.
Reference: | How to mock Spring bean (version 2) from our JCG partner Lubos Krnac at the Lubos Krnac Java blogblog. |
출처: http://www.javacodegeeks.com/2016/01/mock-spring-bean-version-2.html
'Spring > Boot(4.x)' 카테고리의 다른 글
[펌]Spring Cloud AWS with proxy settings (0) | 2016.01.10 |
---|---|
[펌]Getting started with Spring JDBC in a web application (0) | 2016.01.10 |
[펌]Spring Data Redis Example (0) | 2016.01.10 |
[펌]Getting started with Spring Cloud (0) | 2016.01.10 |
Spring Dropbox API 연동 (0) | 2016.01.10 |