有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查TestNG断言。assertEqual()条件检查

让我解释一下我到底期待什么 我有如下方法

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一样。删除(dsrCollection)它应该返回assertError消息


共 (2) 个答案

  1. # 1 楼答案

    为了使removeByObject()更易于测试,只需将try块中的代码提取到它自己的方法中。e、 g:

    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()和/或您不想直接调用removeByObjectOrThrow()的地方对其进行测试,那么您可以对测试单元进行子类化。e、 g:

    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,我已经编造了一些东西,但想法是一样的:分离你想测试的代码,然后测试它

  2. # 2 楼答案

    dsrCollectionRepository。移除(obj)返回一个值,指示移除是否成功?我建议实现该函数,以便在成功删除对象时返回布尔值,否则您将依赖异常来通知您函数失败

    如果它已经返回一个布尔值,只需assertTrue(dsrCollectionRepository.remove(dsrCollection);

    如果函数失败引发已知异常,可以使用以下注释测试函数: @Test(expectedExceptions = MyException.class)然而,您需要依赖这样一个事实,即在执行该函数时只能抛出该异常,如果有人添加了一个可以在您不知情的情况下在实现中抛出的异常,这将是有问题的