Intercept and assert private exception using junit

huangapple go评论53阅读模式
英文:

Intercept and assert private exception using junit

问题

有多个类似于我的问题,只有一点小区别,我想捕获私有内部异常(如果这是准确的定义),然后进行断言。我想出了下面的测试。我正在寻找类似于Spock中的thrown()的方法。在此先感谢您的帮助。我在JUnit方面经验不多,如果您觉得这很简单,请直接回答或提供可能帮助我理解的页面链接。

感谢您的帮助和支持!😀

异常 reactor.core.Exceptions$RetryExhaustedException

public abstract class Exceptions {

	static final class RetryExhaustedException extends IllegalStateException {

		RetryExhaustedException(String message) {
			super(message);
		}

		RetryExhaustedException(String message, Throwable cause) {
			super(message, cause);
		}
	}
}

我的测试

    @Test
    void exampleTest() {
        // given:
        stubToForce5xxResponse();

        // when:
        var ex = getExceptionThrown(() -> exampleService.exampleMethod());

        // then:
        Assertions.assertThat(ex).isNotNull();
        Assertions.assertThat(ex.getClass().getName()).isEqualTo("reactor.core.Exceptions$RetryExhaustedException");
        Assertions.assertThat(ex.getMessage()).isEqualTo("Retries exhausted: 3/3");
    }

    private Throwable getExceptionThrown(Runnable runnable) {
        try {
            runnable.run();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }
英文:

There are multiple question similar to mine with a small difference I want to catch private inner exception (if that's accurate definition), and then assert it. I come up with test below. I'm looking for similar method to one is Spock thrown(). Thanks for your help in advance. I do not have much experience with junit, if that's trivial for you just answer or link page that might help me understand.

Thanks for your help and support! Intercept and assert private exception using junit

The Exception reactor.core.Exceptions$RetryExhaustedException

public abstract class Exceptions {

	static final class RetryExhaustedException extends IllegalStateException {

		RetryExhaustedException(String message) {
			super(message);
		}

		RetryExhaustedException(String message, Throwable cause) {
			super(message, cause);
		}
	}
}

My test

    @Test
    void exampleTest() {
        // given:
        stubToForce5xxResponse();

        // when:
        var ex = getExceptionThrown(() -> exampleService.exampleMethod());

        // then:
        Assertions.assertThat(ex).isNotNull();
        Assertions.assertThat(ex.getClass().getName()).isEqualTo("reactor.core.Exceptions$RetryExhaustedException");
        Assertions.assertThat(ex.getMessage()).isEqualTo("Retries exhausted: 3/3");
    }

    private Throwable getExceptionThrown(Runnable runnable) {
        try {
            runnable.run();
            return null;
        } catch (Exception ex) {
            return ex;
        }
    }

答案1

得分: 1

Given your example code, I assume you use AssertJ:

@Test
void exampleTest() {
	stubToForce5xxResponse();

	assertThatThrownBy(() -> exampleService.exampleMethod())
		.isInstanceOfSatisfying(IllegalStateException.class, ex -> {
			assertThat(ex.getClass().getName()).isEqualTo("reactor.core.Exceptions$RetryExhaustedException");
			assertThat(ex).hasMessage("Retries exhausted: 3/3");
		});
}

If you want to stick to JUnit Jupiter assertions:

@Test
void exampleTest() {
	stubToForce5xxResponse();

	var ex = assertThrows(IllegalStateException.class, () -> exampleService.exampleMethod());

	assertEquals("reactor.core.Exceptions$RetryExhaustedException", ex.getClass().getName());
	assertEquals("Retries exhausted: 3/3", ex.getMessage());
}

(Note that AssertJ is asserThat(actual).isEqualTo(expected), whereas JUnit Jupiter is assertEquals(expected, actual).)

英文:

Given your example code, I assume you use AssertJ:

@Test
void exampleTest() {
	stubToForce5xxResponse();

	assertThatThrownBy(() -> exampleService.exampleMethod())
		.isInstanceOfSatisfying(IllegalStateException.class, ex -> {
			assertThat(ex.getClass().getName()).isEqualTo("reactor.core.Exceptions$RetryExhaustedException");
			assertThat(ex).hasMessage("Retries exhausted: 3/3");
		});
}

If you want to stick to JUnit Jupiter assertions:

@Test
void exampleTest() {
	stubToForce5xxResponse();

	var ex = assertThrows(IllegalStateException.class, () -> exampleService.exampleMethod());

	assertEquals("reactor.core.Exceptions$RetryExhaustedException", ex.getClass().getName());
	assertEquals("Retries exhausted: 3/3", ex.getMessage());
}

(Note that AssertJ is asserThat(actual).isEqualTo(expected), whereas JUnit Jupiter is assertEquals(expected, actual).)

huangapple
  • 本文由 发表于 2023年6月8日 00:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定