Unit Testing – Basics

1. Test Runner

  • A Test Runner is responsible for executing test cases.
  • It scans the code for test annotations (like @Test in JUnit), runs them, and reports results (pass/fail, exceptions, etc.).

Example:

@RunWith(JUnit4.class)
public class MyTestClass {
    @Test
    public void sampleTest() {
        assertTrue(true);
    }
}

Here, JUnit4 is the test runner that executes sampleTest().


2. Test Case

  • A Test Case is an individual test method that verifies a specific unit of logic or functionality.
  • Each test should be independent, repeatable, and small in scope.

Example:

@Test
public void shouldReturnCorrectSpeed() {
    Car car = new Car();
    car.accelerate(10);
    assertEquals(10, car.getSpeed());
}

Here, shouldReturnCorrectSpeed() is a test case.


3. Test Suite

  • A Test Suite groups multiple test classes or cases together for batch execution.
  • Useful for organizing tests by module, feature, or functional area.

Example:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    EngineTest.class,
    WheelTest.class,
    CarTest.class
})
public class VehicleTestSuite {}

This creates a suite that runs all listed test classes.


Popular Mocking Frameworks

1. Mockito

  • Used to create mock or dummy objects to simulate dependencies.
  • Helps test the target class in isolation without invoking real dependencies.

Example Scenario:

class Car {
    private final List<Wheel> wheels;
    Car(List<Wheel> wheels) { this.wheels = wheels; }

    boolean allWheelsReady() {
        return wheels.stream().allMatch(Wheel::isReady);
    }
}

Unit Test using Mockito:

@Mock
List<Wheel> wheels;

@InjectMocks
Car car;

@Before
public void setup() {
    MockitoAnnotations.openMocks(this);
}

@Test
public void testAllWheelsReady() {
    Wheel wheel = mock(Wheel.class);
    when(wheel.isReady()).thenReturn(true);
    when(wheels.stream()).thenReturn(Stream.of(wheel, wheel, wheel, wheel));

    assertTrue(car.allWheelsReady());
}

Key Point: Mockito can mock interfaces, normal classes, and methods — but not static methods or constructors.


2. PowerMockito

  • Extension of Mockito that can mock static, final, or private methods.
  • Useful when dealing with legacy code or third-party libraries where dependencies can’t be injected.

Example:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Math.class })
public class PowerMockExampleTest {

    @Test
    public void testStaticMethod() {
        PowerMockito.mockStatic(Math.class);
        when(Math.random()).thenReturn(0.5);
        assertEquals(0.5, Math.random(), 0);
    }
}

Key Point:
Use PowerMockito cautiously — it’s powerful but can make tests brittle or tightly coupled to implementation details.


Summary Table

ConceptDescriptionExample/Tool
Test RunnerExecutes all testsJUnit4, AndroidJUnitRunner
Test CaseA single test method@Test public void testXYZ()
Test SuiteGroup of test cases@SuiteClasses({A.class, B.class})
MockitoMocks normal dependenciesmock(Wheel.class)
PowerMockitoMocks static/final/private methodsPowerMockito.mockStatic(Math.class)

Leave a Comment

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

Scroll to Top