我知道有一种方法是:

@Test
public void foo() {
   try {
      // execute code that you expect not to throw Exceptions.
   } catch(Exception e) {
      fail("Should not have thrown any exception");
   }
}

还有更干净的方法吗?(可能使用了Junit的@Rule?)


当前回答

在这个问题上遇到了麻烦,因为我创建了一些泛型方法

@Test
void testSomething() {
   checkGeneric(anComplexObect)
}

在https://newbedev.com/sonarqube-issue-add-at-least-one-assertion-to-this-test-case-for-unit-test-with-assertions中提出了一些注释的东西。

解决方案要简单得多。将“checkGeneric”方法重命名为“assertGeneric”就足够了。

@Test
void testSomething() {
  assertGeneric(anComplexObect)
}

其他回答

您可以期望通过创建规则不会抛出异常。

@Rule
public ExpectedException expectedException = ExpectedException.none();

如果您不幸捕获了代码中的所有错误。 你可以愚蠢地做

class DumpTest {
    Exception ex;
    @Test
    public void testWhatEver() {
        try {
            thisShouldThrowError();
        } catch (Exception e) {
            ex = e;
        }
        assertEquals(null,ex);
    }
}

JUnit 5 (Jupiter)提供了三个函数来检查异常是否存在:

● 断言全部()

断言所有提供的可执行文件 不要抛出异常。

● assertDoesNotThrow()

类的执行 提供可执行/供应商 不抛出任何类型的异常。

此函数可用 JUnit 5.2.0以来(2018年4月29日)。

●assertThrows ()

断言所提供的可执行文件的执行 抛出expectedType的异常 并返回异常。

例子

package test.mycompany.myapp.mymodule;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class MyClassTest {

    @Test
    void when_string_has_been_constructed_then_myFunction_does_not_throw() {
        String myString = "this string has been constructed";
        assertAll(() -> MyClass.myFunction(myString));
    }
    
    @Test
    void when_string_has_been_constructed_then_myFunction_does_not_throw__junit_v520() {
        String myString = "this string has been constructed";
        assertDoesNotThrow(() -> MyClass.myFunction(myString));
    }

    @Test
    void when_string_is_null_then_myFunction_throws_IllegalArgumentException() {
        String myString = null;
        assertThrows(
            IllegalArgumentException.class,
            () -> MyClass.myFunction(myString));
    }

}

如果您想测试您的测试目标是否使用异常。只需要将测试保留为(使用jMock2的模拟合作者):

@Test
public void consumesAndLogsExceptions() throws Exception {

    context.checking(new Expectations() {
        {
            oneOf(collaborator).doSth();
            will(throwException(new NullPointerException()));
        }
    });

    target.doSth();
 }

如果您的目标确实使用抛出的异常,则测试将通过,否则测试将失败。

如果您想测试异常使用逻辑,事情会变得更加复杂。我建议将消费委托给一个可能被嘲笑的合作者。因此,测试可以是:

@Test
public void consumesAndLogsExceptions() throws Exception {
    Exception e = new NullPointerException();
    context.checking(new Expectations() {
        {
            allowing(collaborator).doSth();
            will(throwException(e));

            oneOf(consumer).consume(e);
        }
    });

    target.doSth();
 }

但如果你只是想记录它,有时它就设计过度了。在这种情况下,如果您坚持使用tdd,本文(http://java.dzone.com/articles/monitoring-declarative-transac, http://blog.novoj.net/2008/09/20/testing-aspect-pointcuts-is-there-an-easy-way/)可能会有所帮助。

在这个问题上遇到了麻烦,因为我创建了一些泛型方法

@Test
void testSomething() {
   checkGeneric(anComplexObect)
}

在https://newbedev.com/sonarqube-issue-add-at-least-one-assertion-to-this-test-case-for-unit-test-with-assertions中提出了一些注释的东西。

解决方案要简单得多。将“checkGeneric”方法重命名为“assertGeneric”就足够了。

@Test
void testSomething() {
  assertGeneric(anComplexObect)
}