then样式,多个when-then?

hmae6n7t  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(213)

我知道建议在测试中使用“单Assert”,或者在bdd范式中使用一组“when”和“then”。
我在想,在测试异步api时,是否可以放松这个假设。其中请求必须首先提出,然后再完成。
像这样:

@Rule
public final Java6JUnitSoftAssertions softly = new Java6JUnitSoftAssertions();

@Test
public void setLessStrictCriterionGivenAllFetchesCompleted() {
    givenRepoWithCriterion(new MockCriterion(2));
    givenAllFetchesCompleted();

    // When I set a less strict criterion of ">= 1"
    final MockCriterion criterionMinValue1 = new MockCriterion(1);
    itemRepo.setCriteriaAndFetch(criterionMinValue1);

    // Then there is a fetch request in the queue, to fetch ">= 1"
    assertQueue(criterionMinValue1);
    // And there are loading items
    assertRepoItems(null, null, null);
    assertAll();

    // -------- Notice second group of When-Then starts here --------

    // When the fetch finishes
    fetcher.yieldFetch();

    // Then item list is 1 2 3
    assertRepoItems(1, 2, 3);
}

注意有两组when-then。
这个 assertAll() 有没有强制Assert第一组assertj软Assert。
在实际的实现中,通常通过http进行获取,但是对于测试,有一个模拟的获取程序,通过http模拟获取的完成 fetcher.yieldFetch(); 您认为总体结构是编写此类测试的好方法吗?
还有什么更好的方法?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题