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

חזרה למפגשBack
JavaJava

JUnit 5, Maven, Gradle ו-GitJUnit 5, Maven, Gradle & Git

מפגש 30: בדיקות אוטומטיות עם JUnit 5, פרויקט Maven או Gradle, ניהול תלויות, ו-Git ו-GitHub.Session 30: Automated tests with JUnit 5, a Maven or Gradle project, dependencies, and Git and GitHub.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • בדיקה היא קוד שבודק קודA test is code that checks code
  • אותו דבר ב-JUnit 5The same in JUnit 5
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • בדיקת יחידה: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד:unit test: Small code checking one unit (a method, a class) in one situation:
  • JUnit 5: ספריית הבדיקות הנפוצה ב-Java.JUnit 5: Java's common testing library.
  • assert: assertEquals(expected, actual), assertTrue, assertThrows(Type.class, () -> ...):assertion: assertEquals(expected, actual), assertTrue, assertThrows(Type.class, () -> ...):
  • Maven: כלי בנייה:Maven: A build tool:
  • Gradle: כלי בנייה נוסף, עם build.gradle במקום pom.xml.Gradle: Another build tool, with build.gradle instead of pom.xml.
  • Git: מערכת שמירת גרסאות:Git: A version-control system:
נושאTopic 2

בדיקה היא קוד שבודק קודA test is code that checks code

בדיקה היא קוד שבודק קודA test is code that checks code

  • לפני 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).
נושאTopic 3

אותו דבר ב-JUnit 5The same in JUnit 5

אותו דבר ב-JUnit 5The same in JUnit 5

  • ב-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.
  • ב-IntelliJ לוחצים על החץ הירוק שליד המחלקה.In IntelliJ, click the green arrow next to the class.
נושאTopic 4

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf

בדיקת יחידה: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד:unit test: Small code checking one unit (a method, a class) in one situation:

JUnit 5: ספריית הבדיקות הנפוצה ב-Java.JUnit 5: Java's common testing library.

assert: assertEquals(expected, actual), assertTrue, assertThrows(Type.class, () -> ...):assertion: assertEquals(expected, actual), assertTrue, assertThrows(Type.class, () -> ...):

Maven: כלי בנייה:Maven: A build tool:

Gradle: כלי בנייה נוסף, עם build.gradle במקום pom.xml.Gradle: Another build tool, with build.gradle instead of pom.xml.

Git: מערכת שמירת גרסאות:Git: A version-control system:

לפני 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.

ב-IntelliJ לוחצים על החץ הירוק שליד המחלקה.In IntelliJ, click the green arrow next to the class.

מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf

למה @BeforeEach בונה מדף חדש לפני כל בדיקה?Why does @BeforeEach build a new shelf before every test?

  • כדי שבדיקה אחת לא תשפיע על אחרתSo one test can't affect another
  • כי זה מהיר יותרBecause it's faster
  • כי JUnit מחייבBecause JUnit requires it
  • אין סיבהNo reason

כדי שבדיקה אחת לא תשפיע על אחרתSo one test can't affect another

איפה שמים בדיקות בפרויקט Maven?Where do tests go in a Maven project?

  • src/test/javasrc/test/java
  • src/main/javasrc/main/java
  • tests/tests/
  • באותו קובץ כמו הקודIn the same file as the code

src/test/javasrc/test/java

מה עושה git commit -m "Add ratings"?What does git commit -m "Add ratings" do?

  • שומר תמונת מצב של מה שהוספתם עם git addSaves a snapshot of what you added with git add
  • מעלה ל-GitHubUploads to GitHub
  • מוחק שינוייםDeletes changes
  • יוצר ענףCreates a branch

שומר תמונת מצב של מה שהוספתם עם git addSaves a snapshot of what you added with git add

בדיקות לחילוקTests for division

  • כתבו assertEquals ו-assertThrows קטנות (כמו ב-JUnit), ובדקו את Calc.divide:Write small assertEquals and assertThrows (like JUnit's), and test Calc.divide:

צייד באגים: הבדיקות מצאו באגBug hunter: the tests found a bug

  • הבדיקות של isLeap מדפיסות FAIL 1900 is not (divisible by 100).The isLeap tests print FAIL 1900 is not (divisible by 100).

קודם בדיקה, אחר כך קודTest first, code later

  • הבדיקות של Text.slug כבר כתובות ונכשלות.The tests for Text.slug are already written and failing.

מצב נקי לכל בדיקהA clean state per test

  • newShelf() עושה מה ש-@BeforeEach עושה:newShelf() does what @BeforeEach does:

Git ראשוןFirst Git

  • במחשב שלכם, בתיקיית java-course:On your computer, in the java-course folder:

מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf

  • כתבו שלוש בדיקות למדף בתור מתודות עם שמות של JUnit (addsANewBook, rejectsADuplicate, throwsWhenMissing), כל אחת עם מדף חדש, וסיכום בסגנון Maven:Write three shelf tests as methods with JUnit names (addsANewBook, rejectsADuplicate, throwsWhenMissing), each with a fresh shelf, and a Maven-style summary:

פרויקט Maven עם JUnitA Maven project with JUnit

  • במחשב שלכם:On your computer:

ענף, מיזוג ו-GitHubA branch, a merge and GitHub

  • פתחו ענף feature/ratings, שנו משהו ושמרו commit, חזרו ל-main ומזגו.Open a feature/ratings branch, change something and commit, go back to main and merge.

assertThrows מקבל Action (interface עם void run(), כמו Executable של JUnit) ומריץ אותו בתוך try.assertThrows takes an Action (an interface with void run(), like JUnit's Executable) and runs it inside a try.

(year % 4 == 0 && year % 100 != 0) || year % 400 == 0(year % 4 == 0 && year % 100 != 0) || year % 400 == 0

trim(), toLowerCase(), replaceAll("\\s+", "-")trim(), toLowerCase(), replaceAll("\\s+", "-")

אם שתי הבדיקות היו משתמשות באותו מדף, התוצאה הייתה תלויה בסדר.If both tests used the same shelf, the result would depend on the order.

מה למדנוWhat we learned

  • בדיקת יחידה: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד:unit test: Small code checking one unit (a method, a class) in one situation:
  • לפני JUnit, הרעיון עצמו ב-Java רגילה: כל בדיקה מפעילה את הקוד ומשווה לתוצאה צפויה.Before JUnit, the idea itself in plain Java: each test runs the code and compares with an expected result.
  • ב-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.
  • מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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