Sobes.tech
Junior — Middle

Can you explain what a Stub is in the context of software testing?

sobes.tech AI

Answer from AI

Stub is a simple placeholder object used in testing to replace a real component that is currently unavailable or complex to use.

A stub returns predefined data in response to calls, allowing the tested code to be isolated from external dependencies. Unlike mocks, which can verify calls, stubs typically just provide fixed responses.

Example in C#:

public interface IDataService {
    int GetData();
}

public class DataServiceStub : IDataService {
    public int GetData() {
        return 42; // fixed value for testing
    }
}

// In a test, you can use DataServiceStub instead of the real service
Can you explain what a Stub is in the context of… - sobes.tech