Unit testing @Asynchronous method in a spy in java 7
up vote
0
down vote
favorite
I am trying to test some awful legacy code (Java 7)
I need to spy on a stateless service, but call an asynchronous method
I get the error
Object does not represent an actual Future
How should I try and test this?
@InjectMocks
private OrderDataImport spyImporter;
/* has spyImporter as a dependency */
@InjectMocks
private ImportService sut; /* system under test */
@Before
public void setUp() {
// create spy
spyImporter = Mockito.spy(new OrderDataImport());
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
sut.import();
}
Import Code
class ImportService {
@EJB
private OrderDataImport dependency;
public boolean import() {
List<Future<Result>> resultList = new ArrayList<>();
int THREAD_COUNT = 3;
while (THREAD_COUNT-- > 0)
resultList.add(dependency.create(...));
for (int i = 0; i < resultList.size(); i++) {
/* Object does not represent an actual Future */
Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
}
}
}
java junit mockito future
add a comment |
up vote
0
down vote
favorite
I am trying to test some awful legacy code (Java 7)
I need to spy on a stateless service, but call an asynchronous method
I get the error
Object does not represent an actual Future
How should I try and test this?
@InjectMocks
private OrderDataImport spyImporter;
/* has spyImporter as a dependency */
@InjectMocks
private ImportService sut; /* system under test */
@Before
public void setUp() {
// create spy
spyImporter = Mockito.spy(new OrderDataImport());
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
sut.import();
}
Import Code
class ImportService {
@EJB
private OrderDataImport dependency;
public boolean import() {
List<Future<Result>> resultList = new ArrayList<>();
int THREAD_COUNT = 3;
while (THREAD_COUNT-- > 0)
resultList.add(dependency.create(...));
for (int i = 0; i < resultList.size(); i++) {
/* Object does not represent an actual Future */
Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
}
}
}
java junit mockito future
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to test some awful legacy code (Java 7)
I need to spy on a stateless service, but call an asynchronous method
I get the error
Object does not represent an actual Future
How should I try and test this?
@InjectMocks
private OrderDataImport spyImporter;
/* has spyImporter as a dependency */
@InjectMocks
private ImportService sut; /* system under test */
@Before
public void setUp() {
// create spy
spyImporter = Mockito.spy(new OrderDataImport());
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
sut.import();
}
Import Code
class ImportService {
@EJB
private OrderDataImport dependency;
public boolean import() {
List<Future<Result>> resultList = new ArrayList<>();
int THREAD_COUNT = 3;
while (THREAD_COUNT-- > 0)
resultList.add(dependency.create(...));
for (int i = 0; i < resultList.size(); i++) {
/* Object does not represent an actual Future */
Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
}
}
}
java junit mockito future
I am trying to test some awful legacy code (Java 7)
I need to spy on a stateless service, but call an asynchronous method
I get the error
Object does not represent an actual Future
How should I try and test this?
@InjectMocks
private OrderDataImport spyImporter;
/* has spyImporter as a dependency */
@InjectMocks
private ImportService sut; /* system under test */
@Before
public void setUp() {
// create spy
spyImporter = Mockito.spy(new OrderDataImport());
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
sut.import();
}
Import Code
class ImportService {
@EJB
private OrderDataImport dependency;
public boolean import() {
List<Future<Result>> resultList = new ArrayList<>();
int THREAD_COUNT = 3;
while (THREAD_COUNT-- > 0)
resultList.add(dependency.create(...));
for (int i = 0; i < resultList.size(); i++) {
/* Object does not represent an actual Future */
Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
}
}
}
java junit mockito future
java junit mockito future
edited Nov 9 at 8:34
asked Nov 9 at 7:50
Jim
5,933638108
5,933638108
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Maybe you need mockito's static method when() , just try it
doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)
add a comment |
up vote
0
down vote
You should not spy on anything else in your test than the SUT itself.
OrderDataImport
is a dependency that you should aim to mock, not spy.
You also should use the @InjectMocks
on the SUT only:
@Mock
private OrderDataImport importerStub;
@InjectMocks
private ImportService sut;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
// Arrange
when(importerStub.create(..)).thenReturn(/* Future instance */);
// Act
sut.import();
}
In the end, I see that you don't really need any spying here.
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Maybe you need mockito's static method when() , just try it
doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)
add a comment |
up vote
0
down vote
accepted
Maybe you need mockito's static method when() , just try it
doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Maybe you need mockito's static method when() , just try it
doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)
Maybe you need mockito's static method when() , just try it
doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)
answered Nov 9 at 8:06
winters
612
612
add a comment |
add a comment |
up vote
0
down vote
You should not spy on anything else in your test than the SUT itself.
OrderDataImport
is a dependency that you should aim to mock, not spy.
You also should use the @InjectMocks
on the SUT only:
@Mock
private OrderDataImport importerStub;
@InjectMocks
private ImportService sut;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
// Arrange
when(importerStub.create(..)).thenReturn(/* Future instance */);
// Act
sut.import();
}
In the end, I see that you don't really need any spying here.
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
add a comment |
up vote
0
down vote
You should not spy on anything else in your test than the SUT itself.
OrderDataImport
is a dependency that you should aim to mock, not spy.
You also should use the @InjectMocks
on the SUT only:
@Mock
private OrderDataImport importerStub;
@InjectMocks
private ImportService sut;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
// Arrange
when(importerStub.create(..)).thenReturn(/* Future instance */);
// Act
sut.import();
}
In the end, I see that you don't really need any spying here.
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
add a comment |
up vote
0
down vote
up vote
0
down vote
You should not spy on anything else in your test than the SUT itself.
OrderDataImport
is a dependency that you should aim to mock, not spy.
You also should use the @InjectMocks
on the SUT only:
@Mock
private OrderDataImport importerStub;
@InjectMocks
private ImportService sut;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
// Arrange
when(importerStub.create(..)).thenReturn(/* Future instance */);
// Act
sut.import();
}
In the end, I see that you don't really need any spying here.
You should not spy on anything else in your test than the SUT itself.
OrderDataImport
is a dependency that you should aim to mock, not spy.
You also should use the @InjectMocks
on the SUT only:
@Mock
private OrderDataImport importerStub;
@InjectMocks
private ImportService sut;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public Test() {
// Arrange
when(importerStub.create(..)).thenReturn(/* Future instance */);
// Act
sut.import();
}
In the end, I see that you don't really need any spying here.
answered Nov 9 at 8:16


Maciej Kowalski
11.2k72037
11.2k72037
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
add a comment |
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
I need to spy on it, as it's part of the legacy code that needs to execute.
– Jim
Nov 9 at 8:31
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
So the ImportService is not the SUT actually here?
– Maciej Kowalski
Nov 9 at 8:33
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
– Jim
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
Then i would say you should test that inside a test class where OrderDataImport is the SUT
– Maciej Kowalski
Nov 9 at 8:35
add a comment |
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%2f53221689%2funit-testing-asynchronous-method-in-a-spy-in-java-7%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