微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

assertCustomThrownBy检查自定义异常的字段

如何解决assertCustomThrownBy检查自定义异常的字段

如何使用assertJ检查自定义exepiton中的特定字段值?

这是异常类:

import React,{ Component } from "react";

export class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      dateTime: ""
    };
  }

  getDateTime = () => {
    let Now = new Date();
    let year = Now.getFullYear();
    let month = Now.getMonth() + 1;
    let day = Now.getDate();
    let hour = Now.getHours();
    let minute = Now.getMinutes();
    let second = Now.getSeconds();
    if (month.toString().length === 1) {
      month = "0" + month;
    }
    if (day.toString().length === 1) {
      day = "0" + day;
    }
    if (hour.toString().length === 1) {
      hour = "0" + hour;
    }
    if (minute.toString().length === 1) {
      minute = "0" + minute;
    }
    if (second.toString().length === 1) {
      second = "0" + second;
    }
    var dateTime = hour + ":" + minute + ":" + second;
    return dateTime;
  };

  componentDidMount() {
    this.myInterval = setInterval(() => {
      this.setState({ dateTime: this.getDateTime() });
    },1000);
  }

  render() {
    return <div>{<h2>The time is {this.state.dateTime}</h2>}</div>;
  }
}

export default App;

这是我的考试:

public class SomeException extends RuntimeException {
    private final Set<Integer> something;

    public SomeException (String message,Set<Integer> something) {
        super(message);

        this.something = something;
    }

    public Set<Integer> getSomething() {
        return something;
    }
}

问题在于,如果我链接 extracting(),它将认为我正在使用 Throwable 。所以我无法提取 field 某物

更新

    assertthatThrownBy(() -> service.doSomething())
        .isinstanceOf(SomeException.class)
        .hasMessageStartingWith("SomeException has 1,2,3,4 in something field. I want assert that")
        . ??? check that SomeException.getSomething() has 1,4 ???

我已尝试按照建议的方法进行以下操作:

SomeException throwable = (SomeException) catchThrowable(() -> service.doSomething(

assertthat(throwable)
    .hasMessageStartingWith("extracting() bellow still think we're working with Throwable")
    .extracting(SomeException::getSomething <<<--- doesn't work here)

但是我不能再使用 containsExactlyInAnyOrder ()了:(

请告知

解决方法

extracting上有很多变体,您要使用的是extracting(String),例如:

   assertThatThrownBy(() -> service.doSomething())
        .isInstanceOf(SomeException.class)
        .hasMessageStartingWith("SomeException ... ")
        .extracting("something")
        .isEqualTo(1,2,3,4);

使用extracting(String,InstanceOfAssertFactory)获得专门的断言,因此,如果该值是一个集合,则可以尝试:

   assertThatThrownBy(() -> service.doSomething())
        .isInstanceOf(SomeException.class)
        .hasMessageStartingWith("SomeException ... ")
        .extracting("something",InstanceOfAssertFactories.ITERABLE)
        .contains();

您也可以尝试:hasFieldOrPropertyWithValue

更新:工作示例

SomeException throwable = new SomeException("foo",Sets.newSet(1,4));

assertThat(throwable).hasMessageStartingWith("fo")
                     .extracting("something",InstanceOfAssertFactories.ITERABLE)
                     .containsExactly(1,4);
,

我会做类似的事情:

assertThatThrownBy(() -> service.doSomething())
    .isInstanceOf(SomeException.class)
    .hasMessageStartingWith("SomeException occurred")
    .isEqualToComparingFieldByField(
        new SomeException("",Sets.newHashSet(1,4)));

这样您就不必担心将来更改字段名称,因为您不会在 assert 语句的任何位置对其进行硬编码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。