@Test public void notContextAccessibleDueToPublisher() { Publisher<Integer> publisher = subscriber -> subscriber.onSubscribe(new Subscription() { @Override public void request(long l) { subscriber.onComplete(); } @Override public void cancel() { //NO-OP } }); Step<Integer> step = StepVerifier.create(publisher); DefaultContextExpectations<Integer> expectations = new DefaultContextExpectations<>(step, new ErrorFormatter(null)); assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> expectations.then().verifyComplete()) .withMessage("No propagated Context"); }
@Test public void notAssertThat() throws Exception { assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.assertThat(c -> { throw new AssertionError("boom"); })) .withMessage("boom"); }
@Test public void notContainsAllOfMap() throws Exception { Map<String, String> expected = new HashMap<>(); expected.put("foo", "bar"); expected.put("other", "stuff"); assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.containsAllOf(expected)) .withMessage("Expected Context Context2{foo=bar, foobar=baz} to contain all " + "of {other=stuff, foo=bar}"); }
@Override public StepVerifier.ContextExpectations<T> expectAccessibleContext() { return new DefaultContextExpectations<>(this, errorFormatter); }
private void assertContextExpectation( Function<Flux<Integer>, Flux<Integer>> sourceTransformer, Function<DefaultContextExpectations<Integer>, ContextExpectations<Integer>> expectations, long count) { Flux<Integer> source = sourceTransformer.apply(Flux.range(1, 10)); Step<Integer> step = StepVerifier.create(source); final DefaultContextExpectations<Integer> base = new DefaultContextExpectations<>(step, new ErrorFormatter(null)); expectations .apply(base) .then() .expectNextCount(count) .verifyComplete(); }
@Override public StepVerifier.ContextExpectations<T> expectAccessibleContext() { return new DefaultContextExpectations<>(this, errorFormatter); }
@Test public void notMatchesWithDescriptionAndScenarioName() { Flux<Integer> source = Flux.range(1, 10) .subscriberContext(Context.of("foo", "bar")); Step<Integer> step = StepVerifier.create(source); final DefaultContextExpectations<Integer> base = new DefaultContextExpectations<>(step, new ErrorFormatter("scenario")); assertThatExceptionOfType(AssertionError.class) .isThrownBy( base.matches(Objects::isNull, "someDescription") .then() .expectNextCount(10)::verifyComplete) .withMessage("[scenario] Context Context1{foo=bar} doesn't match predicate someDescription"); }
@Test public void notMatchesWithDescription() throws Exception { assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.matches(Objects::isNull, "desc")) .withMessage("Context Context1{foo=bar} doesn't match predicate desc"); }
@Test public void notMatches() throws Exception { assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.matches(Objects::isNull)) .withMessage("Context Context1{foo=bar} doesn't match predicate"); }
@Test public void notContainsOnlyOfMapContent() throws Exception { Map<String, String> expected = new HashMap<>(); expected.put("foo", "bar"); expected.put("other", "stuff"); assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.containsOnly(expected)) .withMessage("Expected Context Context2{foo=bar, foobar=baz} to contain " + "same values as {other=stuff, foo=bar}, but they differ in content"); }
@Test public void containsOnlyOfMap() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.containsOnly(Collections.singletonMap("foo", "bar"))); }
@Test public void containsAllOfContext() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.containsAllOf(Context.of("foo", "bar"))); }
@Test public void notContainsKey() throws Exception { assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.contains("fooz", "bar")) .withMessage("Expected value bar for key fooz, key not present in Context " + "Context2{foo=bar, foobar=baz}"); }
@Test public void notHasSize() throws Exception { assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")) .subscriberContext(Context.of("fails", true)), e -> e.hasSize(2)) .withMessageStartingWith("Expected Context of size 2, got 3 for Context Context3{"); }
@Test public void hasSize() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.hasSize(2)); }
@Test public void contains() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.contains("foo", "bar")); }
@Test public void notContainsOnlyOfMapSize() throws Exception { Map<String, String> expected = new HashMap<>(); expected.put("foo", "bar"); expected.put("other", "stuff"); assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.containsOnly(expected)) .withMessage("Expected Context Context1{foo=bar} to contain same values as " + "{other=stuff, foo=bar}, but they differ in size"); }
@Test public void notContainsOnlyOfContextContent() throws Exception { Context expected = Context.of("foo", "bar", "other", "stuff"); assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.containsOnly(expected)) .withMessage("Expected Context Context2{foo=bar, foobar=baz} to contain " + "same values as Context2{foo=bar, other=stuff}, but they differ in content"); }
@Test public void notContainsOnlyOfContextSize() throws Exception { Context expected = Context.of("foo", "bar", "other", "stuff"); assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.containsOnly(expected)) .withMessage("Expected Context Context1{foo=bar} to contain same values as " + "Context2{foo=bar, other=stuff}, but they differ in size"); }
@Test public void containsOnlyOfContext() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.containsOnly(Context.of("foo", "bar"))); }