Open GoogleCodeExporter opened 9 years ago
You need to prepare the class that instantiates Inventory (i.e. does new) for
test.
Original comment by johan.ha...@gmail.com
on 5 Aug 2010 at 9:10
Hi Johan, The creation of Inventory object happens in my Test class as well as
my source class.
I am preparing my class under test. I also tried with preparing my test class
as well. But the error remains.
We could solve the problem by keeping a flag which was set in the replay()
methods. Until the flag was set, any new invocations were ignored. When
replay() is called, the flag is set and the present behavior continues.
If this kind of flag was provided as a setting, it would really help us. Please
let us know what you would recommend.
Thanks
Original comment by vivek.ka...@gmail.com
on 5 Aug 2010 at 9:56
It would really help if you could provide a small example (code under test as
well as a test case) that demonstrates the issue and I'll look into it.
Original comment by johan.ha...@gmail.com
on 8 Aug 2010 at 6:05
Johan, Please find sample source code attached...Running the given testcase
will recreate the problem.
Thanks for your help!
Original comment by vivek.ka...@gmail.com
on 9 Aug 2010 at 2:15
Attachments:
I've had a look at it now. The reason is that you do "new" after you've
expected a new of instance of the same type. E.g.
A a1 = new A();
expectNew(A.class).andReturn(a1);
A a2 = new A(); // Here a2 will be substituted for a1
expectNew(A.class).andReturn(a2); // Here powermock will scew up.
A work-around would be to declare your instances before any expectNew call:
A a1 = new A();
A a2 = new A();
expectNew(A.class).andReturn(a1);
expectNew(A.class).andReturn(a2);
(or more shortly: "expectNew(A.class).andReturn(a1).andReturn(a2);")
I'm not sure how to fix this in a good way or if it even should be fixed at all
(perhaps a better error message at least).
I'm attaching a refactored version of the test that uses the workaround I've
mentioned. I've also added a new version of the test which I think is a better
way to test the method (which doesn't use expectNew) that you may consider.
Original comment by johan.ha...@gmail.com
on 13 Aug 2010 at 1:17
Attachments:
Original issue reported on code.google.com by
vivek.ka...@gmail.com
on 4 Aug 2010 at 1:51