From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Solution: Write a JUnit 5 Test for a Temperature Converter
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Solution: Write a JUnit 5 Test for a Temperature Converter
(bright electronic music) - [Instructor] Let's write some automated tests for our TemperatureConverter. First, we'll create a test class to hold our tests. We'll create a new file and call it TemperatureConverterTest, and we'll put it inside of our app folder. Our first test will be for a normal conversion. We'll use the test annotation to create this new test, and we'll name it the method we're testing, so that's celsiusToFahrenheit. Since this method is a static method and it doesn't require any objects, we don't need to do much setup. We'll just call the function with valid inputs and then store the result. We'll call it with zero. This will return a double, that'll be our result. In order to verify this function is working as expected, we'll use an assertion method. Zero Celsius is 32 Fahrenheit, so we'll expect this function to return 32. Let's use assertEquals. We'll assert the result is 32, and we'll add that import. Now, we can simplify this even more by calling the function…
Contents
-
-
-
-
Set up JUnit 5 in your Java application2m 24s
-
(Locked)
Write and run your first JUnit 5 test4m 11s
-
(Locked)
Assertions in JUnit 57m 56s
-
(Locked)
JUnit 5 test lifecycle1m 51s
-
(Locked)
JUnit 5 lifecycle hooks2m 49s
-
(Locked)
Testing exceptions in JUnit 52m 54s
-
(Locked)
Debug tests in JUnit 54m 30s
-
(Locked)
Best practices for writing tests in JUnit 52m 17s
-
(Locked)
Challenge: Write a JUnit 5 test for a temperature converter47s
-
(Locked)
Solution: Write a JUnit 5 Test for a Temperature Converter6m 33s
-
-
-
-
-
-
-
-