How to mock a method call on the constructor using easymock?
-
I have a class that receives a factory as an argument that is called inside the constructor. It must be called there because the object should be fully initalized before it can be used. Something like: class MyClass { private Foo foo; MyClass(FooFactory fooFactory, Bar bar) { this.foo = fooFactory.newFoo(bar); } } An instance of Foo cannot be directly passed to the constructor, so consider that this is a restriction of the problem. MyClass cannot be tested using EasyMock, because fooFactory would be a mock that was not initialized in the setUp: private MyClass myClass; public void setUp() { FooFactory fooFactory = mock(fooFactory); Bar bar = new Bar(); myClass = new MyClass(fooFactory, bar) } So when setUp is called, the call for fooFactory.newFoo(bar) inside the constructor will fail with an IllegalStateException, because no such expectations were set. The question is the, how can you mock method calls in the constructor?
-
Answer:
You can EasyMock.reset(fooFactory) in setUp after MyClass is created.
peninha at Stack Overflow Visit the source
Related Q & A:
- How to invoke a method at particular time asynchronously every day in Mac application?Best solution by Ask Different
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How to create a Restful web service in .Net Using MySQL?Best solution by stackoverflow.com
- How to call a method once everyday?Best solution by Stack Overflow
- How long is a typical call back for a job?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.