נבנה על ידיBuilt by Ariel Yohimovich
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.
בדיקות אוטומטיות עם JUnit 5, פרויקט Maven או Gradle, ניהול תלויות, ו-Git ו-GitHub. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Automated tests with JUnit 5, a Maven or Gradle project, dependencies, and Git and GitHub. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- בדיקה היא קוד שבודק קודA test is code that checks code
- אותו דבר ב-JUnit 5The same in JUnit 5
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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).
לפני 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.
ב-JUnit כל בדיקה היא מתודה עם @Test, ו-@BeforeEach בונה מדף חדש לפני כל אחת, כך שהבדיקות לא תלויות זו בזו. assertThrows בודק שנזרקה חריגה. ב-IntelliJ לוחצים על החץ הירוק שליד המחלקה.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 checks an exception was thrown. 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
כתבו שלוש בדיקות למדף בתור מתודות עם שמות של JUnit (addsANewBook, rejectsADuplicate, throwsWhenMissing), כל אחת עם מדף חדש, וסיכום בסגנון Maven: Tests run: 3, Failures: 0. אחר כך העבירו אותן לקובץ JUnit אמיתי כמו בתזכורת (אתגר 1).Write three shelf tests as methods with JUnit names (addsANewBook, rejectsADuplicate, throwsWhenMissing), each with a fresh shelf, and a Maven-style summary: Tests run: 3, Failures: 0. Then move them into a real JUnit file like the refresher (challenge 1).בדיקת יחידה: קוד קטן שבודק יחידה אחת (מתודה, מחלקה) במצב אחד:unit test: Small code checking one unit (a method, a class) in one situation:
המונחים של המפגשThis session's termsJUnit 5: ספריית הבדיקות הנפוצה ב-Java.JUnit 5: Java's common testing library.
המונחים של המפגשThis session's termsassert: assertEquals(expected, actual), assertTrue, assertThrows(Type.class, () -> ...):assertion: assertEquals(expected, actual), assertTrue, assertThrows(Type.class, () -> ...):
המונחים של המפגשThis session's termsMaven: כלי בנייה:Maven: A build tool:
המונחים של המפגשThis session's termsGradle: כלי בנייה נוסף, עם build.gradle במקום pom.xml.Gradle: Another build tool, with build.gradle instead of pom.xml.
המונחים של המפגשThis session's termsGit: מערכת שמירת גרסאות:Git: A version-control system:
המונחים של המפגשThis session's termsלפני JUnit, הרעיון עצמו ב-Java רגילה: כל בדיקה מפעילה את הקוד ומשווה לתוצאה צפויה.Before JUnit, the idea itself in plain Java: each test runs the code and compares with an expected result.
בדיקה היא קוד שבודק קודA test is code that checks codeבדיקה טובה בודקת גם מקרי קצה (שלילי, אפס).A good test checks edge cases too (negative, zero).
בדיקה היא קוד שבודק קודA test is code that checks codeב-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.
אותו דבר ב-JUnit 5The same in JUnit 5assertThrows בודק שנזרקה חריגה.assertThrows checks an exception was thrown.
אותו דבר ב-JUnit 5The same in JUnit 5ב-IntelliJ לוחצים על החץ הירוק שליד המחלקה.In IntelliJ, click the green arrow next to the class.
אותו דבר ב-JUnit 5The same in JUnit 5מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf
הצעד של היום בפרויקט מדףToday's step in the Shelf projectלמה @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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כדי שבדיקה אחת לא תשפיע על אחרתSo one test can't affect another
נכון: כל בדיקה מתחילה ממצב ידוע.Right: each test starts from a known state.איפה שמים בדיקות בפרויקט Maven?Where do tests go in a Maven project?
src/test/javasrc/test/javasrc/main/javasrc/main/javatests/tests/- באותו קובץ כמו הקודIn the same file as the code
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.src/test/javasrc/test/java
נכון. הקוד עצמו ב-src/main/java.Right. The code itself goes in src/main/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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.שומר תמונת מצב של מה שהוספתם עם git addSaves a snapshot of what you added with git add
נכון.Right.בדיקות לחילוקTests for division
- כתבו
assertEquals ו-assertThrows קטנות (כמו ב-JUnit), ובדקו את Calc.divide:Write small assertEquals and assertThrows (like JUnit's), and test Calc.divide:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: הבדיקות מצאו באג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).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.קודם בדיקה, אחר כך קודTest first, code later
- הבדיקות של
Text.slug כבר כתובות ונכשלות.The tests for Text.slug are already written and failing.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מצב נקי לכל בדיקהA clean state per test
newShelf() עושה מה ש-@BeforeEach עושה:newShelf() does what @BeforeEach does:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.Git ראשוןFirst Git
- במחשב שלכם, בתיקיית
java-course:On your computer, in the java-course folder:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 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:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.פרויקט Maven עם JUnitA Maven project with JUnit
- במחשב שלכם:On your computer:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ענף, מיזוג ו-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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.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.
בדיקות לחילוקTests for division(year % 4 == 0 && year % 100 != 0) || year % 400 == 0(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
צייד באגים: הבדיקות מצאו באגBug hunter: the tests found a bugtrim(), toLowerCase(), replaceAll("\\s+", "-")trim(), toLowerCase(), replaceAll("\\s+", "-")
קודם בדיקה, אחר כך קודTest first, code laterאם שתי הבדיקות היו משתמשות באותו מדף, התוצאה הייתה תלויה בסדר.If both tests used the same shelf, the result would depend on the order.
מצב נקי לכל בדיקהA clean state per testמה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.