Introduction to JUnit
1. Introduction to JUnit
Let's now learn about JUnit!2. Quick facts
JUnit is the most popular Java testing library and the most popular Java library in general. It is well-maintained and constantly updated.3. Writing a JUnit test
Let's write a test! We'll test the `addTwoNumbers()` method, which takes two integers and returns their sum.4. JUnit @Test annotation
We install JUnit using our preferred IDE or build tool. See the example in the citation. First, we need to learn about the @Test annotation. We import it from the `org.junit.jupiter.api` package. To mark a method as a test, we write this annotation above the method. But what is an annotation? An annotation in Java is a special metadata mark that tells Java or various libraries and frameworks how to handle the method. All annotations begin with the @ symbol. The @Test annotation tells JUnit that the method is a test. Without it, JUnit won’t detect or run the method.5. JUnit test structure
We define a test as a `void` method. Tests have a common structure by convention. They have three parts: setup, execution, and verification, also known as given-when-then, or arrange-act-assert. The arrange step sets up variables. The act step calls the method under test. In the assert step, we use the assert statements provided by JUnit to check if actual results match expectations. Here, we use `assertEquals`.6. Assert statements
Assert statements are used to check that our code behaves as expected. They compare actual outcomes to expected ones, and if they don't match, the test fails, helping us quickly catch problems and incorrect logic.7. Test outcomes
If the values in `assertEquals()` match, the test passes. If the values are not equal (e.g. if `actual` is 5), the test will fail with an `AssertionFailedError`. This is handled internally by JUnit. JUnit will further communicate why the assertion failed. Here, we see the expected value is 4, while the value we passed is 5. Note the argument order - in JUnit, the first argument is always the expected one.8. Additional test for overflow
We can now write a test to confirm that overflow occurs when adding large integers. Building on the previous lesson, we pick the boundary case of adding maximum integer and 1. We verify that the result is a negative value.9. Notes about JUnit and testing
There are a few things to note: There is no limit on how many assertions we can use per test. For the test to pass, all assertions need to pass. We can import all assertions with the wildcard, `*`, import from junit assertions.10. More notes
Finally, you will encounter the `datacamp-util` import in the exercises. It is a custom course tool for pretty-printing test results, something typically handled by your IDE or build tool. `import static package.Class.method` allows us to call just `method`, without specifying the class name.11. Let's practice!
Let's write some tests!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.