BAWABAA
No Result
View All Result
  • Home
  • Products
  • Services
    • Courses
    • Pathways
  • Solutions
  • Plans
  • Resources
    • List of Curated Edtech Websites
    • Pathways
    • Blog
      • Programming Languages
      • Tech
      • Graphic Design
      • Inspiration
      • Software Testing
      • Operating Systems
      • Frameworks
BAWABAA
  • Home
  • Products
  • Services
    • Courses
    • Pathways
  • Solutions
  • Plans
  • Resources
    • List of Curated Edtech Websites
    • Pathways
    • Blog
      • Programming Languages
      • Tech
      • Graphic Design
      • Inspiration
      • Software Testing
      • Operating Systems
      • Frameworks
No Result
View All Result
BAWABAA
No Result
View All Result
Home Frameworks

Unit testing – Stub Object in Java

January 23, 2025
Reading Time: 4 mins read

Here’s another example of using a stub object in Java, but this time for testing a WeatherService that fetches weather data from an external WeatherAPI. We’ll use a stub to simulate the behavior of the WeatherAPI to focus on testing the WeatherService.

Scenario: Testing a Weather Service

The WeatherService class depends on the WeatherAPI to fetch weather information. We want to test the WeatherService without calling the actual external API.


Implementation

Classes:

// Weather API Interface (Dependency)
public interface WeatherAPI {
    String getWeather(String city);
}

// Weather Service (Under Test)
public class WeatherService {
    private final WeatherAPI weatherAPI;

    public WeatherService(WeatherAPI weatherAPI) {
        this.weatherAPI = weatherAPI;
    }

    public String getWeatherInfo(String city) {
        String weather = weatherAPI.getWeather(city);
        if ("Sunny".equals(weather)) {
            return "It's a sunny day!";
        } else if ("Rainy".equals(weather)) {
            return "It's a rainy day!";
        } else {
            return "Weather information unavailable";
        }
    }
}

Stub for WeatherAPI:

We create a simple stub for WeatherAPI that returns hardcoded weather data.

// Stub Implementation for WeatherAPI
public class WeatherAPIStub implements WeatherAPI {
    @Override
    public String getWeather(String city) {
        if ("New York".equals(city)) {
            return "Sunny";
        } else if ("Seattle".equals(city)) {
            return "Rainy";
        }
        return "Unknown";  // Default case for other cities
    }
}

Test Case Using Stub:

Now, let’s write unit tests for WeatherService using the WeatherAPIStub to simulate the weather API’s behavior.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class WeatherServiceTest {

    @Test
    public void testGetWeatherInfoSunny() {
        // Arrange: Use the stub
        WeatherAPI weatherAPIStub = new WeatherAPIStub();
        WeatherService weatherService = new WeatherService(weatherAPIStub);

        // Act: Call the method with "New York" which returns "Sunny" from the stub
        String weatherInfo = weatherService.getWeatherInfo("New York");

        // Assert: Verify the result
        assertEquals("It's a sunny day!", weatherInfo);
    }

    @Test
    public void testGetWeatherInfoRainy() {
        // Arrange: Use the stub
        WeatherAPI weatherAPIStub = new WeatherAPIStub();
        WeatherService weatherService = new WeatherService(weatherAPIStub);

        // Act: Call the method with "Seattle" which returns "Rainy" from the stub
        String weatherInfo = weatherService.getWeatherInfo("Seattle");

        // Assert: Verify the result
        assertEquals("It's a rainy day!", weatherInfo);
    }

    @Test
    public void testGetWeatherInfoUnknownCity() {
        // Arrange: Use the stub
        WeatherAPI weatherAPIStub = new WeatherAPIStub();
        WeatherService weatherService = new WeatherService(weatherAPIStub);

        // Act: Call the method with a city that isn't "New York" or "Seattle"
        String weatherInfo = weatherService.getWeatherInfo("Los Angeles");

        // Assert: Verify the result for the unknown city
        assertEquals("Weather information unavailable", weatherInfo);
    }
}

Explanation:

  1. Stub Implementation (WeatherAPIStub):
    • The stub returns hardcoded responses for specific cities ("Sunny" for “New York” and "Rainy" for “Seattle”).
    • For any other city, it returns "Unknown".
  2. Tests:
    • We write three tests, one for each possible outcome: sunny, rainy, and an unknown city.
    • Each test verifies that the WeatherService correctly interprets the result from the WeatherAPIStub.

Benefits of Using Stubs Here:

  1. Isolation: The test focuses on the WeatherService without being dependent on the actual WeatherAPI.
  2. Controlled Environment: The stub gives predictable outputs, ensuring the unit test is stable and repeatable.
  3. Testing Edge Cases: You can easily simulate different conditions (e.g., cities with specific weather, unknown cities) without calling a real external service.

This is another classic example of stubbing external dependencies to isolate the unit under test. Would you like to see another example in a different context?

Share6Tweet4Share1
Previous Post

Stub Object in Unit testing with Java

Next Post

Unit Testing – Test Doubles: Dummy, Stub and Mock Examples

Next Post

Unit Testing - Test Doubles: Dummy, Stub and Mock Examples

Soft skills Outlines

List of Curated Edtech Websites

Understand the differences between entity, class, and object in the context of programming and real-world modeling.

Association, Aggregation, Composition and Dependency relationship

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Frameworks (22)
  • Programming Languages (4)
  • Tech (1)

© 2025 Bawabaa.com

No Result
View All Result
  • Home
  • Products
  • Services
    • Courses
    • Pathways
  • Solutions
  • Plans
  • Resources
    • List of Curated Edtech Websites
    • Pathways
    • Blog
      • Programming Languages
      • Tech
      • Graphic Design
      • Inspiration
      • Software Testing
      • Operating Systems
      • Frameworks

© 2025 Bawabaa.com