我正在尝试使用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);
}
任何想法的人,我做错了什么?
答案 0 :(得分:0)
从JUnit转换为TestNG时,您似乎犯了一个错误。在linked example中,doBeforeEachTestCase
方法在每个测试用例之前运行,这会将模拟的依赖项重置为其基本状态。您尚未包含所有代码:您应该doBeforeEachTestCase
annotated与BeforeMethod
一起运行TestNG。