חלקPart 5: Java מודרנית וכליםModern Java & tools
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
JUnit 5, Maven, Gradle ו-GitJUnit 5, Maven, Gradle & Git
בדיקות אוטומטיות עם JUnit 5, פרויקט Maven או Gradle, ניהול תלויות, ו-Git ו-GitHub.
Automated tests with JUnit 5, a Maven or Gradle project, dependencies, and Git and GitHub.
הנקודות החשובותKey points
- בדיקת יחידה: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד:unit test: Small code checking one unit (a method, a class) in one situation:
- JUnit 5: ספריית הבדיקות הנפוצה ב-Java.JUnit 5: Java's common testing library.
- לפני JUnit, הרעיון עצמו ב-Java רגילה: כל בדיקה מפעילה את הקוד ומשווה לתוצאה צפויה.Before JUnit, the idea itself in plain Java: each test runs the code and compares with an expected result.
- בדיקה טובה בודקת גם מקרי קצה (שלילי, אפס).A good test checks edge cases too (negative, zero).
- ב-JUnit כל בדיקה היא מתודה עם
@Test, ו-@BeforeEachבונה מדף חדש לפני כל אחת, כך שהבדיקות לא תלויות זו בזו.In JUnit each test is a method with@Test, and@BeforeEachbuilds a fresh shelf before each one, so the tests don't depend on each other. assertThrowsבודק שנזרקה חריגה.assertThrowschecks an exception was thrown.- מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf
מונחיםTerms
- בדיקת יחידהunit test: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד: מכינים (Arrange), מפעילים (Act), ובודקים את התוצאה (Assert). רץ בשנייה, ואפשר להריץ אחרי כל שינוי.Small code checking one unit (a method, a class) in one situation: set up (Arrange), run (Act), check the result (Assert). It runs in a second, after every change.
- JUnit 5JUnit 5: ספריית הבדיקות הנפוצה ב-Java. כל מתודה עם
@Testהיא בדיקה,@BeforeEachרץ לפני כל אחת, ו-IntelliJ אוmvn testמריצים הכול ומדווחים.Java's common testing library. Each@Testmethod is a test,@BeforeEachruns before each one, and IntelliJ ormvn testruns them all and reports. - assertassertion:
assertEquals(expected, actual),assertTrue,assertThrows(Type.class, () -> ...): אם הבדיקה נכשלת, JUnit מסמן FAIL ומראה מה היה צפוי ומה התקבל.assertEquals(expected, actual),assertTrue,assertThrows(Type.class, () -> ...): when one fails, JUnit marks FAIL and shows what was expected and what came. - MavenMaven: כלי בנייה: קובץ
pom.xmlמגדיר את הפרויקט ואת התלויות (למשל JUnit), והוא מוריד אותן לבד. קוד ב-src/main/java, בדיקות ב-src/test/java.mvn test,mvn package.A build tool:pom.xmldefines the project and its dependencies (e.g. JUnit), which it downloads itself. Code insrc/main/java, tests insrc/test/java.mvn test,mvn package. - GradleGradle: כלי בנייה נוסף, עם
build.gradleבמקוםpom.xml. אותו מבנה תיקיות.gradle test,gradle build.Another build tool, withbuild.gradleinstead ofpom.xml. The same folder layout.gradle test,gradle build. - GitGit: מערכת שמירת גרסאות: כל
commitהוא תמונת מצב של הפרויקט עם הודעה. אפשר לחזור אחורה, לפתוח ענפים (branches) ולמזג. GitHub שומר עותק ברשת.A version-control system: eachcommitis a snapshot of the project with a message. You can go back, open branches and merge. GitHub keeps a copy online. - commitcommit:
git addבוחר מה ייכנס, ו-git commit -m "..."שומר תמונת מצב. הודעה טובה אומרת מה השתנה ולמה.git addpicks what goes in, andgit commit -m "..."saves a snapshot. A good message says what changed and why. - בדיקת יחידהunit test: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד: מכינים (Arrange), מפעילים (Act), ובודקים את התוצאה (Assert). רץ בשנייה, ואפשר להריץ אחרי כל שינוי.Small code checking one unit (a method, a class) in one situation: set up (Arrange), run (Act), check the result (Assert). It runs in a second, after every change.
- JUnit 5JUnit 5: ספריית הבדיקות הנפוצה ב-Java. כל מתודה עם
@Testהיא בדיקה,@BeforeEachרץ לפני כל אחת, ו-IntelliJ אוmvn testמריצים הכול ומדווחים.Java's common testing library. Each@Testmethod is a test,@BeforeEachruns before each one, and IntelliJ ormvn testruns them all and reports. - assertassertion:
assertEquals(expected, actual),assertTrue,assertThrows(Type.class, () -> ...): אם הבדיקה נכשלת, JUnit מסמן FAIL ומראה מה היה צפוי ומה התקבל.assertEquals(expected, actual),assertTrue,assertThrows(Type.class, () -> ...): when one fails, JUnit marks FAIL and shows what was expected and what came. - MavenMaven: כלי בנייה: קובץ
pom.xmlמגדיר את הפרויקט ואת התלויות (למשל JUnit), והוא מוריד אותן לבד. קוד ב-src/main/java, בדיקות ב-src/test/java.mvn test,mvn package.A build tool:pom.xmldefines the project and its dependencies (e.g. JUnit), which it downloads itself. Code insrc/main/java, tests insrc/test/java.mvn test,mvn package. - GradleGradle: כלי בנייה נוסף, עם
build.gradleבמקוםpom.xml. אותו מבנה תיקיות.gradle test,gradle build.Another build tool, withbuild.gradleinstead ofpom.xml. The same folder layout.gradle test,gradle build. - GitGit: מערכת שמירת גרסאות: כל
commitהוא תמונת מצב של הפרויקט עם הודעה. אפשר לחזור אחורה, לפתוח ענפים (branches) ולמזג. GitHub שומר עותק ברשת.A version-control system: eachcommitis a snapshot of the project with a message. You can go back, open branches and merge. GitHub keeps a copy online. - commitcommit:
git addבוחר מה ייכנס, ו-git commit -m "..."שומר תמונת מצב. הודעה טובה אומרת מה השתנה ולמה.git addpicks what goes in, andgit commit -m "..."saves a snapshot. A good message says what changed and why.
Cheatsheet
בדיקה היא קוד שבודק קודA test is code that checks code
Calculator c = new Calculator();
assertEquals(5, c.add(2, 3), "adds two numbers");
assertEquals(-1, c.add(2, -3), "adds a negative");
assertEquals(0, c.add(0, 0), "zero plus zero");
System.out.println(passed + "/" + total + " passed");אותו דבר ב-JUnit 5The same in JUnit 5
class ShelfTest {
private Shelf shelf;
@BeforeEach
void setUp() {
shelf = new Shelf();
shelf.add(new Book("Dune", "Herbert"));
}
@Testשגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|