【发布时间】:2015-08-04 10:50:28
【问题描述】:
我想知道这段代码是什么意思:
mathApplication.setCalculatorService(calcService);
为什么要使用接口并从中创建对象?而这个注入是什么意思?
这是我的测试代码:
import org.easymock.EasyMock;
import org.easymock.EasyMockRunner;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(EasyMockRunner.class)
public class MathApplicationTester {
private MathApplication mathApplication;
private CalculatorService calcService;
@Before
public void setUp() {
mathApplication = new MathApplication();
calcService = EasyMock.createMock(CalculatorService.class);
mathApplication.setCalculatorService(calcService);
}
@Test
public void testAddAndSubtract() {
//add the behavior to add numbers
EasyMock.expect(calcService.add(20.0, 10.0)).andReturn(30.0);
//subtract the behavior to subtract numbers
EasyMock.expect(calcService.subtract(20.0, 10.0)).andReturn(10.0);
//activate the mock
EasyMock.replay(calcService);
//test the subtract functionality
Assert.assertEquals(mathApplication.subtract(20.0, 10.0), 10.0, 0);
//test the add functionality
Assert.assertEquals(mathApplication.add(20.0, 10.0), 30.0, 0);
//verify call to calcService is made or not
EasyMock.verify(calcService);
}
}
【问题讨论】:
-
猜猜这是对 RTFM 的提示...
标签: java junit mocking easymock inject