Am new to mockito and am stuck with a situation where I have to test
some code which uses factories.
The method to test is something like this :
public Integer save(session, dto) {
PersistenceManagerFactory persistMgrFactory =
PersistenceManagerFactory .getInstance();
PersistenceManager aPersistanceMgr =
persistMgrFactory.getPersistenceManager(A.class) ;
aPersistanceMgr.load(session, dto.getInt() ) ; // I want to stub this
method which am unable to do
}
I can pass mock session and dto and stub methods on them, but am not
able to mock the aPersistanceMgr or stub the methods on it.
Any pointer/pseudo code would be much appreciated.
Thanks,
Rahul
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
On Jan 5, 7:14 pm, Kartik Kumar <krishnan.1...@gmail.com> wrote:
> I don't think that static code can be mocked by Mockito. You probably have
> to do it at byte code level. PowerMock can do it for you. Perhaps this may
> help
>
> http://code.google.com/p/powermock/wiki/MockitoUsage13
>
> On Tue, Jan 5, 2010 at 2:20 PM, Rahul <rahu...@gmail.com> wrote:
> > Hi All,
>
> > Am new to mockito and am stuck with a situation where I have to test
> > some code which uses factories.
>
> > The method to test is something like this :
>
> > public Integer save(session, dto) {
> > PersistenceManagerFactory persistMgrFactory =
> > PersistenceManagerFactory .getInstance();
> > PersistenceManager aPersistanceMgr =
> > persistMgrFactory.getPersistenceManager(A.class) ;
> > aPersistanceMgr.load(session, dto.getInt() ) ; // I want to stub this
> > method which am unable to do
> > }
>
> > I can pass mock session and dto and stub methods on them, but am not
> > able to mock the aPersistanceMgr or stub the methods on it.
>
> > Any pointer/pseudo code would be much appreciated.
>
> > Thanks,
> > Rahul
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mockito" group.
> > To post to this group, send email to moc...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com>
I guess this would solve the issue. But then I would have to change
the signature of the save() method.
If this was one method could have thought about it, but we have this
throughout the code base and it would become a major re-factoring
effort.
Thanks,
Rahul
On Jan 5, 7:27 pm, Michael Hackett <mhack...@kanayo.com> wrote:
> Rahul,
>
> You can't mock global objects (like the Singleton
> PersistenceManagerFactory), unless you can replace the instance it returns.
> Instead, you should pass the factory into the save method, or the
> constructor of the class the method belongs to. Then you can create a mock
> PersistenceManagerFactory and pass it in.
>
> Your test code might look something like this:
>
> PersistenceManagerFactory mockFactory =
> mock(PersistenceManagerFactory.class);
> PersistenceManager mockManager = mock(PersistenceManager.class);
> when(mockManager).load(...) ...
>
> when(mockFactory).getPersistenceManager(any(A.class)).thenReturn(mockManager);
>
> xxx.save(mockFactory, session, dto);
>
> verify(...);
>
> Cheers,
> -- Michael
>
> 2010/1/5 Rahul <rahu...@gmail.com>
Otherwise, you're going to need a setInstance() method on your Singletons, just for testing, which I personally don't like, but might get you through until you can perform the necessary refactorings.
Thanks,
Rahul
On Jan 5, 9:56 pm, Michael Hackett <mhack...@kanayo.com> wrote:
>
> It's going to very difficult to use Mockito if your objects instantiate or
> retrieve the service objects they use, instead of having them passed in.
> Otherwise, you're going to need a setInstance() method on your Singletons,
> just for testing, which I personally don't like, but might get you through
> until you can perform the necessary refactorings.
>
Michael/kartik,
Will try a combination of what you guys have suggested.
Am using mockito-all-jvm14-1.8.0.jar as the we are using jdk 1.4.
Powermock 1.3 needs at least jdk1.5 due to the annotations. Any work
around for this?
Thanks,
Rahul
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
>Michael/kartik,
>Will try a combination of what you guys have suggested.
>Am using mockito-all-jvm14-1.8.0.jar as the we are using jdk 1.4.
>Powermock 1.3 needs at least jdk1.5 due to the annotations. Any work
>around for this?
You shouldn't do it. Add new constructor so that you can pass in the
dependency from the test (you can have this constructor
package-protected if you like).
Really, really, **really** it does not make any sense to apply fancy
frameworks, googling for advanced solutions when there is a very
simple way of achieving the goal: improve testability.
//existing constructor:
public Foo() {
this(PersistenceManagerFactory.getInstance());
}
//new constructor:
Foo(PersistenceManagerFactory factory)
{
this.factory = factory;
}
For any new code: **avoid accessing singletons directly**. Instead
pass in the instances (for example via constructor parameters). Read
Misko Hevery's blog entries about singletons.
Hope that helps.
Szczepan Faber
PS.
I wasn't shouting :) just my humble suggestions.
Rahul