reset() für mockStatic()

722 views
Skip to first unread message

Alex

unread,
Sep 22, 2010, 8:56:34 AM9/22/10
to PowerMock
Hi,

what about adding a method reset(Class<?>... classes) to PowerMockito?

There is a reset method in the original Mockito but it only takes mock
objects and it's not possible to use it for classes that were mocked
using mockStatic.

I know that reset is generally considered bad practice but I don't
think there's another solution to my problem.

I want to do the following:

1) At set up the test needs to instantiate an object. During the
constructor several static methods on another class
are invoked
2) Now I want to test a specific method called print() of the
instantiated object. The method print() calls some of the static
methods that were also called by the constructor
3) Now I need to say verifyStatic(times(2)) in my test which I think
is really bad because I want to specify only what the method print()
does

If you wonder how bad my code must be if a constructor invokes the
same static methods that a method called print() invokes ... well I
think it sounds really ugly indeed ^ ^ But it's because I'm
programming OpenGL. In the constructor the object is initialized
calling GL11.glBindTexture(...) to load a texture into OpenGL. In the
print method this is again called to bind the texture to the current
OpenGL state.

@Test
public void print() throws Exception {
mockOpenGL();
mockGlGenBuffers(new IntGenerator());
BitmapFont bitmapFont = new BitmapFont(...);

bitmapFont.print("A", new Point(33.42f, 21.04f), new Color(0, 1, 2,
3));

// @formatter:off
verifyStatic(); GL11.glPushAttrib(GL11.GL_CURRENT_BIT);
verifyStatic(); GL11.glColor4f(0, 1, 2, 3);
verifyStatic(Mockito.times(2));
GL11.glBindTexture(eq(GL11.GL_TEXTURE_2D), anyInt()); <-- SEE THIS
LINE
....

Alex

unread,
Sep 22, 2010, 9:07:48 AM9/22/10
to PowerMock
I just found out that using mockStatic twice basically resets the
mock. But still I think a reset method would be good because that name
clearer communicates what actually happens ...

Johan Haleby

unread,
Sep 22, 2010, 9:08:44 AM9/22/10
to powe...@googlegroups.com
You're right! We should add it to the API. Thanks for pointing out, I'll add it as an issue. 

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Reply all
Reply to author
Forward
0 new messages