I'm playing around with JMock and trying to understand whether I understand the idea correctly.
There's an interface Printer aimed to print integers somewhere. There's an interface Calculator aimed to perform math. There's a class CalculatingMachine aimed to connect Calculator and Printer: first, it calculates the result with Calculator and then this result is passed to Printer. Here's the code
public class CalculatingMachine {
private final Printer printer;
private final Calculator calculator;
public CalculatingMachine(Printer printer, Calculator calculator) {
this.printer = printer;
this.calculator = calculator;
}
public void processAdd(int x, int y) {
int result = calculator.add(x, y);
printer.print(result);
}
public static interface Printer {
void print(int x);
}
public static interface Calculator {
int add(int x, int y);
}
}
As far as I understand CalculatingMachine only requires one test: we need to make sure IT USES calculator to do the math and then we need to make sure IT USES printer to print the result. So, here's my test:
public class CalculatingMachineTest {
@Test
public void testCalculatingMachine() {
Mockery context = new JUnit4Mockery();
final Printer printer = context.mock(Printer.class);
final Calculator calculator = context.mock(Calculator.class);
context.checking(new Expectations() {{
oneOf(calculator).add(1, 2);
will(returnValue(3));
oneOf(printer).print(3);
}});
CalculatingMachine machine = new CalculatingMachine(printer, calculator);
machine.processAdd(1, 2);
context.assertIsSatisfied();
}
}
Pretty straightforward and does what it supposed to do. There are 2 points I'm curious about:
- Is it the right approach even though CodePro says that
CalculatingMachineitself has 19 lines of code, though test for it is 23 lines of code? I'm just not sure: is it normal to write more tests then code that does something? - Is there any other way to test all the same but with less code?