Skip to content

Instantly share code, notes, and snippets.

@ryu22e
Created August 6, 2010 13:46
Show Gist options
  • Save ryu22e/511336 to your computer and use it in GitHub Desktop.
Save ryu22e/511336 to your computer and use it in GitHub Desktop.
テストコード中でprivateなコンストラクタを呼ぶ(カバレッジを100%にするために)
/**
* ここにテスト対象クラスの完全修飾名を入れる。
*/
private static String CLASS_NAME = "Sample";
@Test
public void testSample() throws Exception {
try {
Class<?> sampleClass = Class.forName(CLASS_NAME);
Constructor<?>[] sample = sampleClass.getDeclaredConstructors();
sample[0].setAccessible(true);
Object object = sample[0].newInstance();
assertNotNull(object);
assertThat(object, instanceOf(Sample.class));
} catch (Exception e) {
fail(e.getMessage());
}
}
@ryu22e
Copy link
Author

ryu22e commented Aug 6, 2010

下記のimport文が必要。

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.lang.reflect.Constructor;

import org.junit.Test;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment