Skip to content

Instantly share code, notes, and snippets.

@mid0111
Created February 7, 2014 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mid0111/8859159 to your computer and use it in GitHub Desktop.
Save mid0111/8859159 to your computer and use it in GitHub Desktop.
Junit spy static method.
public class Foo {
public static String testMethod() {
return "foo";
}
}
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Foo.class })
public class Snipets {
@Test
public void hoge() throws Exception {
PowerMockito.spy(Foo.class);
PowerMockito.when(Foo.class, "testMethod").thenReturn("dummy");
String actual = Foo.testMethod();
assertEquals("dummy", actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment