How to use checkIsNotAbstract method of org.assertj.core.api.junit.jupiter.SoftAssertionsExtension class

Best Assertj code snippet using org.assertj.core.api.junit.jupiter.SoftAssertionsExtension.checkIsNotAbstract

Source:SoftAssertionsExtension.java Github

copy

Full Screen

...230 for (Field softAssertionsField : softAssertionsFields) {231 checkIsNotStaticOrFinal(softAssertionsField);232 Class<? extends SoftAssertionsProvider> softAssertionsProviderClass = asSoftAssertionsProviderClass(softAssertionsField,233 softAssertionsField.getType());234 checkIsNotAbstract(softAssertionsField, softAssertionsProviderClass);235 checkHasDefaultConstructor(softAssertionsField, softAssertionsProviderClass);236 SoftAssertionsProvider softAssertions = getSoftAssertionsProvider(context, softAssertionsProviderClass);237 setTestInstanceSoftAssertionsField(testInstance, softAssertionsField, softAssertions);238 }239 }240 @Override241 public void beforeEach(ExtensionContext context) throws Exception {242 AssertionErrorCollector collector = getAssertionErrorCollector(context);243 if (isPerClassConcurrent(context)) {244 // If the current context is "per class+concurrent", then getSoftAssertionsProvider() will have already set the delegate245 // for all the soft assertions provider to the thread-local error collector, so all we need to do is set the tlec's value246 // for the current thread.247 ThreadLocalErrorCollector tlec = getThreadLocalCollector(context);248 tlec.setDelegate(collector);249 } else {250 // Make sure that all of the soft assertion provider instances have their delegate initialised to the assertion error251 // collector for the current context. Also check enclosing contexts (in the case of nested tests).252 while (initialiseDelegate(context, collector) && context.getParent().isPresent()) {253 context = context.getParent().get();254 }255 }256 }257 private static boolean initialiseDelegate(ExtensionContext context, AssertionErrorCollector collector) {258 Collection<SoftAssertionsProvider> providers = getSoftAssertionsProviders(context);259 if (providers == null) return false;260 providers.forEach(x -> x.setDelegate(collector));261 return context.getParent().isPresent();262 }263 @Override264 public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {265 // Abort if parameter type is unsupported.266 if (isUnsupportedParameterType(parameterContext.getParameter())) return false;267 Executable executable = parameterContext.getDeclaringExecutable();268 // @Testable is used as a meta-annotation on @Test, @TestFactory, @TestTemplate, etc.269 boolean isTestableMethod = executable instanceof Method && isAnnotated(executable, Testable.class);270 if (!isTestableMethod) {271 throw new ParameterResolutionException(format("Configuration error: cannot resolve SoftAssertionsProvider instances for [%s]. Only test methods are supported.",272 executable));273 }274 Class<?> parameterType = parameterContext.getParameter().getType();275 if (isAbstract(parameterType.getModifiers())) {276 throw new ParameterResolutionException(format("Configuration error: the resolved SoftAssertionsProvider implementation [%s] is abstract and cannot be instantiated.",277 executable));278 }279 try {280 parameterType.getDeclaredConstructor();281 } catch (@SuppressWarnings("unused") Exception e) {282 throw new ParameterResolutionException(format("Configuration error: the resolved SoftAssertionsProvider implementation [%s] has no default constructor and cannot be instantiated.",283 executable));284 }285 return true;286 }287 @Override288 public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {289 // The parameter type is guaranteed to be an instance of SoftAssertionsProvider290 @SuppressWarnings("unchecked")291 Class<? extends SoftAssertionsProvider> concreteSoftAssertionsProviderType = (Class<? extends SoftAssertionsProvider>) parameterContext.getParameter()292 .getType();293 SoftAssertionsProvider provider = ReflectionSupport.newInstance(concreteSoftAssertionsProviderType);294 provider.setDelegate(getAssertionErrorCollector(extensionContext));295 return provider;296 }297 @Override298 public void afterTestExecution(ExtensionContext extensionContext) {299 AssertionErrorCollector collector;300 if (isPerClassConcurrent(extensionContext)) {301 ThreadLocalErrorCollector tlec = getThreadLocalCollector(extensionContext);302 collector = tlec.getDelegate().orElseThrow(() -> new IllegalStateException("Expecting delegate to be present for current context"));303 tlec.reset();304 } else {305 collector = getAssertionErrorCollector(extensionContext);306 }307 AbstractSoftAssertions.assertAll(collector);308 }309 private static boolean isUnsupportedParameterType(Parameter parameter) {310 Class<?> type = parameter.getType();311 return !SoftAssertionsProvider.class.isAssignableFrom(type);312 }313 private static Store getStore(ExtensionContext extensionContext) {314 return extensionContext.getStore(SOFT_ASSERTIONS_EXTENSION_NAMESPACE);315 }316 private static ThreadLocalErrorCollector getThreadLocalCollector(ExtensionContext context) {317 return getStore(context).getOrComputeIfAbsent(ThreadLocalErrorCollector.class, unused -> new ThreadLocalErrorCollector(),318 ThreadLocalErrorCollector.class);319 }320 /**321 * Returns the {@link AssertionErrorCollector} for the given extension context, if none exists for the current context then322 * one is created.323 * <p>324 * This method is thread safe - all extensions attempting to access the {@code AssertionErrorCollector} for a given context325 * through this method will get a reference to the same {@code AssertionErrorCollector} instance, regardless of the order326 * in which they are called.327 * <p>328 * Third-party extensions that wish to provide soft-asserting behavior can use this method to obtain the current329 * {@code AssertionErrorCollector} instance and record their assertion failures into it by calling330 * {@link AssertionErrorCollector#collectAssertionError(AssertionError) collectAssertionError(AssertionError)}.<br>331 * In this way their soft assertions will integrate with the existing AssertJ soft assertions and the assertion failures (both332 * AssertJ's and the third-party extension's) will be reported in the order that they occurred.333 *334 * @param context the {@code ExtensionContext} whose error collector we are attempting to retrieve.335 * @return The {@code AssertionErrorCollector} for the given context.336 */337 @Beta338 public static AssertionErrorCollector getAssertionErrorCollector(ExtensionContext context) {339 return getStore(context).getOrComputeIfAbsent(AssertionErrorCollector.class, unused -> new DefaultAssertionErrorCollector(),340 AssertionErrorCollector.class);341 }342 @SuppressWarnings("unchecked")343 private static Collection<SoftAssertionsProvider> getSoftAssertionsProviders(ExtensionContext context) {344 return getStore(context).getOrComputeIfAbsent(Collection.class, unused -> new ConcurrentLinkedQueue<>(), Collection.class);345 }346 private static <T extends SoftAssertionsProvider> T instantiateProvider(ExtensionContext context, Class<T> providerType) {347 T softAssertions = ReflectionSupport.newInstance(providerType);348 // If we are running single-threaded, we won't have any concurrency issues. Likewise,349 // if we are running "per-method", then every test gets its own instance and again there350 // won't be any concurrency issues. But we need to special-case the situation where351 // we are running *both* per class and concurrent - use a thread-local so that each thread352 // gets its own copy. The beforeEach() callback above will take care of setting the353 // ThreadLocal collector's value for the thread in which it is executing.354 if (isPerClassConcurrent(context)) {355 softAssertions.setDelegate(getThreadLocalCollector(context));356 } else if (context.getTestMethod().isPresent()) {357 // If we're already in a method, then set our delegate as the beforeEach() which sets it may have already run.358 softAssertions.setDelegate(getAssertionErrorCollector(context));359 }360 getSoftAssertionsProviders(context).add(softAssertions);361 return softAssertions;362 }363 /**364 * Returns a {@link SoftAssertionsProvider} instance of the given type for the given extension context.365 * If no instance of the given type exists for the supplied context, then one is created.<br>366 * Note that the given type must be a concrete type with an accessible no-arg constructor for this method to work.367 * <p>368 * This method is thread safe - all extensions attempting to access the {@code SoftAssertionsProvider} for a369 * given context through this method will receive end up getting a reference to the same370 * {@code SoftAssertionsProvider} instance of that same type, regardless of the order in which they are called.371 * <p>372 * Third party extensions that wish to use soft assertions in their own implementation can use this373 * to get a {@code SoftAssertionsProvider} instance that interoperates with other soft-asserting374 * extensions (including {@code SoftAssertionsExtension}).375 * <p>376 * The {@code SoftAssertionExtension} will take care of initialising this provider instance's delegate377 * at the appropriate time, so that collected soft assertions are routed to the {@link AssertionErrorCollector}378 * instance for the current context.379 *380 * <pre><code class='java'> public class CustomExtension implements BeforeEachCallback {381 *382 * {@literal @}Override383 * public void beforeEach(ExtensionContext context) {384 * CustomSoftAssertions softly = SoftAssertionsExtension.getSoftAssertionsProvider(context, CustomSoftAssertions.class);385 * softly.assertThat(1).isOne();386 * }387 * }</code>388 * </pre>389 *390 * @param <T> the type of {@link SoftAssertionsProvider} to instantiate.391 * @param context the {@code ExtensionContext} whose error collector we are attempting to retrieve.392 * @param concreteSoftAssertionsProviderType the class instance for the type of soft assertions393 * @return The {@code AssertionErrorCollector} for the given context.394 */395 @Beta396 public static <T extends SoftAssertionsProvider> T getSoftAssertionsProvider(ExtensionContext context,397 Class<T> concreteSoftAssertionsProviderType) {398 return getStore(context).getOrComputeIfAbsent(concreteSoftAssertionsProviderType,399 unused -> instantiateProvider(context, concreteSoftAssertionsProviderType),400 concreteSoftAssertionsProviderType);401 }402 private static void setTestInstanceSoftAssertionsField(Object testInstance, Field softAssertionsField,403 SoftAssertionsProvider softAssertions) {404 softAssertionsField.setAccessible(true);405 try {406 softAssertionsField.set(testInstance, softAssertions);407 } catch (IllegalAccessException e) {408 throw new ExtensionConfigurationException(format("[%s] Could not gain access to field", softAssertionsField.getName()), e);409 }410 }411 private static void checkHasDefaultConstructor(Field softAssertionsField,412 Class<? extends SoftAssertionsProvider> softAssertionsProviderClass) {413 try {414 softAssertionsProviderClass.getDeclaredConstructor();415 } catch (@SuppressWarnings("unused") Exception e) {416 throw new ExtensionConfigurationException(format("[%s] SoftAssertionsProvider [%s] does not have a default constructor",417 softAssertionsField.getName(), softAssertionsProviderClass.getName()));418 }419 }420 private static void checkIsNotAbstract(Field softAssertionsField,421 Class<? extends SoftAssertionsProvider> softAssertionsProviderClass) {422 if (Modifier.isAbstract(softAssertionsProviderClass.getModifiers())) {423 throw new ExtensionConfigurationException(format("[%s] SoftAssertionsProvider [%s] is abstract and cannot be instantiated.",424 softAssertionsField.getName(), softAssertionsProviderClass));425 }426 }427 @SuppressWarnings("unchecked")428 private static Class<? extends SoftAssertionsProvider> asSoftAssertionsProviderClass(Field softAssertionsField,429 Class<?> providerClass) {430 if (!SoftAssertionsProvider.class.isAssignableFrom(providerClass)) {431 throw new ExtensionConfigurationException(format("[%s] field is not a SoftAssertionsProvider (%s).",432 softAssertionsField.getName(), providerClass.getTypeName()));433 }434 // Guaranteed because of the sanity check...

Full Screen

Full Screen

checkIsNotAbstract

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api.junit.jupiter;2import org.junit.jupiter.api.Test;3import org.junit.jupiter.api.extension.ExtendWith;4import org.junit.jupiter.api.extension.ExtensionContext;5import org.junit.jupiter.api.extension.ExtensionContext.Namespace;6import org.junit.jupiter.api.extension.ExtensionContext.Store;7import org.junit.jupiter.api.extension.TestInstancePostProcessor;8import org.junit.jupiter.api.extension.TestTemplateInvocationContext;9import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;10import org.junit.jupiter.api.extension.TestWatcher;11import org.junit.platform.commons.support.AnnotationSupport;12import org.opentest4j.TestAbortedException;13import java.lang.reflect.Method;14import java.util.List;15import java.util.Optional;16import java.util.function.Consumer;17import java.util.stream.Stream;18import static java.util.stream.Collectors.toList;19import static org.assertj.core.api.Assertions.assertThat;20import static org.assertj.core.api.Assertions.fail;21import static org.assertj.core.util.Lists.newArrayList;22import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;23import static org.junit.platform.commons.util.ReflectionUtils.findMethods;24import static org.junit.platform.commons.util.ReflectionUtils.isPrivate;25import static org.junit.platform.commons.util.ReflectionUtils.isStatic;26import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible;27public class SoftAssertionsExtension implements TestWatcher, TestTemplateInvocationContextProvider, TestInstancePostProcessor {28 private static final Namespace NAMESPACE = Namespace.create(SoftAssertionsExtension.class);29 private static final String STORE_KEY = "softAssertions";30 private static final String ABSTRACT_CLASS_ERROR_MESSAGE = "The test class must not be abstract.";31 public void postProcessTestInstance(Object testInstance, ExtensionContext context) {32 checkIsNotAbstract(testInstance.getClass(), context);33 Store store = getStore(context);34 store.put(STORE_KEY, new SoftAssertions());35 }36 public boolean supportsTestTemplate(ExtensionContext context) {37 return true;38 }39 public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {40 List<Method> methods = findTestTemplateMethods(context.getRequiredTestClass());41 return methods.stream()42 .map(method -> new SoftAssertionsInvocationContext(method));43 }44 private List<Method> findTestTemplateMethods(Class<?> testClass) {45 return findMethods(testClass, method -> isTestTemplateMethod(method, testClass));46 }47 private boolean isTestTemplateMethod(Method

Full Screen

Full Screen

checkIsNotAbstract

Using AI Code Generation

copy

Full Screen

1@ExtendWith(SoftAssertionsExtension.class)2class SoftAssertionsExtensionTest {3 void testSoftly(SoftAssertions softly) {4 softly.checkIsNotAbstract("java.lang.String");5 softly.checkIsNotAbstract("java.lang.Object");6 }7}8org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.assertj.core.api.SoftAssertions param0] in method [void SoftAssertionsExtensionTest.testSoftly(org.assertj.core.api.SoftAssertions)]. Registered resolvers are: []9 at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:200)10 at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameters(ExecutableInvoker.java:183)11 at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:74)12 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)13 at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)14 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)15 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)16 at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)17 at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)18 at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)19 at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)20 at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)21 at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)22 at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)23 at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)24 at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)25 at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)26 at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)27 at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)

Full Screen

Full Screen

checkIsNotAbstract

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.BeforeEach;2import org.junit.jupiter.api.Test;3import org.junit.jupiter.api.extension.ExtendWith;4import org.junit.jupiter.api.extension.ExtensionContext;5import org.junit.jupiter.api.extension.TestInstancePostProcessor;6import org.junit.jupiter.api.extension.TestTemplateInvocationContext;7import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;8import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtension;9import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest;10import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider;11import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext;12import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor;13import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.InterceptorInvocationContext;14import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.InterceptorInvocationContext.InterceptorInvocationMode;15import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.Invocation;16import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.Invocation.InvocationMode;17import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.Invocation.InvocationMode.InvocationModeContext;18import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.Invocation.InvocationMode.InvocationModeContext.InvocationModeContextMode;19import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.Invocation.InvocationMode.InvocationModeContext.InvocationModeContextMode.InvocationModeContextModeContext;20import org.junit.jupiter.api.extension.TestTemplateInvocationContextProviderExtensionTest.CustomTestTemplateInvocationContextProvider.CustomTestTemplateInvocationContext.InvocationInterceptor.Invocation.InvocationMode.InvocationModeContext.InvocationMode

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful