Clarification about @Spy and @InjectMocks inside a @Service Spring Boot
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Well, i am very confused about @Spy and @Mock. In my understand @Spy will call real methods and @Mock/@InjectMocks don't, because it just a mock, then i need a stub (when.thenReturn) if i would like to change the behavior of a mock.
In my test class i have this code:
@RunWith(MockitoJUnitRunner.class)
public class CaixaServiceTest {
@InjectMocks
private CaixaService caixaService;
@Mock
private CaixaRepository caixaRepository;
So, CaixaRepository is a JpaRepository interface from Spring Data and CaixaService just have a very simple method:
public void calcular(){
int a = (int) Math.pow(1,3);
log.info(a);
}
If i call caixaRepository.findOne(id)
null should be returned because findOne is never called really, because it just a mock. This case works very well.
But when i call caixaService.calcular()
the method body is executed (shouldn't because it is a mock), so log.info(a)
is logged on my file.
I can't understand this behavior, because as i said in my understand @InjectMocks or @Mock shouldn't execute anything if stub not exists, this a @Spy task.
java mockito
add a comment |
Well, i am very confused about @Spy and @Mock. In my understand @Spy will call real methods and @Mock/@InjectMocks don't, because it just a mock, then i need a stub (when.thenReturn) if i would like to change the behavior of a mock.
In my test class i have this code:
@RunWith(MockitoJUnitRunner.class)
public class CaixaServiceTest {
@InjectMocks
private CaixaService caixaService;
@Mock
private CaixaRepository caixaRepository;
So, CaixaRepository is a JpaRepository interface from Spring Data and CaixaService just have a very simple method:
public void calcular(){
int a = (int) Math.pow(1,3);
log.info(a);
}
If i call caixaRepository.findOne(id)
null should be returned because findOne is never called really, because it just a mock. This case works very well.
But when i call caixaService.calcular()
the method body is executed (shouldn't because it is a mock), so log.info(a)
is logged on my file.
I can't understand this behavior, because as i said in my understand @InjectMocks or @Mock shouldn't execute anything if stub not exists, this a @Spy task.
java mockito
The service is not a mock. It's a real object, where the mocks are injected by Mockito. If everything was mocked, you would test any single line of your code.
– JB Nizet
Nov 23 '18 at 17:44
The service isn't a mock, it's properly instantiated, but its members filled with mocks
– daniu
Nov 23 '18 at 17:44
So InjectMocks works like Spy ?
– RonaldoLanhellas
Nov 23 '18 at 17:45
No. InjectMocks simply creates a real instance of the object, and injects the mocks and spies, which are found thanks the the Mock and Spy annotations. Why don't you read the javadoc?
– JB Nizet
Nov 23 '18 at 17:46
add a comment |
Well, i am very confused about @Spy and @Mock. In my understand @Spy will call real methods and @Mock/@InjectMocks don't, because it just a mock, then i need a stub (when.thenReturn) if i would like to change the behavior of a mock.
In my test class i have this code:
@RunWith(MockitoJUnitRunner.class)
public class CaixaServiceTest {
@InjectMocks
private CaixaService caixaService;
@Mock
private CaixaRepository caixaRepository;
So, CaixaRepository is a JpaRepository interface from Spring Data and CaixaService just have a very simple method:
public void calcular(){
int a = (int) Math.pow(1,3);
log.info(a);
}
If i call caixaRepository.findOne(id)
null should be returned because findOne is never called really, because it just a mock. This case works very well.
But when i call caixaService.calcular()
the method body is executed (shouldn't because it is a mock), so log.info(a)
is logged on my file.
I can't understand this behavior, because as i said in my understand @InjectMocks or @Mock shouldn't execute anything if stub not exists, this a @Spy task.
java mockito
Well, i am very confused about @Spy and @Mock. In my understand @Spy will call real methods and @Mock/@InjectMocks don't, because it just a mock, then i need a stub (when.thenReturn) if i would like to change the behavior of a mock.
In my test class i have this code:
@RunWith(MockitoJUnitRunner.class)
public class CaixaServiceTest {
@InjectMocks
private CaixaService caixaService;
@Mock
private CaixaRepository caixaRepository;
So, CaixaRepository is a JpaRepository interface from Spring Data and CaixaService just have a very simple method:
public void calcular(){
int a = (int) Math.pow(1,3);
log.info(a);
}
If i call caixaRepository.findOne(id)
null should be returned because findOne is never called really, because it just a mock. This case works very well.
But when i call caixaService.calcular()
the method body is executed (shouldn't because it is a mock), so log.info(a)
is logged on my file.
I can't understand this behavior, because as i said in my understand @InjectMocks or @Mock shouldn't execute anything if stub not exists, this a @Spy task.
java mockito
java mockito
asked Nov 23 '18 at 17:41
RonaldoLanhellasRonaldoLanhellas
77721431
77721431
The service is not a mock. It's a real object, where the mocks are injected by Mockito. If everything was mocked, you would test any single line of your code.
– JB Nizet
Nov 23 '18 at 17:44
The service isn't a mock, it's properly instantiated, but its members filled with mocks
– daniu
Nov 23 '18 at 17:44
So InjectMocks works like Spy ?
– RonaldoLanhellas
Nov 23 '18 at 17:45
No. InjectMocks simply creates a real instance of the object, and injects the mocks and spies, which are found thanks the the Mock and Spy annotations. Why don't you read the javadoc?
– JB Nizet
Nov 23 '18 at 17:46
add a comment |
The service is not a mock. It's a real object, where the mocks are injected by Mockito. If everything was mocked, you would test any single line of your code.
– JB Nizet
Nov 23 '18 at 17:44
The service isn't a mock, it's properly instantiated, but its members filled with mocks
– daniu
Nov 23 '18 at 17:44
So InjectMocks works like Spy ?
– RonaldoLanhellas
Nov 23 '18 at 17:45
No. InjectMocks simply creates a real instance of the object, and injects the mocks and spies, which are found thanks the the Mock and Spy annotations. Why don't you read the javadoc?
– JB Nizet
Nov 23 '18 at 17:46
The service is not a mock. It's a real object, where the mocks are injected by Mockito. If everything was mocked, you would test any single line of your code.
– JB Nizet
Nov 23 '18 at 17:44
The service is not a mock. It's a real object, where the mocks are injected by Mockito. If everything was mocked, you would test any single line of your code.
– JB Nizet
Nov 23 '18 at 17:44
The service isn't a mock, it's properly instantiated, but its members filled with mocks
– daniu
Nov 23 '18 at 17:44
The service isn't a mock, it's properly instantiated, but its members filled with mocks
– daniu
Nov 23 '18 at 17:44
So InjectMocks works like Spy ?
– RonaldoLanhellas
Nov 23 '18 at 17:45
So InjectMocks works like Spy ?
– RonaldoLanhellas
Nov 23 '18 at 17:45
No. InjectMocks simply creates a real instance of the object, and injects the mocks and spies, which are found thanks the the Mock and Spy annotations. Why don't you read the javadoc?
– JB Nizet
Nov 23 '18 at 17:46
No. InjectMocks simply creates a real instance of the object, and injects the mocks and spies, which are found thanks the the Mock and Spy annotations. Why don't you read the javadoc?
– JB Nizet
Nov 23 '18 at 17:46
add a comment |
1 Answer
1
active
oldest
votes
All is right but your understanding of @InjectMocks
.
Indeed annotating a field with it will not create a mock object as you think.
Instead of, it will try to inject the mock dependencies to the object referenced by the field where the annotation is.
Note that this way of injecting the dependencies is not explicit and so doesn't document the dependencies to mock in your test.
Besides if the dependencies injection fails, Mockito will not report any failure.
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
Inject it with@Mock
, no problem for that. But I would set them in the object under test in a@Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such asfoo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
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%2f53450994%2fclarification-about-spy-and-injectmocks-inside-a-service-spring-boot%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
All is right but your understanding of @InjectMocks
.
Indeed annotating a field with it will not create a mock object as you think.
Instead of, it will try to inject the mock dependencies to the object referenced by the field where the annotation is.
Note that this way of injecting the dependencies is not explicit and so doesn't document the dependencies to mock in your test.
Besides if the dependencies injection fails, Mockito will not report any failure.
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
Inject it with@Mock
, no problem for that. But I would set them in the object under test in a@Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such asfoo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
add a comment |
All is right but your understanding of @InjectMocks
.
Indeed annotating a field with it will not create a mock object as you think.
Instead of, it will try to inject the mock dependencies to the object referenced by the field where the annotation is.
Note that this way of injecting the dependencies is not explicit and so doesn't document the dependencies to mock in your test.
Besides if the dependencies injection fails, Mockito will not report any failure.
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
Inject it with@Mock
, no problem for that. But I would set them in the object under test in a@Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such asfoo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
add a comment |
All is right but your understanding of @InjectMocks
.
Indeed annotating a field with it will not create a mock object as you think.
Instead of, it will try to inject the mock dependencies to the object referenced by the field where the annotation is.
Note that this way of injecting the dependencies is not explicit and so doesn't document the dependencies to mock in your test.
Besides if the dependencies injection fails, Mockito will not report any failure.
All is right but your understanding of @InjectMocks
.
Indeed annotating a field with it will not create a mock object as you think.
Instead of, it will try to inject the mock dependencies to the object referenced by the field where the annotation is.
Note that this way of injecting the dependencies is not explicit and so doesn't document the dependencies to mock in your test.
Besides if the dependencies injection fails, Mockito will not report any failure.
answered Nov 23 '18 at 17:46
davidxxxdavidxxx
69.5k677104
69.5k677104
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
Inject it with@Mock
, no problem for that. But I would set them in the object under test in a@Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such asfoo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
add a comment |
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
Inject it with@Mock
, no problem for that. But I would set them in the object under test in a@Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such asfoo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
What do you suggest as a best practice to injecting my mock dependencies ?
– RonaldoLanhellas
Nov 23 '18 at 17:51
Inject it with
@Mock
, no problem for that. But I would set them in the object under test in a @Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such as foo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
Inject it with
@Mock
, no problem for that. But I would set them in the object under test in a @Before/@BeforeEach
(JUnit 4 / 5) method from the constructor call such as foo = new Foo(barMock, otherBarMock);
– davidxxx
Nov 23 '18 at 17:56
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
It's true, in this way i follow the best practices in OO. Thanks @davidxxx
– RonaldoLanhellas
Nov 23 '18 at 18:04
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
so i should remove the InjectMocks and create "new CaixaService(mock1,mock2..)" right ?
– RonaldoLanhellas
Nov 23 '18 at 18:06
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
Very right. You are welcome :)
– davidxxx
Nov 23 '18 at 18:11
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%2f53450994%2fclarification-about-spy-and-injectmocks-inside-a-service-spring-boot%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
The service is not a mock. It's a real object, where the mocks are injected by Mockito. If everything was mocked, you would test any single line of your code.
– JB Nizet
Nov 23 '18 at 17:44
The service isn't a mock, it's properly instantiated, but its members filled with mocks
– daniu
Nov 23 '18 at 17:44
So InjectMocks works like Spy ?
– RonaldoLanhellas
Nov 23 '18 at 17:45
No. InjectMocks simply creates a real instance of the object, and injects the mocks and spies, which are found thanks the the Mock and Spy annotations. Why don't you read the javadoc?
– JB Nizet
Nov 23 '18 at 17:46