Created
February 15, 2014 07:27
-
-
Save juzow13/9015683 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mockit.Mock; | |
import mockit.MockUp; | |
import org.junit.Before; | |
import org.junit.Test; | |
private TestTarget target; | |
/* | |
複数のテストから呼び出せるようにメソッドとして書きました。 | |
new MockUp<ClassName>としてブロックの中は@MockのAnnotationのあとにMockしたいメソッドを書きます。 | |
initializeの処理をMockする場合は下記のように$clinit内に書きます。 | |
*/ | |
private void mockMyStaticClass(){ | |
new MockUp<MyStaticClass>(){ | |
@Mock void $clinit() { System.out.println("こうするとコンストラクタもMockできる!"); } | |
@Mock boolean isNew(String someParam){ return true; } | |
@Mock int getYears(String someParam){ return 10; } | |
}; | |
} | |
@Before | |
public void beforeEach(){ | |
target = new TestTarget(); // この中でMyStaticClassが使われているイメージ | |
} | |
@Test | |
public void testSomething(){ | |
mockMyStaticClass(); | |
assertEquals("10 years", target.methodUsingStaticClassInside()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment