Best JGiven code snippet using com.tngtech.jgiven.impl.inject.ScenarioStateField
Source:ValueInjector.java
...35 if (validatedClasses.get(object.getClass()) == Boolean.TRUE) {36 return;37 }38 Map<Object, Field> resolvedFields = Maps.newHashMap();39 for (ScenarioStateField field : getScenarioFields(object)) {40 field.getField().setAccessible(true);41 Resolution resolution = field.getResolution();42 Object key = null;43 if (resolution == Resolution.NAME) {44 key = field.getField().getName();45 } else {46 key = field.getField().getType();47 }48 if (resolvedFields.containsKey(key)) {49 Field existingField = resolvedFields.get(key);50 throw new AmbiguousResolutionException("Ambiguous fields with same " + resolution + " detected. Field 1: "51 + existingField + ", field 2: " + field.getField());52 }53 resolvedFields.put(key, field.getField());54 }55 validatedClasses.put(object.getClass(), Boolean.TRUE);56 }57 private List<ScenarioStateField> getScenarioFields(Object object) {58 @SuppressWarnings("unchecked")59 List<Field> scenarioFields = FieldCache60 .get(object.getClass())61 .getFieldsWithAnnotation(ScenarioState.class, ProvidedScenarioState.class, ExpectedScenarioState.class);62 return scenarioFields.stream()63 .map(ScenarioStateField.fromField)64 .collect(toList());65 }66 /**67 * @throws JGivenMissingGuaranteedScenarioStateException in case a field is guaranteed68 * and is not initialized by the finishing stage69 */70 @SuppressWarnings("unchecked")71 public void readValues(Object object) {72 validateFields(object);73 checkGuaranteedStatesAreInitialized(object);74 for (ScenarioStateField field : getScenarioFields(object)) {75 try {76 Object value = field.getField().get(object);77 updateValue(field, value);78 log.debug("Reading value {} from field {}", value, field.getField());79 } catch (IllegalAccessException e) {80 throw new RuntimeException("Error while reading field " + field.getField(), e);81 }82 }83 }84 /**85 * @throws JGivenMissingRequiredScenarioStateException in case a field requires86 * a value and the value is not present87 */88 @SuppressWarnings("unchecked")89 public void updateValues(Object object) {90 validateFields(object);91 for (ScenarioStateField field : getScenarioFields(object)) {92 Object value = getValue(field);93 if (value != null) {94 try {95 field.getField().set(object, value);96 } catch (IllegalAccessException e) {97 throw new RuntimeException("Error while updating field " + field.getField(), e);98 }99 log.debug("Setting field {} to value {}", field.getField(), value);100 } else if (field.isRequired()) {101 throw new JGivenMissingRequiredScenarioStateException(field.getField());102 }103 }104 }105 public <T> void injectValueByType(Class<T> clazz, T value) {106 state.updateValueByType(clazz, value);107 }108 public <T> void injectValueByName(String name, T value) {109 state.updateValueByName(name, value);110 }111 private void updateValue(ScenarioStateField field, Object value) {112 if (field.getResolution() == Resolution.NAME) {113 state.updateValueByName(field.getField().getName(), value);114 } else {115 state.updateValueByType(field.getField().getType(), value);116 }117 }118 private Object getValue(ScenarioStateField field) {119 if (field.getResolution() == Resolution.NAME) {120 return state.getValueByName(field.getField().getName());121 }122 return state.getValueByType(field.getField().getType());123 }124 private void checkGuaranteedStatesAreInitialized(Object instance) {125 for (Field field: FieldCache.get(instance.getClass())126 .getFieldsWithAnnotation(ProvidedScenarioState.class, ScenarioState.class)) {127 if (field.isAnnotationPresent(ProvidedScenarioState.class)) {128 if (field.getAnnotation(ProvidedScenarioState.class).guaranteed()) {129 checkInitialized(instance, field);130 }131 }132 if (field.isAnnotationPresent(ScenarioState.class)) {...
Source:ScenarioStateField.java
...8import com.tngtech.jgiven.annotation.ScenarioState.Resolution;9/**10 * Used internally to avoid repeated annotation lookups.11 */12final class ScenarioStateField {13 public static Function<Field, ScenarioStateField> fromField = new Function<Field, ScenarioStateField>() {14 @Override15 public ScenarioStateField apply( Field field ) {16 return new ScenarioStateField( field );17 }18 };19 private final Field field;20 private Resolution declaredResolution;21 private boolean required;22 private ScenarioStateField( Field field ) {23 this.field = field;24 collectAnnotations( field );25 if( declaredResolution == null ) {26 throw new IllegalArgumentException( "Field " + field + " has no valid annotation" );27 }28 }29 public Field getField() {30 return field;31 }32 /**33 * Return the {@link Resolution} defined for this state.34 */35 public Resolution getResolution() {36 if( declaredResolution == Resolution.AUTO ) {...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!