Junit-using-assertion
JUnit-アサーションの使用
アサーション
すべてのアサーションはAssertクラスにあります。
public class Assert extends java.lang.Object
このクラスは、テストの記述に役立つ一連のアサーションメソッドを提供します。 失敗したアサーションのみが記録されます。 Assertクラスの重要なメソッドのいくつかは次のとおりです-
Sr.No. | Methods & Description |
---|---|
1 |
void assertEquals(boolean expected, boolean actual) 2つのプリミティブ/オブジェクトが等しいことを確認します。 |
2 |
void assertTrue(boolean condition) 条件が真であることを確認します。 |
3 |
void assertFalse(boolean condition) 条件が偽であることを確認します。 |
4 |
void assertNotNull(Object object) オブジェクトがnullでないことを確認します。 |
5 |
void assertNull(Object object) オブジェクトがnullであることを確認します。 |
6 |
void assertSame(object1, object2) assertSame()メソッドは、2つのオブジェクト参照が同じオブジェクトを指しているかどうかをテストします。 |
7 |
void assertNotSame(object1, object2) assertNotSame()メソッドは、2つのオブジェクト参照が同じオブジェクトを指していないかどうかをテストします。 |
8 |
void assertArrayEquals(expectedArray, resultArray); assertArrayEquals()メソッドは、2つの配列が互いに等しいかどうかをテストします。 |
上記の方法のいくつかを例で使用してみましょう。 C:\> JUNIT_WORKSPACEに TestAssertions.java という名前のJavaクラスファイルを作成します。
import org.junit.Test;
import static org.junit.Assert.*;
public class TestAssertions {
@Test
public void testAssertions() {
//test data
String str1 = new String ("abc");
String str2 = new String ("abc");
String str3 = null;
String str4 = "abc";
String str5 = "abc";
int val1 = 5;
int val2 = 6;
String[] expectedArray = {"one", "two", "three"};
String[] resultArray = {"one", "two", "three"};
//Check that two objects are equal
assertEquals(str1, str2);
//Check that a condition is true
assertTrue (val1 < val2);
//Check that a condition is false
assertFalse(val1 > val2);
//Check that an object isn't null
assertNotNull(str1);
//Check that an object is null
assertNull(str3);
//Check if two object references point to the same object
assertSame(str4,str5);
//Check if two object references not point to the same object
assertNotSame(str1,str3);
//Check whether two arrays are equal to each other.
assertArrayEquals(expectedArray, resultArray);
}
}
次に、C:\> JUNIT_WORKSPACEに TestRunner.java という名前のJavaクラスファイルを作成して、テストケースを実行します。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestAssertions.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
javacを使用して、テストケースとテストランナークラスをコンパイルします。
C:\JUNIT_WORKSPACE>javac TestAssertions.java TestRunner.java
テストランナーを実行します。これにより、提供されたテストケースクラスで定義されたテストケースが実行されます。
C:\JUNIT_WORKSPACE>java TestRunner
出力を確認します。
true
アノテーション
注釈は、コードに追加してメソッドやクラスに適用できるメタタグのようなものです。 JUnitのこれらの注釈は、テストメソッドに関する次の情報を提供します-
- どのメソッドがテストメソッドの前後に実行されるか。
- どのメソッドがすべてのメソッドの前後に実行されるか、および。 *実行中に無視されるメソッドまたはクラス。
次の表は、JUnitでの注釈とその意味のリストです-
Sr.No. | Annotation & Description |
---|---|
1 |
Testアノテーションは、JUnitにアタッチされたパブリックvoidメソッドをテストケースとして実行できることを伝えます。 |
2 |
@Before いくつかのテストでは、実行する前に同様のオブジェクトを作成する必要があります。 public voidメソッドに@Beforeの注釈を付けると、各Testメソッドの前にそのメソッドが実行されます。 |
3 |
@After Beforeメソッドで外部リソースを割り当てる場合、テストの実行後にそれらを解放する必要があります。 @Afterを使用してpublic voidメソッドに注釈を付けると、そのメソッドはTestメソッドの後に実行されます。 |
4 |
@BeforeClass @BeforeClassを使用してpublic static voidメソッドに注釈を付けると、クラス内のテストメソッドの前に1回実行されます。 |
5 |
@AfterClass これは、すべてのテストが終了した後にメソッドを実行します。 これを使用して、クリーンアップアクティビティを実行できます。 |
6 |
@Ignore Ignore注釈は、テストを無視するために使用され、そのテストは実行されません。 |
C:\> JUNIT_WORKSPACEに JunitAnnotation.java という名前のJavaクラスファイルを作成して、注釈をテストします。
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class JunitAnnotation {
//execute before class
@BeforeClass
public static void beforeClass() {
System.out.println("in before class");
}
//execute after class
@AfterClass
public static void afterClass() {
System.out.println("in after class");
}
//execute before test
@Before
public void before() {
System.out.println("in before");
}
//execute after test
@After
public void after() {
System.out.println("in after");
}
//test case
@Test
public void test() {
System.out.println("in test");
}
//test case ignore and will not execute
@Ignore
public void ignoreTest() {
System.out.println("in ignore test");
}
}
次に、C:\> JUNIT_WORKSPACEに TestRunner.java という名前のJavaクラスファイルを作成して、注釈を実行します。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitAnnotation.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
javacを使用して、テストケースとテストランナークラスをコンパイルします。
C:\JUNIT_WORKSPACE>javac JunitAnnotation.java TestRunner.java
テストランナーを実行します。これにより、提供されたテストケースクラスで定義されたテストケースが実行されます。
C:\JUNIT_WORKSPACE>java TestRunner
出力を確認します。
in before class
in before
in test
in after
in after class
true