JUnit :: หรือวิธีการรักเครื่องมือตรวจสอบ JavaRush
สร้างวัตถุ
ดังนั้นเพื่อที่จะทดสอบบางสิ่งบางอย่าง เราจำเป็นต้องมีวัตถุทดสอบก่อน เรามีงานรออยู่ข้างหน้า- เราต้องการวัตถุที่จะจัดเก็บข้อมูลเกี่ยวกับผู้ใช้
- Id - ต้องนับตามลำดับที่มีการเพิ่มผู้ใช้ใหม่
- ชื่อผู้ใช้.
- อายุของเขา.
- เพศ (ชาย/หญิง)
-
จำเป็นต้องจัดให้มีการจัดเก็บรายชื่อผู้ใช้
-
ชั้นจะต้องสามารถ
- สร้างรายชื่อผู้ใช้ทั้งหมด
- สร้างรายชื่อผู้ใช้ตามเพศ (ชาย/หญิง)
- ส่งกลับจำนวนผู้ใช้ในรายการทั่วไป และคำนวณจำนวนตามเพศของผู้ใช้
- คำนวณยอดรวมตามอายุของผู้ใช้และคำนึงถึงเพศด้วย
- คำนวณอายุเฉลี่ยทั้งโดยรวมและแยกตามเพศ
User
ที่จะมีฟิลด์:
private int id;
private String name;
private int age;
private Sex sex;
เพียงพอสำหรับการจัดเก็บข้อมูลผู้ใช้ มาดูกันว่าต้องมีอะไรอีกบ้างสำหรับงานนี้ เราจำเป็นต้องจัดเก็บผู้ใช้ทั้งหมด มาสร้างฟิลด์แบบคงที่ในคลาสของเรากันดีกว่าallUsers
ฉันคิดว่ามันก็โอเคถ้าเป็นMap<Integer, User>
private static Map<Integer, User> allUsers;
เรายังจำเป็นต้องกำหนดหมายเลขลำดับให้กับผู้ใช้ มาสร้างฟิลด์ตัวนับคงที่ ซึ่งเมื่อสร้างผู้ใช้ใหม่ จะกำหนดหมายเลขลำดับId
ให้กับผู้ใช้
private static int countId = 0;
ดูเหมือนว่าเราจะแยกฟิลด์ออกแล้ว มาเขียน Constructor สำหรับอ็อบเจ็กต์ของเรา และ Getters สำหรับฟิลด์id
, name
, age
, sex
กัน ไม่มีอะไรซับซ้อนกับ heterae มาขอ ความช่วยเหลือจาก IDEAกันดีกว่า เธอจะไม่มีวันปฏิเสธ และเราจะทำให้ Constructor ยุ่งยากเล็กน้อย ผู้ออกแบบจะสามารถ เริ่มต้นฟิลด์ ตรวจสอบว่ามีวัตถุดังกล่าวอยู่ในallUsers
หากไม่มีวัตถุดังกล่าว จากนั้นเพิ่มตัวนับของเราcountId++
และเพิ่มลงในรายการผู้ใช้ทั้งหมด และยังเตรียมใช้งานฟิลด์ได้allUsers
หากยังไม่ได้เตรียมใช้งาน เพื่อความสะดวกในการค้นหาวัตถุที่เหมือนกัน เราจะกำหนดวิธีการใหม่equals()
และhashCode()
เราจะขอความช่วยเหลือจากIDEA ที่เราชื่นชอบอีกครั้ง และเปรียบเทียบตามช่องname
, age
, sex
. นอกจากนี้ เรามาสร้างวิธีการส่วนตัวhasUser()
ที่จะตรวจสอบว่าวัตถุดังกล่าวอยู่ในรายการหรือไม่
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age &&
Objects.equals(name, user.name) &&
sex == user.sex;
}
@Override
public int hashCode() {
return Objects.hash(name, age, sex);
}
ในที่สุดฉันก็ได้คนออกแบบแบบนี้
public User(String name, int age, Sex sex) {
if (allUsers == null){
allUsers = new HashMap<>();
}
this.name = name;
this.age = age;
this.sex = sex;
if (!hasUser()){
countId++;
this.id = countId;
allUsers.put(id, this);
}
}
และวิธีการช่วยเหลือแบบส่วนตัว
private boolean hasUser(){
for (User user : allUsers.values()){
if (user.equals(this) && user.hashCode() == this.hashCode()){
return true;
}
}
return false;
}
และยังกำหนดใหม่อีกด้วยtoString()
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
ตอนนี้ได้เวลาใช้ตรรกะของวิธีการที่จำเป็นแล้ว เนื่องจากตรรกะจะทำงานกับฟิลด์คงที่เป็นหลัก เราจึงทำให้วิธีการเป็นแบบคงที่ด้วย ซึ่งไม่จำเป็นสำหรับอ็อบเจ็กต์
- สร้างรายชื่อผู้ใช้ทั้งหมด
- สร้างรายชื่อผู้ใช้ตามเพศ (ชาย/หญิง)
-
ส่งกลับจำนวนผู้ใช้ในรายการทั่วไป และคำนวณจำนวนตามเพศของผู้ใช้
public static int getHowManyUsers(){ return allUsers.size(); } public static int getHowManyUsers(Sex sex){ return getAllUsers(sex).size(); }
-
คำนวณยอดรวมตามอายุของผู้ใช้และคำนึงถึงเพศด้วย มาสร้างวิธีการสำหรับงานนี้กันดีกว่า
public static int getAllAgeUsers(){ int countAge = 0; for (User user : allUsers.values()){ countAge += user.age; } return countAge; } public static int getAllAgeUsers(Sex sex){ int countAge = 0; for (User user : getAllUsers(sex)){ countAge += user.age; } return countAge; }
-
คำนวณอายุเฉลี่ยทั้งโดยรวมและแยกตามเพศ
public static int getAverageAgeOfAllUsers(){ return getAllAgeUsers() / getHowManyUsers(); } public static int getAverageAgeOfAllUsers(Sex sex){ return getAllAgeUsers(sex) / getHowManyUsers(sex); }
เยี่ยมเลย เราได้อธิบายวัตถุที่ต้องการและพฤติกรรมของมันแล้ว ตอนนี้เราสามารถไปยังJUnit ได้แล้ว แต่ก่อนอื่น ฉันจะแสดงให้คุณเห็นว่าการทดสอบ ง่ายๆจะเป็นอย่างไรหากเราทำในmain
public static void main(String[] args) { new User("Eugene", 35, Sex.MALE); new User("Marina", 34, Sex.FEMALE); new User("Alina", 7, Sex.FEMALE); System.out.println("All users:"); User.getAllUsers().forEach(System.out::println); System.out.println("All users: MALE"); User.getAllUsers(Sex.MALE).forEach(System.out::println); System.out.println("All users: FEMALE"); User.getAllUsers(Sex.FEMALE).forEach(System.out::println); System.out.println("================================================"); System.out.println(" all users: " + User.getHowManyUsers()); System.out.println(" all MALE users: " + User.getHowManyUsers(Sex.MALE)); System.out.println("all FEMALE users: " + User.getHowManyUsers(Sex.FEMALE)); System.out.println("================================================"); System.out.println(" total age of all users: " + User.getAllAgeUsers()); System.out.println(" total age of all MALE users: " + User.getAllAgeUsers(Sex.MALE)); System.out.println("total age of all FEMALE users: " + User.getAllAgeUsers(Sex.FEMALE)); System.out.println("================================================"); System.out.println(" average age of all users: " + User.getAverageAgeOfAllUsers()); System.out.println(" average age of all MALE users: " + User.getAverageAgeOfAllUsers(Sex.MALE)); System.out.println("average age of all FEMALE users: " + User.getAverageAgeOfAllUsers(Sex.FEMALE)); System.out.println("================================================"); }
ผลลัพธ์ที่ส่งไปยังคอนโซลจะมีลักษณะดังนี้ จากนั้นเราจะเปรียบเทียบว่าเราทำงานได้ตามปกติหรือไม่ แน่นอนว่าเราสามารถเจาะลึกลงไปอีก เขียนตรรกะการเปรียบเทียบ และดูว่าการคำนวณของเราบอกอะไร แม้ว่าเราจะไม่แน่ใจว่าเราสามารถจัดเตรียมทุกสิ่งได้ก็ตาม
//output Все пользователи: User{id=1, name='Eugene', age=35, sex=MALE} User{id=2, name='Marina', age=34, sex=FEMALE} User{id=3, name='Alina', age=7, sex=FEMALE} Все пользователи: MALE User{id=1, name='Eugene', age=35, sex=MALE} Все пользователи: FEMALE User{id=2, name='Marina', age=34, sex=FEMALE} User{id=3, name='Alina', age=7, sex=FEMALE} ================================================ всех пользователей: 3 всех пользователей MALE: 1 всех пользователей FEMALE: 2 ================================================ общий возраст всех пользователей: 76 общий возраст всех пользователей MALE: 35 общий возраст всех пользователей FEMALE: 41 ================================================ средний возраст всех пользователей: 25 средний возраст всех пользователей MALE: 35 средний возраст всех пользователей FEMALE: 20 ================================================ Process finished with exit code 0
เราไม่พอใจกับผลลัพธ์นี้ แต่สำหรับ การทดสอบ หลักเราต้องการJUnit
จุดaและbสามารถจัดการได้ดีโดยวิธีการgetAllUsers()
ที่จะส่งคืนรายการทั้งหมดUser
และวิธีการโอเวอร์โหลดgetAllUsers(Sex sex)
ที่จะส่งคืนรายการ ขึ้นอยู่กับพารามิเตอร์ที่ส่งSex
ผ่าน
public static List<User> getAllUsers(){
return new ArrayList<>(allUsers.values());
}
public static List<User> getAllUsers(Sex sex){
List<User> listAllUsers = new ArrayList<>();
for (User user : allUsers.values()){
if (user.sex == sex){
listAllUsers.add(user);
}
}
return listAllUsers;
}
วิธีเชื่อมต่อ JUnit กับโปรเจ็กต์
คำถามเกิดขึ้นว่าจะเชื่อมต่อกับโครงการได้อย่างไร สำหรับผู้ที่รู้ ฉันจะไม่เลือกตัวเลือกนี้กับMavenเนื่องจากนี่เป็นเรื่องราวที่แตกต่างไปจากเดิมอย่างสิ้นเชิง ;) เปิดโครงสร้างโครงการ Ctrl + Alt + Shift + S -> Libraries -> คลิก + (New Project Library) -> เลือกจาก Maven


User
-> กด Alt + Enter -> เลือกสร้างการทดสอบ เราควรเห็นหน้าต่างที่เราต้องเลือกไลบรารี JUnit4 -> เลือกวิธีที่เราจะทดสอบ -> ตกลง 
UserTest
นี่คือคลาสที่เราจะครอบคลุมโค้ดของเราด้วยการทดสอบ มาเริ่มกันเลย:
@Test ครั้งแรกของเรา
มาสร้างเมธอด@TestgetAllUsers()
แรกของเรากันดีกว่า - นี่เป็นเมธอดที่ควรส่งคืนผู้ใช้ทั้งหมด การทดสอบจะมีลักษณะดังนี้:
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
actual.add(user2);
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
ที่นี่เราสร้างผู้ใช้ทดสอบหลายราย -> สร้างรายการexpected
ที่เราจะวางผู้ใช้ที่มีวิธีการกลับมาหาเราgetAllUsers()
-> สร้างรายการactual
ที่เราจะวางผู้ใช้ที่เราถือว่าgetAllUsers()
วิธีการ Assert.assertEquals(actual, คาดหวัง) จะถูกนำไปใช้และเราจะส่งรายการตรวจสอบและเป็นปัจจุบันไปให้ วิธีนี้จะทดสอบออบเจ็กต์ในรายการที่ให้ไว้และส่งกลับผลการทดสอบ วิธีการนี้จะเปรียบเทียบฟิลด์ของอ็อบเจ็กต์ทั้งหมด แม้ว่าจะผ่านฟิลด์ของพาเรนต์หากมีการสืบทอดก็ตาม มาเริ่มการทดสอบแรกกันดีกว่า... 
actual
รายการ
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
//actual.add(user2);
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
เราทำการทดสอบและดูสิ่งต่อไปนี้: 
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(new User("User1", 1, Sex.MALE));
actual.add(new User("User2", 2, Sex.FEMALE));
actual.add(new User("User3", 3, Sex.MALE));
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
นี่คือสิ่งที่จะอยู่ในคอนโซล: 

main
สิ่งนี้เกิดขึ้นได้ไหม? - เลขที่. JUnit : main = 2 : 0 เอาล่ะ เรามาต่อกันดีกว่า เรายังมี method มากมายที่ต้องทำการทดสอบ) แต่เดี๋ยวก่อน มันก็ไม่แย่ที่จะตรวจสอบว่า method นั้นจะgetAllUsers()
กลับมา หาเราหรือเปล่า เพราะนั่นคือ สิ่งที่เราทำกับ null
งานJavaRushที่เครื่องมือตรวจสอบจับได้) เอาล่ะ มันเป็นเรื่องของสาม kopecks...
@Test
public void getAllUsers_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers();
Assert.assertNotNull(expected);
}
ใช่ ใช่ นี่คือวิธีที่ Validator จับโค้ดnull
ของเราโดยประมาณ ;) เรามาทำการทดสอบนี้กันดีกว่า และดูว่ามันแสดงอะไรให้เราเห็นบ้าง แล้วมันจะแสดง error ยังไง???? ข้อผิดพลาดในการทดสอบเกิดขึ้นได้อย่างไร))) 
allUsers
เราได้เตรียมข้อมูลเบื้องต้นให้กับฟิลด์ในตัวสร้าง ซึ่งหมายความว่าเมื่อเรียกใช้เมธอดgetAllUsers()
เราจะอ้างถึงอ็อบเจ็กต์ที่ยังไม่ได้เตรียมใช้งาน มาแก้ไขกัน ลบการกำหนดค่าเริ่มต้นออกจาก Constructor และทำเมื่อประกาศฟิลด์
private static Map<Integer, User> allUsers = new HashMap<>();
public User(String name, int age, Sex sex) {
this.name = name;
this.age = age;
this.sex = sex;
if (!hasUser()) {
countId++;
this.id = countId;
allUsers.put(id, this);
}
}
มาทำการทดสอบกันดีกว่า ตอนนี้ทุกอย่างเรียบร้อยดี 
package user;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class UserTest {
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
actual.add(user2);
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers();
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_MALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
List<User> expected = User.getAllUsers(Sex.MALE);
List<User> actual = new ArrayList<>();
actual.add(user);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_MALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.MALE);
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_FEMALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
List<User> expected = User.getAllUsers(Sex.FEMALE);
List<User> actual = new ArrayList<>();
actual.add(user1);
actual.add(user2);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_FEMALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.FEMALE);
Assert.assertNotNull(expected);
}
@Test
public void getHowManyUsers() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getHowManyUsers();
int actual = 3;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_MALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getHowManyUsers(Sex.MALE);
int actual = 1;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_FEMALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getHowManyUsers(Sex.FEMALE);
int actual = 2;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getAllAgeUsers();
int actual = 35 + 34 + 7;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_MALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getAllAgeUsers(Sex.MALE);
int actual = 35;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_FEMALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getAllAgeUsers(Sex.FEMALE);
int actual = 34 + 7;
Assert.assertEquals(expected, actual);
}
}
ใช่มันกลายเป็นเรื่องไม่เล็ก แต่จะเกิดอะไรขึ้นเมื่อทำงานกับโปรเจ็กต์ขนาดใหญ่ ลดอะไรได้บ้าง หลังจากประเมินทุกอย่างแล้วคุณจะเห็นว่าเราสร้างข้อมูลการทดสอบในการทดสอบแต่ละครั้งและคำอธิบายประกอบก็เข้ามาช่วยเหลือเราที่นี่ ลองทำดู @Before
- คำอธิบายประกอบ@Before
ระบุว่าจะมีการดำเนินการเมธอดก่อนแต่ละวิธีที่@Test
ทดสอบ นี่คือลักษณะของคลาสทดสอบพร้อมคำอธิบายประกอบของเรา@Before
:
package user;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class UserTest {
private User user;
private User user1;
private User user2;
@Before
public void setUp() throws Exception {
user = new User("Eugene", 35, Sex.MALE);
user1 = new User("Marina", 34, Sex.FEMALE);
user2 = new User("Alina", 7, Sex.FEMALE);
}
@Test
public void getAllUsers() {
List<User> expected = User.getAllUsers();
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
actual.add(user2);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_NO_NULL() {
List<User> expected = User.getAllUsers();
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_MALE() {
List<User> expected = User.getAllUsers(Sex.MALE);
List<User> actual = new ArrayList<>();
actual.add(user);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_MALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.MALE);
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_FEMALE() {
List<User> expected = User.getAllUsers(Sex.FEMALE);
List<User> actual = new ArrayList<>();
actual.add(user1);
actual.add(user2);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_FEMALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.FEMALE);
Assert.assertNotNull(expected);
}
@Test
public void getHowManyUsers() {
int expected = User.getHowManyUsers();
int actual = 3;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_MALE() {
int expected = User.getHowManyUsers(Sex.MALE);
int actual = 1;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_FEMALE() {
int expected = User.getHowManyUsers(Sex.FEMALE);
int actual = 2;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers() {
int expected = User.getAllAgeUsers();
int actual = 35 + 34 + 7;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_MALE() {
int expected = User.getAllAgeUsers(Sex.MALE);
int actual = 35;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_FEMALE() {
int expected = User.getAllAgeUsers(Sex.FEMALE);
int actual = 34 + 7;
Assert.assertEquals(expected, actual);
}
}
แล้วคุณล่ะ มันสนุกกว่าและอ่านง่ายกว่าอยู่แล้ว ;) นี่คือรายการคำอธิบายประกอบสำหรับ JUnit ที่จะอยู่กับพวกมันได้ง่ายกว่าแน่นอน
@Test – определяет что метод method() является тестовым.
@Before – указывает на то, что метод будет выполнятся перед каждым тестируемым методом @Test.
@After – указывает на то что метод будет выполнятся после каждого тестируемого метода @Test
@BeforeClass – указывает на то, что метод будет выполнятся в начале всех тестов,
а точней в момент запуска тестов(перед всеми тестами @Test).
@AfterClass – указывает на то, что метод будет выполнятся после всех тестов.
@Ignore – говорит, что метод будет проигнорирован в момент проведения тестирования.
(expected = Exception.class) – указывает на то, что в данном тестовом методе
вы преднамеренно ожидаете Exception.
(timeout = 100) – указывает, что тестируемый метод не должен занимать больше чем 100 миллисекунд.
วิธีการเรียนหลักAssert
สำหรับการตรวจสอบ:
fail(String) – указывает на то что бы тестовый метод завалился при этом выводя текстовое сообщение.
assertTrue([message], boolean condition) – проверяет, что логическое condition истинно.
assertsEquals([String message], expected, actual) – проверяет, что два значения совпадают.
Примечание: для массивов проверяются ссылки, а не содержание массивов.
assertNull([message], object) – проверяет, что an object является пустым null.
assertNotNull([message], object) – проверяет, что an object не является пустым null.
assertSame([String], expected, actual) – проверяет, что обе переменные относятся к одному an objectу.
assertNotSame([String], expected, actual) – проверяет, что обе переменные относятся к разным an objectм.
นี่คือวิธีที่เราสามารถเพิ่มการพึ่งพา JUnit 4.12 ใน Maven
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
มีต่อที่นี่ -> JUnit ตอนที่ II
GO TO FULL VERSION