/** * Returns true if {@code e} is due to a firmware bug fixed after Android 4.2.2. * https://code.google.com/p/android/issues/detail?id=54072 */ public static boolean isAndroidGetsocknameError(AssertionError e) { return e.getCause() != null && e.getMessage() != null && e.getMessage().contains("getsockname failed"); }
/** * Execute the given Runnable, catch any {@link AssertionError}, decorate * with {@code AssertionError} containing diagnostic information about the * request and response, and then re-throw. */ public void assertWithDiagnostics(Runnable assertion) { try { assertion.run(); } catch (AssertionError ex) { throw new AssertionError(ex.getMessage() + "\n" + this, ex); } }
@Test public void assertValueSetMissing() { Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 4, 5, 6, 7)); try { Observable.range(1, 5) .test() .assertValueSet(set); throw new RuntimeException("Should have failed"); } catch (AssertionError ex) { assertTrue(ex.getMessage(), ex.getMessage().contains("Value not in the expected collection: " + 3)); } } }
@Test public void assertValueSetMissing() { Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 4, 5, 6, 7)); try { Flowable.range(1, 5) .test() .assertValueSet(set); throw new RuntimeException("Should have failed"); } catch (AssertionError ex) { assertTrue(ex.getMessage(), ex.getMessage().contains("Value not in the expected collection: " + 3)); } } }
@Test public void headerContainsWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void queryParamContainsWithMissingValue() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request)); assertThat(error.getMessage(), containsString("was \"bar\"")); }
@Test public void headerMissing() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("was null")); }
@Test public void headersWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void queryParamMissing() throws Exception { this.request.setURI(new URI("http://foo.com/a")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void queryParamMissingValue() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request)); assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>")); }
@Test public void headerContainsWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)); assertThat(error.getMessage(), containsString("was \"bar\"")); }
@Test public void headerMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bad").match(this.request)); assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>")); }
@Test public void methodNoMatch() throws Exception { this.request.setMethod(HttpMethod.POST); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)); assertThat(error.getMessage(), containsString("expected:<GET> but was:<POST>")); }
@Test public void headersWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Collections.singletonList("bar")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)); assertThat(error.getMessage(), containsString("to have at least <2> values")); }
@Test public void assertNoTimeout2() { try { Flowable.never() .test() .awaitCount(1, TestWaitStrategy.SLEEP_1MS, 50) .assertNoTimeout(); throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.getMessage().contains("Timeout?!")); } }
@Test public void assertTimeout2() { try { Flowable.empty() .test() .awaitCount(1, TestWaitStrategy.SLEEP_1MS, 50) .assertTimeout(); throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.getMessage().contains("No timeout?!")); } }
@Test public void repositoryIsNotTxProxy() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class); ctx.refresh(); try { assertTxProxying(ctx); fail("expected exception"); } catch (AssertionError ex) { assertThat(ex.getMessage(), equalTo("FooRepository is not a TX proxy")); } }
@Test public void unexpectedRequest() throws Exception { try { this.manager.validateRequest(createRequest(GET, "/foo")); } catch (AssertionError error) { assertEquals("No further requests expected: HTTP GET /foo\n" + "0 request(s) executed.\n", error.getMessage()); } }
@Test public void unexpectedRequest() throws Exception { try { this.manager.validateRequest(createRequest(GET, "/foo")); } catch (AssertionError error) { assertEquals("No further requests expected: HTTP GET /foo\n" + "0 request(s) executed.\n", error.getMessage()); } }
@Test public void contentTypeNoMatch() throws Exception { this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); try { executeAndVerify("foo"); } catch (AssertionError error) { String message = error.getMessage(); assertTrue(message, message.startsWith("Content type expected:<application/json;charset=UTF-8>")); } }