Java 如何检查TestNG Assert.assertEqual()条件检查
让我解释一下我到底期待什么 我有如下方法Java 如何检查TestNG Assert.assertEqual()条件检查,java,unit-testing,testng,Java,Unit Testing,Testng,让我解释一下我到底期待什么 我有如下方法 public void removeByObject() { try { DsrCollection dsrCollection = new DsrCollection(); dsrCollection. setNuId(180); dsrCollectionRepository.remove(dsrCollection); } catch (Exception e) {
public void removeByObject()
{
try {
DsrCollection dsrCollection = new DsrCollection();
dsrCollection. setNuId(180);
dsrCollectionRepository.remove(dsrCollection);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
在这里,我想检查特定的方法removeByObject()是否成功执行(还想包括Assert.assertEqual(dsrCollectionRepository.remove(dsrCollection),?)。因此,为了检查条件,实际值应该是多少
或者更具体地说,什么对象应该出现在实际值位置。我的要求类似于应用程序无法执行dsrCollectionRepository。remove(dsrCollection)应返回assertError消息。dsrCollectionRepository是否返回指示删除是否成功的值?我建议实现该函数,使其在成功删除对象时返回布尔值,否则依赖异常通知函数失败 如果它已经返回一个布尔值,则只返回assertTrue(dsrCollectionRepository.remove(dsrCollection); 如果函数失败引发已知异常,则可以使用以下注释测试函数:
@Test(expectedExceptions=MyException.class)
,但是您需要依赖这样一个事实,即在执行该函数时只能引发该异常,如果有人在您不知情的情况下添加了可以在实现中引发的异常,这将是一个问题。使removeByObject()
更易测试您只需将try
块中的代码提取到自己的方法中即可。例如:
public class DsrCollectionRepositoryManager /* Or whatever your class is called. */ {
/* ... */
public void removeByObject() {
try {
removeByObjectOrThrow();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected boolean /* or whatever your return type is */ removeByObjectOrThrow() {
DsrCollection dsrCollection = new DsrCollection();
dsrCollection.setNuId(180);
return dsrCollectionRepository.remove(dsrCollection);
}
/* ... */
}
现在您可以测试removeByObjectOrThrow()
,看看它是否引发异常
如果您试图在代表您调用removeByObject()
的位置对其进行测试,并且/或者您不想直接调用RemoveByObjectorRow()
,那么您可以对正在测试的单元进行子类化。例如:
public class DsrCollectionRepositoryManagerTest {
@Test
public void removeObjectSuccessful() {
boolean expected = true;
DsrCollectionRepositoryManager dsrCollectionRepositoryManager = new DsrCollectionRepositoryManager() {
@Override
protected boolean removeByObjectOrThrow() {
try {
boolean actual = super.removeByObjectOrThrow();
Assert.assertEquals(actual, expected);
return actual;
} catch (Exception cause) {
String message = "`removeObject` should not have thrown an exception but did";
throw new AssertionError(message, cause);
}
}
};
dsrCollectionRepositoryManager.removeByObject();
}
@Test
public void removeObjectUnsuccessful() {
DsrCollectionRepositoryManager dsrCollectionRepositoryManager = new DsrCollectionRepositoryManager() {
@Override
protected boolean removeByObjectOrThrow() {
super.removeByObjectOrThrow();
String detailMessage = "`removeByObject` should have thrown an exception but did not";
throw new AssertionError(detailMessage);
}
};
dsrCollectionRepositoryManager.removeByObject();
}
}
由于我不知道您被测试单元的名称,也不知道您想使用什么类型的
assertEquals
,我已经编造了一些东西,但想法是一样的:隔离您想测试的代码并测试它。感谢ans但dsrCollectionRepository.remove(dsrCollection)不返回任何布尔值。如果我更改remove()的返回类型,则会导致更改基本模型存储库实现。这是不被接受的。因此,您对此有任何其他建议吗?您是否可以检查dsrCollectionRepository以查看对象是否仍然存在?例如.isPresent或.hasObject函数?