Closed GoogleCodeExporter closed 9 years ago
The thing is that suppressConstructor doesn't suppress the constructor in the
class
that you specify as the argument (see the documentation) unless you prepare the
class
that instantiates the actual class. It only suppress the constructor in the
super
class of the class (if the super class is not java.lang.Object). So in your case
suppressConstructor shouldn't do anything at all normally, which is why it
works when
you rename the test case. However if you where to to prepare the actual test
case for
test (i.e. prepare MTest.class as well) the constructor of Mock would indeed be
suppressed because it's MTest that creates the instance of Mock. Do you see the
difference? So in your particular case the suppressConstructor shouldn't
actually
acheive anything (unless you add MTest.class to the @PrepareForTest
annotation). You
might as well remove it actually.
However in this case when the test is named MockTest it (MockTest.class)
actually
gets prepared even though you haven't specified it. This is where the
problem/bug is.
The reason for this is that MockTest is located in the same package and starts
with
the same name as your class under test (Mock.class). PowerMock then prepares
both
Mock.class and MockTest.class which now causes the constructor of Mock to be
suppressed and thus the field is never initialized. If you would have moved
MockTest
to a different package or renamed it to something not starting with "Mock" it
wouldn't have prepared MockTest for and thus the constructor of Mock wouldn't
have
been suppressed.
Original comment by johan.ha...@gmail.com
on 21 Aug 2009 at 7:07
Perhaps the test case should always be prepared for test automatically? This
would
make it easier to under stand how to final system classes as well.
Original comment by johan.ha...@gmail.com
on 21 Aug 2009 at 7:20
Original comment by johan.ha...@gmail.com
on 21 Aug 2009 at 7:21
Original comment by johan.ha...@gmail.com
on 21 Aug 2009 at 7:21
Original comment by johan.ha...@gmail.com
on 21 Aug 2009 at 7:22
[deleted comment]
Perhaps we should make use of wildcards (at least *) if subpackages should also
be
removed?
Original comment by johan.ha...@gmail.com
on 8 Sep 2009 at 6:11
Classes that were accidentally prepared for test automatically if the fully
qualified
name of the class started with the same fully qualified name as a class that
were
prepared for test. This has now been resolved. This may lead to backward
incompatibility issues in cases where tests didn't prepare all necessary
artifacts
for test.
Original comment by johan.ha...@gmail.com
on 19 Sep 2009 at 3:25
Original issue reported on code.google.com by
johan.ha...@gmail.com
on 21 Aug 2009 at 6:31