- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的程序中,我抛出一个自定义异常对象MyCustomException
,如下所示:
public class MyCustomException
{
private MyCustomExceptionObject myCustomExceptionObject;
// Getters, Setters, Constructors...
}
public class MyCustomExceptionObject
{
int code;
// Getters, Setters, Constructors...
}
通过 Spring Boot Starter Test,我可以使用许多测试库。
目前我主要使用AssertJ。我的一项测试为方法提供了无效参数,并预计会出现异常。
@Test
public void test()
{
org.assertj.core.api.Assertions.assertThatThrownBy(() -> someMethod(-1)).isExactlyInstanceOf(MyCustomException.class);
}
public void someMethod(int number)
{
if (number < 0)
{
throw new MyCustomException(new MyCustomExceptionObject(12345));
}
//else do something useful
}
这工作正常,但我想更具体地测试异常,测试 code
是否符合预期。像这样的事情会起作用:
try
{
someMethod(-1);
}
catch (MyCustomException e)
{
if (e.getCode() == 12345)
{
return;
}
}
org.assertj.core.api.Assertions.fail("Exception not thrown");
但我宁愿寻找这样的一句台词:
org.assertj.core.api.Assertions.assertThatThrownBy(() -> someMethod(-1)).isExactlyInstanceOf(MyCustomException.class).exceptionIs((e) -> e.getCode() == 12345);
上面列出的任何测试库中是否存在类似的东西(首选 AssertJ)?
最佳答案
我会尝试catchThrowableOfType
它允许您断言自定义异常数据,例如:
class CustomParseException extends Exception {
int line;
int column;
public CustomParseException(String msg, int l, int c) {
super(msg);
line = l;
column = c;
}
}
CustomParseException e = catchThrowableOfType(
() -> { throw new CustomParseException("boom!", 1, 5); },
CustomParseException.class);
// assertions succeed
assertThat(e).hasMessageContaining("boom");
assertThat(e.line).isEqualTo(1);
assertThat(e.column).isEqualTo(5);
关于java - 使用 Spring Boot Starter Test 在单元测试中获取异常对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49936268/
有什么区别: spring-boot-starter-web spring-boot-starter-web-services spring-boot-starter-jersey? 文档说 Star
我注意到 Spring Boot Sample Data Redis 声明了以下依赖项: org.springframework.boot spring-boot-starter
我正在观看有关 Spring Boot 的视频教程(来自 javabrains.io)。示例项目的 pom 文件包含一个父 block ,其 groupId 为 org.springframework
我正在努力尝试使用 spring boot (1.1.6.RELEASE)、gradle (2.1) 和 Java 1.8 来练习一些技术。所以我们正在考虑支持一些旧式 SOAP Web 服务调用以及
我正在尝试学习 Spring Boot,但我注意到有两种选择。 spring-boot-starter-web - 根据文档,它支持全栈 Web 开发,包括 Tomcat 和 web-mvc spri
我正在尝试在 IntellIJ 中开发一个基于 Spring Boot 的示例应用程序。所以我使用了 spring Initialzr 方法,并在设置过程中进行了默认选择。我最终得到的 pom.xml
是否可以在一个应用程序中同时使用这两个启动器? 我想将 CSV 文件中的记录加载到数据库表中。 Spring Batch 表存储在不同的数据库中,因此我假设我需要使用 JTA 来处理事务。 每当我将
我有一个微服务应用程序,在我包含的 JAR 依赖项之一中 org.springframework.boot spring-boot-starter-webflux 所以我可以使用响
当我开始学习spring-webflux时,我对这个组件有疑问。 我构建了一个简单的项目,使用maven来管理它。我添加了与 spring-boot-starter-web 和 spring-boot
我已经使用 optaplanner-spring-boot-starter 作为依赖项开发了一个完整的工作 spring boot 项目,用于任务分配,但要求是使用 kie-server-spring
当我开始学习spring-webflux时,我对这个组件有疑问。 我构建了一个简单的项目,使用 maven 来管理它。我添加了与 spring-boot-starter-web 和 spring-bo
我尝试了 spring-boot-starter-actuator,默认指令,在 pom 中添加到 spring boot 项目,它运行没有任何问题。 一旦我添加 spring-boot-starte
我当时尝试包含上述依赖项之一,但找不到任何区别: spring boot starter web:我可以看到 Flux 和 Mono 类并制作一个响应式(Reactive)休息 Controller
我们将 spring-cloud-starter-hystrix 与 spring-cloud-starter-archaius 一起使用,一旦 war 取消部署,我们将无法停止 archaius 的
我刚刚开始使用 OAuth2 学习 Spring Security,我想为我的微服务 Restful Api 创建授权服务器和资源服务器。但是我不知道从哪个依赖项开始,因为我找到了其中的一些。我使用
我正在使用 spring-boot-starter-jersey 和 spring-boot-starter-tomcat (v. 1.5.2) 和作为因此,我宁愿不添加 spring-boot-st
我正在尝试对微服务的端点实现基本身份验证。所以我使用了 spring-boot-starter-security。但我注意到 spring-boot-starter-security 不适用于 spr
我想使用 spring-boot-starter,但这意味着我必须使用“spring-boot-starter-parent”pom 而不能使用公司广泛使用的根 pom。 Spring-boot-st
目前我们正在使用 com.graphql-java-kickstart graphql-spring-boot-starter ${graph
我是 Spring Cloud 的新手,两者之间的确切区别是什么spring-cloud-starter-eureka-server和 spring-cloud-starter-netflix-eur
我是一名优秀的程序员,十分优秀!