【发布时间】:2012-03-20 11:22:56
【问题描述】:
我正在尝试使用 TestNG 运行给定 here 的 EasyMock 示例,但遇到了一个奇怪的问题。前两个测试运行良好,但如果我单独运行,第三个测试 (getPriceDataAccessThrowsRuntimeException) 运行成功。但是,当我单独或一起运行其他两个测试时,第三个测试失败,我得到以下结果:
FAILED: getPriceDataAccessThrowsRuntimeException
org.testng.TestException:
Expected exception java.lang.RuntimeException but got org.testng.TestException:
Expected exception java.lang.RuntimeException but got java.lang.AssertionError:
Unexpected method call DataAccess.getPriceBySku("3283947"):
下面是测试代码:
@Test
public void getPrice() throws Exception {
// Set expectations on mocks.
expect(mockedDependency.getPriceBySku(SKU)).andReturn(new BigDecimal(100));
// Set mocks into testing mode.
replay(mockedDependency);
final BigDecimal price = systemUnderTest.getPrice(SKU);
assertNotNull(price);
// Verify behavior.
verify(mockedDependency);
}
@Test(expectedExceptions = MyCustomException.class)
public void getPriceNonExistentSkuThrowsException() throws Exception {
// Set expectations on mocks.
expect(mockedDependency.getPriceBySku(BAD_SKU)).andReturn(null);
// Set mocks into testing mode.
replay(mockedDependency);
final BigDecimal price = systemUnderTest.getPrice(BAD_SKU);
}
@Test(expectedExceptions = RuntimeException.class)
public void getPriceDataAccessThrowsRuntimeException() throws Exception {
// Set expectations on mocks.
expect(mockedDependency.getPriceBySku(SKU)).andThrow(new RuntimeException("Fatal data access exception."));
// Set mocks into testing mode.
replay(mockedDependency);
final BigDecimal price = systemUnderTest.getPrice(SKU);
}
各位大侠,我到底做错了什么?
【问题讨论】: