Tìm hi u công c ki m th JUnit
Mcl c
I. JUnit là gì ............................................................................................................. 2
1.
JUnit là gì?...................................................................................................... 2
2.
a Junit ................................................................................... 2
3.
Ví d JUnit v i Eclipse +Maven .................................................................... 2
3.1.
4.
.....................................................................................2
Ví d ................................................................................................................. 3
4.1.
Code Test ................................................................................................. 3
II.
Assertions ............................................................................................................. 6
III.
Test runner và Test suite.................................................................................... 8
1.
Test runners .................................................................................................... 8
2.
Annotation @RunWith................................................................................... 9
3.
Test Suite -- T o b
test v i Junit ................................................................... 9
IV.
Th t th c thi các class test trong m t test suite ............................................ 11
V.
Th t th c thi các method trong m t class test .................................................12
VI.
JUnit Expected Exceptions .............................................................................. 13
6.1.
S d ng expect exception ...................................................................... 13
VII. Ignoring tests.................................................................................................... 13
VIII.
..........................14
I.
1.
JUnit là gì
JUnit là gì?
--
Link download: https://www.jetbrains.com/idea/download/#section=windows
2.
:
3.
3.1.
src/main/java
test src/test/java)
:
4.
isPrimeNumber
public class Demo {
public boolean isPrimeNumber(int input) {
for (int i = 2; i < input; i++) {
if (input % i == 0)
return false;
}
return true;
}
}
4.1. Code Test:
public class TestDemo {
@Test
public void testIsPrimeNumber1() {
Demo demo1 = new Demo();
boolean result = demo1.isPrimeNumber(-1);
assertFalse(result);
}
@Test
public void testIsPrimeNumber2() {
Demo demo1 = new Demo();
boolean result = demo1.isPrimeNumber(0);
assertFalse(result);
}
@Test
public void testIsPrimeNumber3() {
Demo demo1 = new Demo();
boolean result = demo1.isPrimeNumber(1);
assertFalse(result);
}
@Test
public void testIsPrimeNumber4() {
Demo demo1 = new Demo();
boolean result = demo1.isPrimeNumber(2);
assertTrue(result);
}
@Test
public void testIsPrimeNumber5() {
Demo demo1 = new Demo();
boolean result = demo1.isPrimeNumber(4);
assertFalse(result);
}
@Test
public void testIsPrimeNumber6() {
Demo demo1 = new Demo();
boolean result = demo1.isPrimeNumber(5);
assertTrue(result);
}
}
dùng assertFalse
true nên mình dùng assertTrue
là true
eclipse
chúng ta
method isPrimeNumber
isPrimeNumber
public class Demo {
public boolean isPrimeNumber(int input) {
if (input < 2) {
return false;
}
for (int i = 2; i < input; i++) {
if (input % i == 0)
return false;
}
return true;
}
}
II.
Assertions:
trên chúng ta dùng assertFalse,assertTrue
assertEquals, assertArrayEquals
assertions trong JUnit, assertions
assert
là assert[ki u so sánh](expecteds_value, actuals_value)
sage, expecteds_value, actuals_value)
message
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;
assert[ki u so sánh](mes
public class AssertTests {
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
assertArrayEquals("failure - byte arrays not same", expected, actual);
}
@Test
public void testAssertEquals() {
assertEquals("failure - strings are not equal", "text", "text");
}
@Test
public void testAssertFalse() {
assertFalse("failure - should be false", false);
}
@Test
public void testAssertNotNull() {
assertNotNull("should not be null", new Object());
}
@Test
public void testAssertNotSame() {
assertNotSame("should not be same Object", new Object(), new Object());
}
@Test
public void testAssertNull() {
assertNull("should be null", null);
}
@Test
public void testAssertSame() {
Integer aNumber = Integer.valueOf(768);
assertSame("should be same", aNumber, aNumber);
}
// JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
assertThat("albumen", both(containsString("a")).and(containsString("b")));
}
@Test
public void testAssertThatHasItems() {
assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
}
@Test
public void testAssertThatEveryItemContainsString() {
assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }),
everyItem(containsString("n")));
}
// Core Hamcrest Matchers with assertThat
@Test
public void testAssertThatHamcrestCoreMatchers() {
assertThat("good", allOf(equalTo("good"), startsWith("good")));
assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
assertThat(7,
not(CombinableMatcher.<Integer>either(equalTo(3)).or(equalTo(4))));
assertThat(new Object(), not(sameInstance(new Object())));
}
@Test
public void testAssertTrue() {
assertTrue("failure - should be true", true);
}
}
III.
Test runner và Test suite:
1.
Test runners:
2.
Annotation @RunWith
3.
@RunWith(Suite.class)
Test Suite --
JUnit cùng lúc.
:
\
:
@RunWith(Suite.class)và @SuiteClasses(TestClass1.class, ...). Bên
trong @SuiteClasses
Class TestDemo
@Test
public void testIsPrimeNumber4() (
Demo demol = new Demo();
boolean result = demol.isPrimeNumber(2);
assertTrue(result);
@Test
public void testIsPrimeNumber5() (
Demo demol = new Demo();
boolean result = demol.isPrimeNumber(4);
assertEafse(result);
@Test
public void testIsPrimeNumber6() (
Demo demol = new Demo();
boolean result = demol.isPrimeNumber(5);
assertTrue(result);
Class TestPasswordValidator:
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runners.MethodSorters;
import static org.junit.Assert.*;
public class TestPasswordValidator {
@Test
public void testIsPasswordStrong() throws Exception{
PasswordValidator pv = new PasswordValidator();
boolean isstrong
pv.isPasswordStrong("12345678");
assertTrue(isStrong);
@Test
public void testIsPasswordNotStrong() throws Exception(
PasswordValidator pv = new PasswordValidator();
boolean isstrong = pv.isPasswordStrong("1234");
assertFafse(isStrong);
@Test
public void testIsPasswordTooLong() throws Exception{
PasswordValidator pv = new PasswordValidator();
assertTfrows(Exception.class, ()->
pv.isPasswordStrong("l23456vhvjhjhfhj"));
Tao bo test gom 2 class test la TestDemo.java và TestPasswordValidator.java
Demo --
IV.
Th t th c thi các class test trong m t test suite:
trong annotation @SuiteClasses
TestDemo
TestPasswordValidator.java trong
TestDemo
TestPasswordValidator
TestPasswordValidator
V.
Th t th c thi các method trong m t class test:
annotation @FixMethodOrder
@FixMethodOrder(MethodSorters.DEFAULT)
@FixMethodOrder
@FixMethodOrder(MethodSorters.JVM)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
MethodSorters.NAME_ASCENDING
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestA {
@Test
public void testA2() {
System.out.println("testA2");
assertTrue(true);
}
@Test
public void testA() {
System.out.println("testA");
assertTrue(true);
}
@Test
public void testA1() {
System.out.println("testA1");
assertTrue(true);
}
}
VI.
JUnit Expected Exceptions:
6.1. S d ng expect exception:
exception thì chúng ta
:
VII.
Ignoring tests:
@Test
@Ignore
annotation @Test
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
public class DemoIgnoreTest {
@Ignore("Test is ignored as a demonstration")
@Test
public void testEquals() {
String str = "stackjava.com";
assertEquals(str, "stackjava.com");
}
@Test
public void testTrue() {
assertTrue(true);
}
@Test
public void testFalse() {
assertFalse(false);
}
}
VIII.
T
:
Chit thtch - Annotations
Chñ thich
Ser miéu tâ
@BeforeEach
Thirc hien trwñc moi bñi kiem tra. Dirwc sir dung dé chuan bi
moi trirñng thir nghiem, vI du: khdi tao cac trirñng trong lip
thir nghiem, cau hinh mñi truñng, v.v.
@AfterEach
Thuc hien sau moi lan kiem tra. Dupc sir dung de don dep moi
trircrng thir nghiem, vI du: xña dii lieu tarn thai, khoi phuc gia tr|
mac dinh, don dep cau truc bo nhér dat tien.
@DisplayName("<Name>")
<Ten> se dirwc hien thi bñi ngtrdi chay thir nghiem. Ngtrwc lai
véri ten phtrcrng thirc, ten cñ the châa khofing trang de car thien
kha nang dpc.
@BeforeAll
Chu thich mpt phirwng thuc tinh dirpc thuc thi mpt lan, trtrñc
khi bat dau tat ca cac this nghiem. Nñ dirpc sir dung de thuc
hien cac hoat dong tñn nhieu théri gran, vi du, de ket nñi véri cv
sd dii lieu. Cac phwcrng thuc dwpc danh dau bang chu thich nñy
can dirpc xac dinh la s fiafii chodt dong véri JUnit.
@AfterAll
Chu thlch mot phuwng thuc tinh dwpc thuc thi mot lan, sau khi
tat ca cac thir nghiem dñ ket thuc. Nñ dirwc sir dung de thrrc
hien cac hoat dong don dep, vi du, de ngat ket nñi khñi cv st dir
lieu. Cac phirwng thuc dirwc chu thich bang chu thlch nay can
dirpc xac dinh la s I afii choat dong vñi JUnit.
@TestFactory
Chu thich mot phirwng phap la mot Nha may de tao cac thir
nghiem dong
@Nested
Cho phép ban long cac lérp kiem tra ben trong de nhñm cac bñi
kiem tra cua ban vñ cñ thém cac phwcrng thirc @BeforeEach va
@AfterEach.
é Tag ( "<TaqName >" )
The mot phirwng phap kiém tra, cac bai kiém tra trong JUnit 5
co the dirwc loc theo thé. Vi du: chi chay cac bai kiem tra dirwc
gan thé "nhanh".
@ExtendWith
Cho phép ban dang ky mpt lip MW rong bñ sung chic nang cho
cac bai kiem tra
1
I
1
1
1
1
1
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )