Refine search
@Test public void getBeanByTypeAmbiguityRaisesException() { ApplicationContext context = new AnnotationConfigApplicationContext(TwoTestBeanConfig.class); try { context.getBean(TestBean.class); } catch (NoSuchBeanDefinitionException ex) { assertThat(ex.getMessage(), allOf( containsString("No qualifying bean of type '" + TestBean.class.getName() + "'"), containsString("tb1"), containsString("tb2") ) ); } }
static <V extends TemporalValue<?,V>> void assertEqualTemporal( V expected, V actual ) { assertThat( actual, allOf( equalTo( expected ), equalOn( "timezone", FrozenClockRule::timezone, expected ), equalOn( "temporal", TemporalValue::temporal, expected ) ) ); }
@Test public void testRandomAsciiRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{ASCII}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomAscii(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
@Test public void testAttributeHamcrestMatchers() throws Exception { mockMvc.perform(get("/")) .andExpect(model().attribute("integer", equalTo(3))) .andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value")))) .andExpect(model().attribute("person", hasProperty("name", equalTo("a name")))); }
private void assertConstructorThrows( long months, long days, long seconds, int nanos ) { InvalidValuesArgumentException e = assertThrows( InvalidValuesArgumentException.class, () -> duration( months, days, seconds, nanos ) ); assertThat( e.getMessage(), Matchers.allOf( Matchers.containsString( "Invalid value for duration" ), Matchers.containsString( "months=" + months ), Matchers.containsString( "days=" + days ), Matchers.containsString( "seconds=" + seconds ), Matchers.containsString( "nanos=" + nanos ) ) ); } }
@Test public void testRandomNumericRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Digit}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomNumeric(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
@Test public void test_getCacheHitPercentage() throws Exception { heapCache.put("key", "value"); heapCache.get("key"); heapCache.get("key"); heapCache.get("key"); heapCache.get("nokey"); heapCache.get("nokey"); assertThat(heapStatistics.getCacheHitPercentage(), is(allOf(greaterThan(59f), lessThan(61f)))); }
/** * Check some general validations that all REST responses should always pass. */ public static ClientResponse sanityCheck( ClientResponse response ) { List<String> contentEncodings = response.getHeaders().get( "Content-Encoding" ); String contentEncoding; if ( contentEncodings != null && (contentEncoding = Iterables.singleOrNull( contentEncodings )) != null ) { // Specifically, this is never used for character encoding. contentEncoding = contentEncoding.toLowerCase(); assertThat( contentEncoding, anyOf( containsString( "gzip" ), containsString( "deflate" ) ) ); assertThat( contentEncoding, allOf( not( containsString( "utf-8" ) ) ) ); } return response; }
@Test public void testRandomAlphanumericRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Alnum}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomAlphanumeric(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
@Test public void test_getCacheMissPercentage() throws Exception { heapCache.put("key", "value"); heapCache.get("key"); heapCache.get("key"); heapCache.get("key"); heapCache.get("nokey"); heapCache.get("nokey"); assertThat(heapStatistics.getCacheMissPercentage(), is(allOf(greaterThan(39f), lessThan(41f)))); }
@Property public void shouldHold( @InRange(min = "01/01/2012", max = "12/31/2012", format = "MM/dd/yyyy") Date d) throws Exception { SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); assertThat( d, allOf( greaterThanOrEqualTo(formatter.parse("01/01/2012")), lessThanOrEqualTo(formatter.parse("12/31/2012")))); } }
@Test public void testRandomGraphRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Graph}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomGraph(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
@Test public void testPlaygroundLaunches() { onView(componentHostWithText(containsString("Playground"))) .check(matches(isDisplayed())) .perform(click()); intended(allOf( toPackage("com.facebook.samples.litho"), hasComponent(hasClassName(DemoListActivity.class.getName())), hasExtras(hasEntry(equalTo("demoName"), equalTo("Playground"))))); } }
@Property public void shouldHold( @InRange( min = "2012-01-01T00:00:00.0Z", max = "2012-12-31T23:59:59.999999999Z") Instant i) { assertThat( i, allOf( greaterThanOrEqualTo(Instant.parse("2012-01-01T00:00:00.0Z")), lessThanOrEqualTo(Instant.parse("2012-12-31T23:59:59.999999999Z")))); } }
@Test public void testRandomAlphabeticRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Alpha}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomAlphabetic(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
@Test public void verifyAssertingOnViewInRemoteProcessIsSuccessful() { Log.d(TAG, "Checking main process name..."); onView(withId(R.id.textNamedProcess)).check(matches(withText(is(DEFAULT_PROC_NAME)))); Log.d(TAG, "Starting activity in a secondary process..."); onView(withId(R.id.startActivityBtn)).perform(click()); Log.d(TAG, "Checking private process name..."); onView(withId(R.id.textPrivateProcessName)) .check(matches(withText(is(DEFAULT_PROC_NAME + ":PID2")))); Log.d(TAG, "Clicking list item in private process activity..."); onData(allOf(instanceOf(String.class), is("Doppio"))).perform(click()); Log.d(TAG, "Check selected text appears..."); onView(withId(R.id.selectedListItemText)).check(matches(withText("Selected: Doppio"))); } }
@Property public void shouldHold(@SmallChange BigDecimal d) { assertEquals(2, d.scale()); assertThat( d, allOf( greaterThanOrEqualTo(BigDecimal.ZERO), lessThanOrEqualTo(new BigDecimal("20")))); } }
@Test public void testRandomPrintRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Print}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomPrint(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
@Test public void autoCompleteTextView_onDataClickAndCheck() { // NB: The autocompletion box is implemented with a ListView, so the preferred way // to interact with it is onData(). We can use inRoot here too! onView(withId(R.id.auto_complete_text_view)) .perform(typeText("S"), closeSoftKeyboard()); // This is useful because some of the completions may not be part of the View Hierarchy // unless you scroll around the list. onData(allOf(instanceOf(String.class), is("Baltic Sea"))) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .perform(click()); // The text should be filled in. onView(withId(R.id.auto_complete_text_view)) .check(matches(withText("Baltic Sea"))); } }