How to use getRelevantClass method of org.assertj.core.internal.TypeHolder class

Best Assertj code snippet using org.assertj.core.internal.TypeHolder.getRelevantClass

Source:TypeHolder.java Github

copy

Full Screen

...51 * @param clazz the class for which to find a entity52 * @return the most relevant entity, or {@code null} if on entity could be found53 */54 public T get(Class<?> clazz) {55 Class<?> relevantType = getRelevantClass(clazz);56 return relevantType == null ? null : typeHolder.get(relevantType);57 }58 /**59 * Puts the {@code entity} for the given {@code clazz}.60 *61 * @param clazz the class for the comparator62 * @param entity the entity itself63 */64 public void put(Class<?> clazz, T entity) {65 typeHolder.put(clazz, entity);66 }67 /**68 * Checks, whether an entity is associated with the giving type.69 *70 * @param type the type for which to check an entity71 * @return is the giving type associated with any entity72 */73 public boolean hasEntity(Class<?> type) {74 return get(type) != null;75 }76 /**77 * @return {@code true} is there are registered entities, {@code false} otherwise78 */79 public boolean isEmpty() {80 return typeHolder.isEmpty();81 }82 /**83 * Removes all registered entities.84 */85 public void clear() {86 typeHolder.clear();87 }88 /**89 * Returns a sequence of all type-entity pairs which the current holder supplies.90 *91 * @return sequence of field-entity pairs92 */93 public Stream<Entry<Class<?>, T>> entityByTypes() {94 return typeHolder.entrySet().stream();95 }96 /**97 * Returns the most relevant class for the given type from the giving collection of types.98 * <p>99 * The order of checks is the following:100 * <ol>101 * <li>If there is a registered message for {@code clazz} then this one is used</li>102 * <li>We check if there is a registered message for a superclass of {@code clazz}</li>103 * <li>We check if there is a registered message for an interface of {@code clazz}</li>104 * </ol>105 * If there is no relevant type in the giving collection - {@code null} will be returned.106 *107 * @param cls type to find a relevant class.108 * @return the most relevant class.109 */110 private Class<?> getRelevantClass(Class<?> cls) {111 Set<Class<?>> keys = typeHolder.keySet();112 if (keys.contains(cls)) return cls;113 for (Class<?> superClass : getAllSuperclasses(cls)) {114 if (keys.contains(superClass)) return superClass;115 }116 for (Class<?> interfaceClass : getAllInterfaces(cls)) {117 if (keys.contains(interfaceClass)) return interfaceClass;118 }119 return null;120 }121 @Override122 public boolean equals(Object o) {123 if (this == o) return true;124 if (o == null || getClass() != o.getClass()) return false;...

Full Screen

Full Screen

getRelevantClass

Using AI Code Generation

copy

Full Screen

1public class TypeHolderTest {2 public void should_return_null_when_type_is_null() {3 assertThat(TypeHolder.getRelevantClass(null)).isNull();4 }5 public void should_return_null_when_type_is_not_parameterized() {6 assertThat(TypeHolder.getRelevantClass(Object.class)).isNull();7 }8 public void should_return_null_when_type_is_not_parameterized_with_array() {9 assertThat(TypeHolder.getRelevantClass(Object[].class)).isNull();10 }11 public void should_return_relevant_type_when_type_is_parameterized_with_array() {12 assertThat(TypeHolder.getRelevantClass(Object[][].class)).isEqualTo(Object.class);13 }14 public void should_return_relevant_type_when_type_is_parameterized() {15 assertThat(TypeHolder.getRelevantClass(List.class)).isNull();16 assertThat(TypeHolder.getRelevantClass(List.class.getTypeParameters()[0])).isNull();17 assertThat(TypeHolder.getRelevantClass(new ParameterizedType() {18 public Type[] getActualTypeArguments() {19 return new Type[] { Object.class };20 }21 public Type getRawType() {22 return List.class;23 }24 public Type getOwnerType() {25 return null;26 }27 })).isEqualTo(Object.class);28 }29}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful