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

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