Assert trong java la gi
Trong bài trước, chúng ta đã cùng tìm hiểu về một số Annotation cơ bản của JUnit. Trong bài này, chúng ta sẽ cùng tìm hiểu một số API của JUnit như Assert, Test Runner (JUnitCore), Test Suite, Assume. JUnit Assert ClassJUnit cung cấp các phương thức static để kiểm tra các điều kiện nhất định thông qua lớp Assert. Các phương thức này thường bắt đầu với assertXxx(). Nó cho phép chúng ta xác định thông báo lỗi (error message), kết quả mong đợi (expected) và kết quả thực tế (actual). Các phương thức Assert so sánh giá trị thực tế được trả về bởi một phương thức test với giá trị mong đợi, nó ném một AssertionException nếu so sánh thất bại. Chúng ta có thể sử dụng Assert để kiểm tra kết quả mong đợi cho các primitive type, Object, array primitive, array Object.
Mặc dù chúng ta có thể chỉ cần sử dụng phương thức assertTrue() cho gần như hầu hết các test case, tuy nhiên thì việc sử dụng một trong các phương thức assertXXX() cụ thể sẽ làm cho các test của chúng ta dễ hiểu hơn và cung cấp các thông điệp thất bại rõ ràng hơn. Ví dụ: package com.gpcoder.junit; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.both; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.everyItem; import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.Arrays; import org.hamcrest.core.CombinableMatcher; import org.junit.Test; public class AssertTest { @Test public void testAssertEquals() { assertEquals("failure - strings are not equal", "text", "text"); } @Test public void testAssertArrayEquals() { int[] arr1 = { 1, 2, 3 }; int[] arr2 = { 1, 2, 3 }; assertArrayEquals("failure - two arrays not same", arr1, arr2); } @Test public void testAssertTrue() { assertTrue("failure - should be true", true); } @Test public void testAssertFalse() { assertFalse("failure - should be false", false); } @Test public void testAssertNull() { assertNull("should be null", null); } @Test public void testAssertNotNull() { assertNotNull("should not be null", new Object()); } @Test public void testAssertSame() { Integer aNumber = Integer.valueOf(768); assertSame("should be same", aNumber, aNumber); } @Test public void testAssertNotSame() { assertNotSame("should not be same Object", new Object(), new Object()); } @Test public void testFail() throws Exception { Assert.fail("Make fails a test"); } }Ví dụ assertThat() và sử dụng Matcher của thư viện org.hamcrest chúng ta sẽ cùng tìm hiểu ở một bài viết khác. JUnit Assume ClassAnnotation @Ingore cho phép chúng ta sử dụng để đánh dấu phương thức này để được bỏ qua (ignore/ disable), không cần thực thi test. Một cách khác để làm việc này là sử dụng Assume.assumeXxx() để định nghĩa điều kiện Test.
Khi chúng ta sử dụng các phương thức Assume, nếu một test case bị fail, JUnit runner sẽ xem như là một phương thức được @Ignore. Ví dụ: package com.gpcoder.junit; import static org.junit.Assume.assumeNotNull; import static org.junit.Assume.assumeTrue; import org.junit.Test; public class AssumeTest { @Test public void assumeTrueTest() { assumeTrue(true); System.out.println("execute test"); } @Test public void assumeNotNullTest() { Object object = null; assumeNotNull(object); System.out.println("execute test"); } }Output của chương trình trên: Như bạn thấy, phương thức assumeNotNullTest() mặc dù là fail, nhưng với việc sử dụng assumeNotNull() nên phương thức test này được đánh dấu như là @Ignore. Bình thường, các IDE như NetBeans, Eclipse đều có sẵn trình chạy (runner) cho JUnit để hiển thị kết quả các test case, ví dụ: Chúng ta có thể gọi một API được hỗ trợ từ JUnit để thực thi các class test một cách thủ công thông qua JUnitCore. Để chạy từ chương trình Java ta sử dụng: org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);Để chạy từ console ta sử dụng: java org.junit.runner.JUnitCore TestClass1 [...other test classes...]Ví dụ: Output của chương trình: Test result: false Total of runtime in milliseconds: 9 Number of tests run: 9 Number of tests ignore: 0 Number of tests failure: 1 Failed -> testFail(com.gpcoder.junit.AssertTest): Make fails a testTài liệu tham khảo:
Chuyên mục: Unit Test Được gắn thẻ: JUnit Bình luậnbình luận |