java


Mockito + Spy: How to gather return values


I got a class using a factory for creating some object.
In my unit test I would like to access the return value of the factory.
Since the factory is directly passed to the class and no getter for the created object is provided I need to intercept returning the object from the factory.
RealFactory factory = new RealFactory();
RealFactory spy = spy(factory);
TestedClass testedClass = new TestedClass(factory);
// At this point I would like to get a reference to the object created
// and returned by the factory.
Is there a possibility to access the return value of the factory? Probably using the spy?
The only way I can see is to mock the factory create method...
Regards
The standard mocking approach would be to:
Pre-create the object you want the factory to return in the test case
Create a mock (or spy) of the factory
Prescribe the mock factory to return your pre-created object.
If you really want to have the RealFactory create the object on the fly, you can subclass it and override the factory method to call super.create(...), then save the reference to a field accessible by the test class, and then return the created object.
First thing, you should be passing spy in as the constructor argument.
That aside, here's how you could do it.
public class ResultCaptor<T> implements Answer {
private T result = null;
public T getResult() {
return result;
}
#Override
public T answer(InvocationOnMock invocationOnMock) throws Throwable {
result = (T) invocationOnMock.callRealMethod();
return result;
}
}
Intended usage:
RealFactory factory = new RealFactory();
RealFactory spy = spy(factory);
TestedClass testedClass = new TestedClass(spy);
// At this point I would like to get a reference to the object created
// and returned by the factory.
// let's capture the return values from spy.create()
ResultCaptor<RealThing> resultCaptor = new ResultCaptor<>();
doAnswer(resultCaptor).when(spy).create();
// do something that will trigger a call to the factory
testedClass.doSomething();
// validate the return object
assertThat(resultCaptor.getResult())
.isNotNull()
.isInstanceOf(RealThing.class);

Related Links

if/else statement based on comma count
neo4j: Using graph algorithm in cypher
Are raw types always bad?
org.apache.http.NoHttpResponseException: The target server failed to respond: Rollback data
Throwable and Exception in Java
how to show variable's value in pop up dialog with java swing
Array index out of bounds in Android Studio
Automation screenshot
Problems following arquillian-graphene guide
Automating Jenkins pages with selenium
Regex for 8,2 decimal with thousand separator in JAVA
What is the best way to deal with EDT/non-EDT issues with JUnit?
How do i encrypt a message to ascii characters without using big integer? java rsa
Java FX, function not working properly
fix cannot find symbol in public class java
Get SVN buildnumber of specific branch in Maven

Categories

HOME
winforms
multithreading
pdf
deployment
heroku
iot
syntax
analysis
flyway4
podio
webrequest
kibana-4
spring-kafka
gz
leiningen
messages
hex-editors
hapi
netflix
iolanguage
seaborn
footer
invantive-sql
reactcsstransitiongroup
ehcache
sox
jndi
claims-based-identity
info.plist
tooltipster
minitab
wtx
dbclient
jaxb2
kendo-datasource
procdump
gsmcomm
gsoap
nameservers
yadcf
karaf
asset-pipeline
catalog
neuroscience
streamsets
reportbuilder
particles.js
unoconv
android-tabhost
hp-ux
grid.mvc
dynamic-reports
password-encryption
pubmed
nstextview
drupal-6
darcs
nbconvert
watchconnectivity
azure-sdk
measures
mathematica-frontend
autorest
storekit
ready-api
qgraphicsview
pagerank
iad
feedback
computer-algebra-systems
muse
map-projections
directoryservices
jsonpickle
knuth
java-metro-framework
castle
project-planning
wp-query
xceed-datagrid
ember-charts
srs
terminal-services
blending
odata4j
dotnetnuke-5
selected
dsn
viewswitcher
appconkit
uiviewanimation-curve
nsmanagedobject
appender
radcombobox
asp.net-mvc-areas
revisions
asp.net-profiles
avatar
xetex
temporal-database
sustainable-pace
post-build
rtml
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App