נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
30
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובות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 @BeforeEach builds a fresh shelf before each one, so the tests don't depend on each other.
  • assertThrows בודק שנזרקה חריגה.assertThrows checks 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 @Test method is a test, @BeforeEach runs before each one, and IntelliJ or mvn test runs 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.xml defines the project and its dependencies (e.g. JUnit), which it downloads itself. Code in src/main/java, tests in src/test/java. mvn test, mvn package.
  • GradleGradle: כלי בנייה נוסף, עם build.gradle במקום pom.xml. אותו מבנה תיקיות. gradle test, gradle build.Another build tool, with build.gradle instead of pom.xml. The same folder layout. gradle test, gradle build.
  • GitGit: מערכת שמירת גרסאות: כל commit הוא תמונת מצב של הפרויקט עם הודעה. אפשר לחזור אחורה, לפתוח ענפים (branches) ולמזג. GitHub שומר עותק ברשת.A version-control system: each commit is 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 add picks what goes in, and git 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 @Test method is a test, @BeforeEach runs before each one, and IntelliJ or mvn test runs 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.xml defines the project and its dependencies (e.g. JUnit), which it downloads itself. Code in src/main/java, tests in src/test/java. mvn test, mvn package.
  • GradleGradle: כלי בנייה נוסף, עם build.gradle במקום pom.xml. אותו מבנה תיקיות. gradle test, gradle build.Another build tool, with build.gradle instead of pom.xml. The same folder layout. gradle test, gradle build.
  • GitGit: מערכת שמירת גרסאות: כל commit הוא תמונת מצב של הפרויקט עם הודעה. אפשר לחזור אחורה, לפתוח ענפים (branches) ולמזג. GitHub שומר עותק ברשת.A version-control system: each commit is 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 add picks what goes in, and git 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

נבנה על ידיBuilt by Ariel Yohimovich