Tag Archives: testing

Mock private variable

Use PowerMock to mock private variable. MyClass.java public class MyClass { private String str; public String getStr(){ return str; } } TestDrive.java public class TestDrive { @Test public void test1() { MyClass myClass = new MyClass(); try { MemberModifier.field(MyClass.class, “str”).set(myClass, “This is a mock string injected by test.”); } catch (IllegalAccessException e) { e.printStackTrace(); }… Read More »

Simple easymock example

EasyMock can mock an interface, or class. In this way, we can test our code without implementing them. In order to use the static functions in easyMock, we can either extend it, or use “import static” key word. I think using “import static” key word is more general. Let’s start review the code. main/java — foo.bar.MyInterface… Read More »