Mock an autowired spring dependency (Interface) which is not directly used by the test classInjecting Mockito mocks into a Spring beanTesting Private method using mockitoMockito: Trying to spy on method is calling the original methodmocking/spying private member of super classUsing autowired dependencies with certain mock dependency in Spring4Mock an Autowired ExecutorServiceSpring autowiring for unit tests, overriding dependency with mockMock autowired generic interfaceMock autowired dependency in JUnit 5 test for Spring Boot 2 appSpring 4, Mockito 2, Junit 4 in Eclipse Oxygen - DAO not mocked
How do I lift the insulation blower into the attic?
Weird lines in Microsoft Word
Recursively move files within sub directories
Showing mass murder in a kid's book
Exposing a company lying about themselves in a tightly knit industry (videogames) : Is my career at risk on the long run?
Why is "la Gestapo" feminine?
Unfrosted light bulb
Why didn't Voldemort know what Grindelwald looked like?
A seasonal riddle
Amorphous proper classes in MK
Connection Between Knot Theory and Number Theory
What properties make a magic weapon befit a Rogue more than a DEX-based Fighter?
Writing in a Christian voice
Calculate Pi using Monte Carlo
Output visual diagram of picture
Why doesn't Gödel's incompleteness theorem apply to false statements?
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
How can a new country break out from a developed country without war?
Does capillary rise violate hydrostatic paradox?
Can you take a "free object interaction" while incapacitated?
categorizing a variable turns it from insignificant to significant
New Order #2: Turn My Way
Why do Radio Buttons not fill the entire outer circle?
"Oh no!" in Latin
Mock an autowired spring dependency (Interface) which is not directly used by the test class
Injecting Mockito mocks into a Spring beanTesting Private method using mockitoMockito: Trying to spy on method is calling the original methodmocking/spying private member of super classUsing autowired dependencies with certain mock dependency in Spring4Mock an Autowired ExecutorServiceSpring autowiring for unit tests, overriding dependency with mockMock autowired generic interfaceMock autowired dependency in JUnit 5 test for Spring Boot 2 appSpring 4, Mockito 2, Junit 4 in Eclipse Oxygen - DAO not mocked
Question (in short) - how do I mock a spring dependency that is not directly referenced in my JUnit.
Below is the setup that am working with.
ServiceA is an interface and its implementation requires a handful of autowired dependencies for its many logic to work.
public interface ServiceA
void invoke();
...10 more methods
@Service
public class ServiceAImpl implements ServiceA
@Autowired
private ServiceB serviceB;
@Autowired
private ServiceC serviceC;
@Autowired
private DAOA daoA;
@Autowired
private DAOB daoB;
@Override
public void invoke(arg1, arg2)
// use above dependencies to do something
serviceB.sendEmail(args);
ServiceB sends an email by invoking yet another util dependency.
public interface ServiceB
void sendEmail();
...10 more methods
@Service
public class ServiceBImpl implements ServiceB{
@Autowired
private EmailUtil emailUtil;
@Override
public void sendEmail(args)
emailUtil.sendEmail(args);
Its worth noting that the sendEmail method returns void.
@Component
publIt'sclass EmailUtil
@Autowired
private JavaMailSenderImpl javaMailSenderImpl;
public void sendEmail(args)
// send email using spring & other APIs
My integration test JUnit looks like this.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@Test
public void testA()
// prepare data mock in H2 DB
serviceA.invoke(arg1, arg2);
// assertions
Question (long) - Am using H2 as mock DB so my DAO calls can (need to) function. I want to mock other 3rd party integrations, such as emailUtil.sendEmail call in serviceB so that email is not sent. Since in my JUnit am using serviceA, my understanding is that, I need to create @Mock of EmailUtil and @InjectMocks it into ServiceB which is @InjectMocks into ServiceA. When I do this, Mockito throws errors saying ServiceB is an interface. Creating mock objects of ServiceA, ServiceB may not be a great option, because then I might have to stub/mock too many behaviors. I want the actual logic to be executed in these classes, the mocking should be done only for sendEmail method.
- Is this a common problem?
- How can I address this?
- Is there a problem with the design of this code (i.e. using
interfaces and autowiring everything), which makes it difficult to
test/mock. If yes, what's a better way to do this?
Spring - 4.0.3.RELEASE, JUnit - 4.12, mockito-core - 1.10.19
java spring mockito junit4
add a comment |
Question (in short) - how do I mock a spring dependency that is not directly referenced in my JUnit.
Below is the setup that am working with.
ServiceA is an interface and its implementation requires a handful of autowired dependencies for its many logic to work.
public interface ServiceA
void invoke();
...10 more methods
@Service
public class ServiceAImpl implements ServiceA
@Autowired
private ServiceB serviceB;
@Autowired
private ServiceC serviceC;
@Autowired
private DAOA daoA;
@Autowired
private DAOB daoB;
@Override
public void invoke(arg1, arg2)
// use above dependencies to do something
serviceB.sendEmail(args);
ServiceB sends an email by invoking yet another util dependency.
public interface ServiceB
void sendEmail();
...10 more methods
@Service
public class ServiceBImpl implements ServiceB{
@Autowired
private EmailUtil emailUtil;
@Override
public void sendEmail(args)
emailUtil.sendEmail(args);
Its worth noting that the sendEmail method returns void.
@Component
publIt'sclass EmailUtil
@Autowired
private JavaMailSenderImpl javaMailSenderImpl;
public void sendEmail(args)
// send email using spring & other APIs
My integration test JUnit looks like this.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@Test
public void testA()
// prepare data mock in H2 DB
serviceA.invoke(arg1, arg2);
// assertions
Question (long) - Am using H2 as mock DB so my DAO calls can (need to) function. I want to mock other 3rd party integrations, such as emailUtil.sendEmail call in serviceB so that email is not sent. Since in my JUnit am using serviceA, my understanding is that, I need to create @Mock of EmailUtil and @InjectMocks it into ServiceB which is @InjectMocks into ServiceA. When I do this, Mockito throws errors saying ServiceB is an interface. Creating mock objects of ServiceA, ServiceB may not be a great option, because then I might have to stub/mock too many behaviors. I want the actual logic to be executed in these classes, the mocking should be done only for sendEmail method.
- Is this a common problem?
- How can I address this?
- Is there a problem with the design of this code (i.e. using
interfaces and autowiring everything), which makes it difficult to
test/mock. If yes, what's a better way to do this?
Spring - 4.0.3.RELEASE, JUnit - 4.12, mockito-core - 1.10.19
java spring mockito junit4
can you show how you are mocking thatEmailUtil
?
– Deadpool
Mar 7 at 2:17
add a comment |
Question (in short) - how do I mock a spring dependency that is not directly referenced in my JUnit.
Below is the setup that am working with.
ServiceA is an interface and its implementation requires a handful of autowired dependencies for its many logic to work.
public interface ServiceA
void invoke();
...10 more methods
@Service
public class ServiceAImpl implements ServiceA
@Autowired
private ServiceB serviceB;
@Autowired
private ServiceC serviceC;
@Autowired
private DAOA daoA;
@Autowired
private DAOB daoB;
@Override
public void invoke(arg1, arg2)
// use above dependencies to do something
serviceB.sendEmail(args);
ServiceB sends an email by invoking yet another util dependency.
public interface ServiceB
void sendEmail();
...10 more methods
@Service
public class ServiceBImpl implements ServiceB{
@Autowired
private EmailUtil emailUtil;
@Override
public void sendEmail(args)
emailUtil.sendEmail(args);
Its worth noting that the sendEmail method returns void.
@Component
publIt'sclass EmailUtil
@Autowired
private JavaMailSenderImpl javaMailSenderImpl;
public void sendEmail(args)
// send email using spring & other APIs
My integration test JUnit looks like this.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@Test
public void testA()
// prepare data mock in H2 DB
serviceA.invoke(arg1, arg2);
// assertions
Question (long) - Am using H2 as mock DB so my DAO calls can (need to) function. I want to mock other 3rd party integrations, such as emailUtil.sendEmail call in serviceB so that email is not sent. Since in my JUnit am using serviceA, my understanding is that, I need to create @Mock of EmailUtil and @InjectMocks it into ServiceB which is @InjectMocks into ServiceA. When I do this, Mockito throws errors saying ServiceB is an interface. Creating mock objects of ServiceA, ServiceB may not be a great option, because then I might have to stub/mock too many behaviors. I want the actual logic to be executed in these classes, the mocking should be done only for sendEmail method.
- Is this a common problem?
- How can I address this?
- Is there a problem with the design of this code (i.e. using
interfaces and autowiring everything), which makes it difficult to
test/mock. If yes, what's a better way to do this?
Spring - 4.0.3.RELEASE, JUnit - 4.12, mockito-core - 1.10.19
java spring mockito junit4
Question (in short) - how do I mock a spring dependency that is not directly referenced in my JUnit.
Below is the setup that am working with.
ServiceA is an interface and its implementation requires a handful of autowired dependencies for its many logic to work.
public interface ServiceA
void invoke();
...10 more methods
@Service
public class ServiceAImpl implements ServiceA
@Autowired
private ServiceB serviceB;
@Autowired
private ServiceC serviceC;
@Autowired
private DAOA daoA;
@Autowired
private DAOB daoB;
@Override
public void invoke(arg1, arg2)
// use above dependencies to do something
serviceB.sendEmail(args);
ServiceB sends an email by invoking yet another util dependency.
public interface ServiceB
void sendEmail();
...10 more methods
@Service
public class ServiceBImpl implements ServiceB{
@Autowired
private EmailUtil emailUtil;
@Override
public void sendEmail(args)
emailUtil.sendEmail(args);
Its worth noting that the sendEmail method returns void.
@Component
publIt'sclass EmailUtil
@Autowired
private JavaMailSenderImpl javaMailSenderImpl;
public void sendEmail(args)
// send email using spring & other APIs
My integration test JUnit looks like this.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@Test
public void testA()
// prepare data mock in H2 DB
serviceA.invoke(arg1, arg2);
// assertions
Question (long) - Am using H2 as mock DB so my DAO calls can (need to) function. I want to mock other 3rd party integrations, such as emailUtil.sendEmail call in serviceB so that email is not sent. Since in my JUnit am using serviceA, my understanding is that, I need to create @Mock of EmailUtil and @InjectMocks it into ServiceB which is @InjectMocks into ServiceA. When I do this, Mockito throws errors saying ServiceB is an interface. Creating mock objects of ServiceA, ServiceB may not be a great option, because then I might have to stub/mock too many behaviors. I want the actual logic to be executed in these classes, the mocking should be done only for sendEmail method.
- Is this a common problem?
- How can I address this?
- Is there a problem with the design of this code (i.e. using
interfaces and autowiring everything), which makes it difficult to
test/mock. If yes, what's a better way to do this?
Spring - 4.0.3.RELEASE, JUnit - 4.12, mockito-core - 1.10.19
java spring mockito junit4
java spring mockito junit4
asked Mar 7 at 1:07
Cyriac GeorgeCyriac George
295
295
can you show how you are mocking thatEmailUtil
?
– Deadpool
Mar 7 at 2:17
add a comment |
can you show how you are mocking thatEmailUtil
?
– Deadpool
Mar 7 at 2:17
can you show how you are mocking that
EmailUtil
?– Deadpool
Mar 7 at 2:17
can you show how you are mocking that
EmailUtil
?– Deadpool
Mar 7 at 2:17
add a comment |
1 Answer
1
active
oldest
votes
In the test class ServiceIT
mock EmailUtil
using @MockBean
Annotation that can be used to add mocks to a Spring ApplicationContext. Can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@MockBean
private EmailUtil emailUtil;
@Test
public void testA()
// prepare data mock in H2 DB
//given(this.emailUtil.sendEmail(ArgumentMatchers.any()).willReturn( custom object);
serviceA.invoke(arg1, arg2);
// assertions
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…
– Deadpool
Mar 7 at 16:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034562%2fmock-an-autowired-spring-dependency-interface-which-is-not-directly-used-by-th%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the test class ServiceIT
mock EmailUtil
using @MockBean
Annotation that can be used to add mocks to a Spring ApplicationContext. Can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@MockBean
private EmailUtil emailUtil;
@Test
public void testA()
// prepare data mock in H2 DB
//given(this.emailUtil.sendEmail(ArgumentMatchers.any()).willReturn( custom object);
serviceA.invoke(arg1, arg2);
// assertions
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…
– Deadpool
Mar 7 at 16:06
add a comment |
In the test class ServiceIT
mock EmailUtil
using @MockBean
Annotation that can be used to add mocks to a Spring ApplicationContext. Can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@MockBean
private EmailUtil emailUtil;
@Test
public void testA()
// prepare data mock in H2 DB
//given(this.emailUtil.sendEmail(ArgumentMatchers.any()).willReturn( custom object);
serviceA.invoke(arg1, arg2);
// assertions
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…
– Deadpool
Mar 7 at 16:06
add a comment |
In the test class ServiceIT
mock EmailUtil
using @MockBean
Annotation that can be used to add mocks to a Spring ApplicationContext. Can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@MockBean
private EmailUtil emailUtil;
@Test
public void testA()
// prepare data mock in H2 DB
//given(this.emailUtil.sendEmail(ArgumentMatchers.any()).willReturn( custom object);
serviceA.invoke(arg1, arg2);
// assertions
In the test class ServiceIT
mock EmailUtil
using @MockBean
Annotation that can be used to add mocks to a Spring ApplicationContext. Can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/test-context.xml" )
public class ServiceIT
@Autowired
private ServiceA serviceA;
@MockBean
private EmailUtil emailUtil;
@Test
public void testA()
// prepare data mock in H2 DB
//given(this.emailUtil.sendEmail(ArgumentMatchers.any()).willReturn( custom object);
serviceA.invoke(arg1, arg2);
// assertions
answered Mar 7 at 2:21
DeadpoolDeadpool
7,2672729
7,2672729
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…
– Deadpool
Mar 7 at 16:06
add a comment |
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…
– Deadpool
Mar 7 at 16:06
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
Thanks for your response. It took me a while to retrofit this (@MockBean) into my project, but I was able to make a POC work...yay. It would have been of great help if I knew MockBean come from spring-boot-starter-test and I also need minimum spring 5.0.0 for this to work. As I mentioned in my original question, I was using 4.0.3. Perhaps, if it makes sense, you can add these details in your answer.
– Cyriac George
Mar 7 at 7:19
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…– Deadpool
Mar 7 at 16:06
@MockBean
is from 1.4.0 docs.spring.io/spring-boot/docs/current/api/org/springframework/…– Deadpool
Mar 7 at 16:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034562%2fmock-an-autowired-spring-dependency-interface-which-is-not-directly-used-by-th%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you show how you are mocking that
EmailUtil
?– Deadpool
Mar 7 at 2:17