/** * Await until an instance field matches something. E.g. * <p> * <pre> * await().until(fieldIn(service).ofType(int.class).andWithName("fieldName"), greaterThan(2)); * </pre> * <p> * Here Awaitility waits until a field with name <code>fieldName</code> and of the <code>int.class</code> * in object <code>service</code> is greater than 2. * <p> * Note that the field must be thread-safe in order to guarantee correct behavior. * * @param object The object that contains the field. * @return A field supplier builder which lets you specify the parameters needed to find the field. */ public static FieldSupplierBuilder fieldIn(Object object) { return new FieldSupplierBuilder(object); }
/** * <p>Constructor for FieldSupplierBuilder.</p> * * @param object a {@link java.lang.Object} object. */ public FieldSupplierBuilder(Object object) { assertNotNullParameter(object, "Object passed to fieldIn"); this.object = object; }
private String generateFieldSupplierErrorMessage(Callable<T> supplier) { final FieldSupplierBuilder fieldSupplier = WhiteboxImpl.getInternalState(supplier, "this$0"); final Class<? extends Annotation> expectedAnnotation = fieldSupplier.getExpectedAnnotation(); final String expectedFieldName = fieldSupplier.getExpectedFieldName(); final Class<?> expectedFieldType = fieldSupplier.getExpectedFieldType(); final Object object = fieldSupplier.getObject(); final Class<?> objectClass = object instanceof Class ? (Class<?>) object : object.getClass();
@Test(timeout = 2000) public void typeOnly() throws Exception { new Asynch(fakeRepository).perform(); await().until(fieldIn(fakeRepository).ofType(int.class), equalTo(1)); assertEquals(1, fakeRepository.getValue()); }
@Test(timeout = 2000) public void returnsResultAfterFieldInSupplier() throws Exception { new Asynch(fakeRepository).perform(); int value = await().until(fieldIn(fakeRepository).ofType(int.class).andWithName("value"), equalTo(1)); assertEquals(1, value); } }
private String generateFieldSupplierErrorMessage(Callable<T> supplier) { final FieldSupplierBuilder fieldSupplier = WhiteboxImpl.getInternalState(supplier, "this$0"); final Class<? extends Annotation> expectedAnnotation = fieldSupplier.getExpectedAnnotation(); final String expectedFieldName = fieldSupplier.getExpectedFieldName(); final Class<?> expectedFieldType = fieldSupplier.getExpectedFieldType(); final Object object = fieldSupplier.getObject(); final Class<?> objectClass = object instanceof Class ? (Class<?>) object : object.getClass();
@Test(timeout = 2000) public void ofTypeAndName() throws Exception { new Asynch(fakeRepository).perform(); await().until(fieldIn(fakeRepository).ofType(int.class).andWithName("value"), equalTo(1)); assertEquals(1, fakeRepository.getValue()); } @Test(timeout = 2000)
/** * Await until a static field matches something. E.g. * <p> * <pre> * await().until(fieldIn(Service.class).ofType(int.class).andWithName("fieldName"), greaterThan(2)); * </pre> * <p> * Here Awaitility waits until a static field with name <code>fieldName</code> and of the * <code>int.class</code> in object <code>service</code> is greater than 2. * <p> * Note that the field must be thread-safe in order to guarantee correct behavior. * * @param clazz The class that contains the static field. * @return A field supplier builder which lets you specify the parameters needed to find the field. */ public static FieldSupplierBuilder fieldIn(Class<?> clazz) { return new FieldSupplierBuilder(clazz); } }
/** * <p>Constructor for FieldSupplierBuilder.</p> * * @param object a {@link java.lang.Object} object. */ public FieldSupplierBuilder(Object object) { assertNotNullParameter(object, "Object passed to fieldIn"); this.object = object; }
@Test(timeout = 2000, expected = FieldNotFoundException.class) public void givenTypeAndNameWhenNameMatchButTypeDoesntThenFieldNotFoundExceptionIsThrown() throws Exception { FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); byte one = (byte) 1; await().until(fieldIn(repository).ofType(byte.class).andWithName("value"), equalTo(one)); assertEquals(1, repository.getValue()); }
/** * Await until an instance field matches something. E.g. * <p> * <pre> * await().until(fieldIn(service).ofType(int.class).andWithName("fieldName"), greaterThan(2)); * </pre> * <p> * Here Awaitility waits until a field with name <code>fieldName</code> and of the <code>int.class</code> * in object <code>service</code> is greater than 2. * <p> * Note that the field must be thread-safe in order to guarantee correct behavior. * * @param object The object that contains the field. * @return A field supplier builder which lets you specify the parameters needed to find the field. */ public static FieldSupplierBuilder fieldIn(Object object) { return new FieldSupplierBuilder(object); }
@Test(timeout = 2000, expected = FieldNotFoundException.class) public void givenAnnotationAndTypeWhenTypeMatchButAnnotationDoesntThenFieldNotFoundExceptionIsThrown() throws Exception { FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); await().until(fieldIn(repository).ofType(int.class).andAnnotatedWith(ExampleAnnotation2.class), equalTo(1)); assertEquals(1, repository.getValue()); }
/** * Await until a static field matches something. E.g. * <p> * <pre> * await().until(fieldIn(Service.class).ofType(int.class).andWithName("fieldName"), greaterThan(2)); * </pre> * <p> * Here Awaitility waits until a static field with name <code>fieldName</code> and of the * <code>int.class</code> in object <code>service</code> is greater than 2. * <p> * Note that the field must be thread-safe in order to guarantee correct behavior. * * @param clazz The class that contains the static field. * @return A field supplier builder which lets you specify the parameters needed to find the field. */ public static FieldSupplierBuilder fieldIn(Class<?> clazz) { return new FieldSupplierBuilder(clazz); } }
@Test(timeout = 2000) public void givenStaticFieldAndUsingOfTypeAndName() throws Exception { FakeRepositoryWithStaticFieldAndAnnotation repository = new FakeRepositoryWithStaticFieldAndAnnotation(); new Asynch(repository).perform(); await().until(fieldIn(FakeRepositoryWithStaticFieldAndAnnotation.class).ofType(int.class).andWithName("value"), equalTo(1)); assertEquals(1, repository.getValue()); }
@Test(timeout = 2000) public void typeAndAnnotation() throws Exception { FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); await().until(fieldIn(repository).ofType(int.class).andAnnotatedWith(ExampleAnnotation.class), equalTo(1)); assertEquals(1, repository.getValue()); }
@Test(timeout = 2000, expected = FieldNotFoundException.class) public void givenStaticFieldAndUsingOfTypeAndNameThrowsFieldNotFoundExceptionWhenUsingInstance() throws Exception { FakeRepositoryWithStaticFieldAndAnnotation repository = new FakeRepositoryWithStaticFieldAndAnnotation(); new Asynch(repository).perform(); await().until(fieldIn(repository).ofType(int.class).andWithName("value"), equalTo(1)); assertEquals(1, repository.getValue()); }
@Test(timeout = 2000, expected = FieldNotFoundException.class) public void givenAnnotationAndTypeWhenAnnotationMatchButTypeDoesntThenFieldNotFoundExceptionIsThrown() throws Exception { FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); byte one = (byte) 1; await().until(fieldIn(repository).ofType(byte.class).andAnnotatedWith(ExampleAnnotation.class), equalTo(one)); assertEquals(1, repository.getValue()); }
@Test(timeout = 2000, expected = FieldNotFoundException.class) public void givenTypeAndNameWhenTypeMatchButNameDoesntThenFieldNotFoundExceptionIsThrown() throws Exception { FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); await().until(fieldIn(repository).ofType(int.class).andWithName("value2"), equalTo(1)); assertEquals(1, repository.getValue()); }
@Test public void showsErrorMessageContainingClassAndTypeWhenOnlyTypeSpecifiedWhenTimeout() throws Exception { exception.expect(ConditionTimeoutException.class); exception.expectMessage("Field in org.awaitility.classes.FakeRepositoryWithAnnotation of type int expected <1> but was <0> within 200 milliseconds."); FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); await().atMost(200, MILLISECONDS).until(fieldIn(repository).ofType(int.class), equalTo(1)); }
@Test(timeout = 2000, expected = FieldNotFoundException.class) public void givenTypeAndAnnotationAndNameWhenNameAndTypeMatchButAnnotationNotFoundThenFieldNotFoundExceptionIsThrown() throws Exception { FakeRepositoryWithAnnotation repository = new FakeRepositoryWithAnnotation(); new Asynch(repository).perform(); await().until( fieldIn(repository).ofType(int.class).andAnnotatedWith(ExampleAnnotation2.class).andWithName("value"), equalTo(1)); assertEquals(1, repository.getValue()); }