Skip to content

Instantly share code, notes, and snippets.

@thomo
Created October 19, 2012 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomo/3916072 to your computer and use it in GitHub Desktop.
Save thomo/3916072 to your computer and use it in GitHub Desktop.
Unittest to verify that static method is called - PowerMock, Mockito, TestNG
package abc;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import java.awt.Component;
import javax.swing.JOptionPane;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.Test;
class MessageBox {
public void showErrorMessage(String msg) {
JOptionPane.showMessageDialog(null, msg, "Bar", JOptionPane.ERROR_MESSAGE);
}
}
@PrepareForTest(JOptionPane.class)
public class VerifyStaticCallTest extends PowerMockTestCase {
/**
* Unittest to verify that static method is called with the specified parameter.
*/
@Test
public void showErrorMessage() {
String msg = "foo";
PowerMockito.mockStatic(JOptionPane.class);
MessageBox cut = new MessageBox();
cut.showErrorMessage(msg);
PowerMockito.verifyStatic();
JOptionPane.showMessageDialog(any(Component.class),
eq(msg),
any(String.class),
eq(JOptionPane.ERROR_MESSAGE));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment