英文:
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!
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)
.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论