Use dummy objects to satisfy method parameters in unit test
Preconditions (2)
✅ Expected Result: The test passes successfully, confirming the add method works and the dummy object satisfies the parameter requirement without affecting the test
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class Calculator { // Method that requires a dummy object parameter but does not use it public int add(int a, int b, Object dummy) { return a + b; } } public class CalculatorTest { // Dummy object class static class DummyObject {} @Test void testAddWithDummyObject() { Calculator calculator = new Calculator(); DummyObject dummy = new DummyObject(); // dummy object to satisfy parameter int result = calculator.add(5, 7, dummy); assertEquals(12, result, "The add method should return the sum of two numbers"); } }
This test shows how to use a dummy object to satisfy a method parameter that is not used in the logic. The Calculator.add method requires three parameters, but the third parameter is not used. We create a simple DummyObject class and pass an instance to the method. The test asserts that the sum is correct, ignoring the dummy object. This keeps the test focused and simple.
Now add data-driven testing with 3 different pairs of integers to test the add method with dummy objects
dummy object in JUnit testing?List<String> in JUnit?public void testProcess() {
Processor processor = new Processor();
Data dummyData = new Data(); // dummy object
boolean result = processor.process(dummyData);
assertTrue(result);
}dummyData here?@Test
public void testCalculate() {
Calculator calc = new Calculator();
int result = calc.calculate(null); // passing null as dummy
assertEquals(0, result);
}sendEmail(User user, EmailContent content) where content is not used in your test. How should you use a dummy object for content in your JUnit test to keep it clean and safe?content is unused, a simple empty instance suffices as dummy.Pass the test and this pattern moves unknown