当前位置: 首页 > news >正文

泉州企业免费建站网站建设与管理实训

泉州企业免费建站,网站建设与管理实训,搬瓦工建立wordpress,寿宁县建设局网站mock测试使用断言受GeeCON会议期间tkaczanowski演讲的启发#xff0c;我决定仔细研究AssertJ库的自定义断言。 在我的“骰子”游戏中#xff0c;我创建了一个“机会”#xff0c;它是骰子的任何组合#xff0c;其分数是所有骰子的总和。 这是相对简单的对象#xff1a; … mock测试使用断言 受GeeCON会议期间tkaczanowski演讲的启发我决定仔细研究AssertJ库的自定义断言。 在我的“骰子”游戏中我创建了一个“机会”它是骰子的任何组合其分数是所有骰子的总和。 这是相对简单的对象 class Chance implements Scorable {Overridepublic Score getScore(CollectionDice dice) {int sum dice.stream().mapToInt(die - die.getValue()).sum();return scoreBuilder(this).withValue(sum).withCombination(dice).build();} }public interface Scorable {Score getScore(CollectionDice dice); } 在我的测试中我想看看如何计算不同骰子组合的分数。 我从简单开始实际上只有一个 public class ChanceTest {private Chance chance new Chance();TestParameterspublic void chance(CollectionDice rolled, int scoreValue) {// arrangeCollectionDice rolled dice(1, 1, 3, 3, 3);// actScore score chance.getScore(rolled);// assertassertThat(actualScore.getScorable()).isNotNull();assertThat(actualScore.getValue()).isEqualTo(expectedScoreValue);assertThat(actualScore.getReminder()).isEmpty();assertThat(actualScore.getCombination()).isEqualTo(rolled);}} 测试中验证了单个概念得分对象。 为了提高分数验证的可读性和可重用性我将创建一个自定义断言。 我希望我的断言像其他任何AssertJ断言一样被使用如下所示 public class ChanceTest {private Chance chance new Chance();Testpublic void scoreIsSumOfAllDice() {CollectionDice rolled dice(1, 1, 3, 3, 3);Score score chance.getScore(rolled);ScoreAssertion.assertThat(score).hasValue(11).hasNoReminder().hasCombination(rolled);} } 为了实现这一点我需要创建一个从org.assertj.core.api.AbstractAssert扩展的ScoreAssertion类。 该类应具有公共静态工厂方法和所有必需的验证方法。 最后实现可能如下图所示。 class ScoreAssertion extends AbstractAssertScoreAssertion, Score {protected ScoreAssertion(Score actual) {super(actual, ScoreAssertion.class);}public static ScoreAssertion assertThat(Score actual) {return new ScoreAssertion(actual);}public ScoreAssertion hasEmptyReminder() {isNotNull();if (!actual.getReminder().isEmpty()) {failWithMessage(Reminder is not empty);}return this;}public ScoreAssertion hasValue(int scoreValue) {isNotNull();if (actual.getValue() ! scoreValue) {failWithMessage(Expected score to be %s, but was %s, scoreValue, actual.getValue());}return this;}public ScoreAssertion hasCombination(CollectionDice expected) {Assertions.assertThat(actual.getCombination()).containsExactly(expected.toArray(new Dice[0]));return this;} } 创建这样的断言的动机是拥有更多可读性和可重用性的代码。 但这要付出一些代价–需要创建更多代码。 在我的示例中我知道我很快就会创建更多Scorables并且需要验证它们的评分算法因此创建附加代码是合理的。 增益将可见。 例如我创建了一个NumberInARow类该类计算给定骰子组合中所有连续数字的分数。 分数是具有给定值的所有骰子的总和 class NumberInARow implements Scorable {private final int number;public NumberInARow(int number) {this.number number;}Overridepublic Score getScore(CollectionDice dice) {CollectionDice combination dice.stream().filter(value - value.getValue() number).collect(Collectors.toList());int scoreValue combination.stream().mapToInt(value - value.getValue()).sum();CollectionDice reminder dice.stream().filter(value - value.getValue() ! number).collect(Collectors.toList());return Score.scoreBuilder(this).withValue(scoreValue).withReminder(reminder).withCombination(combination).build();} } 我从连续检查两个5的测试开始但是我已经错过了断言 hasReminder 因此改进了ScoreAssertion 。 我继续用其他测试更改断言直到获得可以在测试中使用的非常完善的DSL public class NumberInARowTest {Testpublic void twoFivesInARow() {NumberInARow numberInARow new NumberInARow(5);CollectionDice dice dice(1, 2, 3, 4, 5, 5);Score score numberInARow.getScore(dice);// static import ScoreAssertionassertThat(score).hasValue(10).hasCombination(dice(5, 5)).hasReminder(dice(1, 2, 3, 4));}Testpublic void noNumbersInARow() {NumberInARow numberInARow new NumberInARow(5);CollectionDice dice dice(1, 2, 3);Score score numberInARow.getScore(dice);assertThat(score).isZero().hasReminder(dice(1, 2, 3));} }public class TwoPairsTest {Testpublic void twoDistinctPairs() {TwoPairs twoPairs new TwoPairs();CollectionDice dice dice(2, 2, 3, 3, 1, 4);Score score twoPairs.getScore(dice);assertThat(score).hasValue(10).hasCombination(dice(2, 2, 3, 3)).hasReminder(dice(1, 4));} } 更改后的断言如下所示 class ScoreAssertion extends AbstractAssertScoreAssertion, Score {protected ScoreAssertion(Score actual) {super(actual, ScoreAssertion.class);}public static ScoreAssertion assertThat(Score actual) {return new ScoreAssertion(actual);}public ScoreAssertion isZero() {hasValue(Score.ZERO);hasNoCombination();return this;}public ScoreAssertion hasValue(int scoreValue) {isNotNull();if (actual.getValue() ! scoreValue) {failWithMessage(Expected score to be %s, but was %s,scoreValue, actual.getValue());}return this;}public ScoreAssertion hasNoReminder() {isNotNull();if (!actual.getReminder().isEmpty()) {failWithMessage(Reminder is not empty);}return this;}public ScoreAssertion hasReminder(CollectionDice expected) {isNotNull();Assertions.assertThat(actual.getReminder()).containsExactly(expected.toArray(new Dice[0]));return this;}private ScoreAssertion hasNoCombination() {isNotNull();if (!actual.getCombination().isEmpty()) {failWithMessage(Combination is not empty);}return this;}public ScoreAssertion hasCombination(CollectionDice expected) {isNotNull();Assertions.assertThat(actual.getCombination()).containsExactly(expected.toArray(new Dice[0]));return this;} } 我真的很喜欢自定义AssertJ断言的想法。 在某些情况下它们将提高我的代码的可读性。 另一方面我很确定不能在所有情况下都使用它们。 尤其是在那些可重用机会很小的地方。 在这种情况下可以使用带有分组断言的私有方法。 你有什么意见 资源资源 https://github.com/joel-costigliola/assertj-core/wiki/Creating-specific-assertions tkaczanowski的断言演变 翻译自: https://www.javacodegeeks.com/2014/05/spice-up-your-test-code-with-custom-assertions.htmlmock测试使用断言
http://www.pierceye.com/news/89217/

相关文章:

  • 做网站是不是就能上传东西电商系统排行榜
  • 俄罗斯门户网站有哪些手机怎么编辑网页
  • 织梦网站首页标签网站建设布吉
  • 西安装修行业网站建设最好网站开发公司
  • 昆明网站营销网页制作
  • 网站建设的经费专业的网站建设报价
  • 企业网站模板php网站建设优化论坛
  • 做网站涉及到哪些wordpress友链自定义排序
  • 赣州营销型网站策划番禺建设网站哪家好
  • 做打鱼网站犯法不十堰市郧城建设网站
  • 引蜘蛛网站徐州集团网站建设
  • 免费制作主图的网站江西智能网站建设哪里有
  • 信息安全网站建设方案书点击排名软件哪个好
  • 企业网站添加图片网站被抄袭怎么办
  • 网站建设 软件开发的公司排名上海做网站比较好的公司有哪些
  • 网站建设备案不通过壹互联是网站公司吗
  • 扬州做网站网络系统开发
  • 网站的二级菜单怎么做设计师网页设计作品
  • 合肥网站建设技术什么是网络营销的重要组成部分
  • 网站建设注意哪些事项非常好的网站建设公司
  • 网站如何做交换链接免费建站公司联系方式
  • 个人网站名字取名怎么做网站建设客户资料收集清单
  • 中国建设银行总部网站wordpress 微博评论插件
  • 官方网站建设源码系统昨天正常的网站突然显示建设中
  • 大麦网建设网站的功能定位app安装官方免费下载
  • 南宁做网站开发的公司有哪些app软件下载网站源码
  • 刷赞抖音推广网站asp影视网站源码
  • 北京网站建设龙鹏php做网站需要注意什么
  • wordpress简易主题网站关键词在线优化
  • 银川网站建设哪家价格低别人做的网站自己根目录吗