网站设计培训班如何,应聘网站建设工程师,怀化网站优化公司推荐,网站推广的作用在哪里AssertJ是广泛使用的Hamcrest匹配器的替代匹配库。 实际上#xff0c;对于我自己的项目#xff0c;我已经更改为仅使用AssertJ-我只是发现流畅的界面和可扩展性非常吸引人。 您可以编写自定义断言#xff0c;如下所示#xff1a; 想象一下一种具有强度和饮料类型的咖啡 对于我自己的项目我已经更改为仅使用AssertJ-我只是发现流畅的界面和可扩展性非常吸引人。 您可以编写自定义断言如下所示 想象一下一种具有强度和饮料类型的咖啡 例如Espresso或Latte 。 定制CoffeeAssert根据其定制业务逻辑在本例中为其属性验证咖啡实例。 public class CoffeeAssert extends AbstractAssertCoffeeAssert, Coffee {public CoffeeAssert(Coffee actual) {super(actual, CoffeeAssert.class);}public static CoffeeAssert assertThat(Coffee actual) {return new CoffeeAssert(actual);}public CoffeeAssert hasType(Coffee.Type type) {isNotNull();if (actual.getType() ! type) {failWithMessage(Expected the coffee type to be %s but was %s, type, actual.getType());}return this;}// hasStrength(Strength) omitted ...public CoffeeAssert isNotDecaf() {isNotNull();if (actual.getStrength() Coffee.Strength.DECAF) {failWithMessage(Expected a coffee but got decaf!);}return this;}
} 然后可以使用自定义断言简单地验证咖啡实例。 assertThat的静态导入必须引用CoffeeAssert 。 import static com.example.coffee.CoffeeAssert.assertThat;
...Coffee coffee new Coffee();
coffee.setStrength(Strength.STRONG);
coffee.setType(Type.ESPRESSO);assertThat(coffee).hasType(Type.ESPRESSO).isNotDecaf(); 使用自定义断言可以极大地提高测试代码的质量。 这篇帖子从我的新闻简讯012中转贴了 发现帖子有用吗 订阅我的时事通讯获取有关IT和Java的更多免费内容技巧和窍门 翻译自: https://www.javacodegeeks.com/2018/01/write-custom-assertj-assertions.html