בדיקות אוטומטיות עם JUnit 5, פרויקט Maven או Gradle, ניהול תלויות, ו-Git ו-GitHub.
Automated tests with JUnit 5, a Maven or Gradle project, dependencies, and Git and GitHub.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
בדיקות לחילוקTests for division
כתבו assertEquals ו-assertThrows קטנות (כמו ב-JUnit), ובדקו את Calc.divide: 8/2 = 4, 7/2 = 3, ו-1/0 זורק ArithmeticException. הפלט: שלוש שורות PASS ... ואז 3/3 passed.
Write small assertEquals and assertThrows (like JUnit's), and test Calc.divide: 8/2 = 4, 7/2 = 3, and 1/0 throws ArithmeticException. The output: three PASS ... lines then 3/3 passed.
איך הפלט אמור להיראות?What should the output look like?
PASS divides evenly
PASS drops the remainder
PASS refuses zero
3/3 passed
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: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.
צייד באגים: הבדיקות מצאו באגBug hunter: the tests found a bug
הבדיקות של isLeap מדפיסות FAIL 1900 is not (divisible by 100). אל תשנו את הבדיקות: תקנו את isLeap (שנה מעוברת: מתחלקת ב-4 ולא ב-100, או מתחלקת ב-400) עד 4/4 passed.
The isLeap tests print FAIL 1900 is not (divisible by 100). Don't change the tests: fix isLeap (a leap year divides by 4 and not by 100, or divides by 400) until 4/4 passed.
איך הפלט אמור להיראות?What should the output look like?
PASS 2024 is leap
PASS 2023 is not
PASS 1900 is not (divisible by 100)
PASS 2000 is leap (divisible by 400)
4/4 passed
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:(year % 4 == 0 && year % 100 != 0) || year % 400 == 0(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
הבדיקות של Text.slug כבר כתובות ונכשלות. כתבו את slug כך ששלושתן יעברו: אותיות קטנות, בלי רווחים בקצוות, וכל רצף רווחים הופך למקף אחד.
The tests for Text.slug are already written and failing. Write slug so all three pass: lower case, no spaces at the ends, and every run of spaces becomes one dash.
איך הפלט אמור להיראות?What should the output look like?
PASS lower case and dash
PASS trims
PASS many spaces become one dash
3/3 passed
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
newShelf() עושה מה ש-@BeforeEach עושה: מדף חדש עם Dune. כתבו ארבע בדיקות (הוספה, גודל, דחיית כפול, גודל שנשאר) כך שכל זוג מקבל מדף משלו. הפלט: ארבע PASS ו-4/4 passed.
newShelf() does what @BeforeEach does: a new shelf with Dune. Write four tests (adding, size, rejecting a duplicate, size unchanged) so each pair gets its own shelf. The output: four PASS and 4/4 passed.
איך הפלט אמור להיראות?What should the output look like?
PASS adds a new title
PASS size grows
PASS rejects a duplicate
PASS size stays
4/4 passed
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:אם שתי הבדיקות היו משתמשות באותו מדף, התוצאה הייתה תלויה בסדר.If both tests used the same shelf, the result would depend on the order.
במחשב שלכם, בתיקיית java-course: אתחלו מאגר Git, הוסיפו את כל הקבצים, צרו commit ראשון, והסתכלו בהיסטוריה ובמצב.
On your computer, in the java-course folder: initialise a Git repository, add all the files, make a first commit, and look at the history and the status.
רמזHint 1:בפעם הראשונה Git יבקש שם ומייל: git config --global user.name "..." ו-user.email.The first time Git asks for a name and email: git config --global user.name "..." and user.email.
מדף, צעד 30: בדיקות למדףShelf, step 30: tests for the shelf
פרויקט מלווהCompanion project
כתבו שלוש בדיקות למדף בתור מתודות עם שמות של 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).
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:throwsWhenMissing תופסת את BookNotFoundException ובודקת שהיא נזרקה.throwsWhenMissing catches BookNotFoundException and checks that it was thrown.
במחשב שלכם: צרו פרויקט Maven, הוסיפו ל-pom.xml את התלות org.junit.jupiter:junit-jupiter (בגרסה עדכנית, scope test), העתיקו את Shelf ואת ShelfTest מהתזכורת, הריצו mvn test, ואחר כך mvn package.
On your computer: create a Maven project, add the org.junit.jupiter:junit-jupiter dependency to pom.xml (a current version, scope test), copy Shelf and ShelfTest from the refresher, run mvn test, then mvn package.
רמזHint 1:ב-Gradle: gradle init, ואז testImplementation ב-build.gradle ו-gradle test.With Gradle: gradle init, then testImplementation in build.gradle and gradle test.
רמזHint 2:ב-IntelliJ: New Project ← Build system: Maven עושה את רוב זה לבד.In IntelliJ: New Project → Build system: Maven does most of this for you.