Best Assertj code snippet using org.assertj.core.error.BasicErrorMessageFactory.create
Source:ApplicationContextAssert.java
1/*2 * Copyright 2012-2017 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.springframework.boot.test.context.assertj;17import org.assertj.core.api.AbstractAssert;18import org.assertj.core.api.AbstractObjectArrayAssert;19import org.assertj.core.api.AbstractObjectAssert;20import org.assertj.core.api.AbstractThrowableAssert;21import org.assertj.core.api.Assertions;22import org.assertj.core.api.MapAssert;23import org.assertj.core.error.BasicErrorMessageFactory;24import org.springframework.beans.factory.NoSuchBeanDefinitionException;25import org.springframework.boot.test.context.runner.ApplicationContextRunner;26import org.springframework.context.ApplicationContext;27import org.springframework.util.Assert;28import static org.assertj.core.api.Assertions.assertThat;29/**30 * AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to an31 * {@link ApplicationContext}.32 *33 * @param <C> The application context type34 * @author Phillip Webb35 * @since 2.0.036 * @see ApplicationContextRunner37 * @see AssertableApplicationContext38 */39public class ApplicationContextAssert<C extends ApplicationContext>40 extends AbstractAssert<ApplicationContextAssert<C>, C> {41 private final Throwable startupFailure;42 /**43 * Create a new {@link ApplicationContextAssert} instance.44 * @param applicationContext the source application context45 * @param startupFailure the startup failure or {@code null}46 */47 ApplicationContextAssert(C applicationContext, Throwable startupFailure) {48 super(applicationContext, ApplicationContextAssert.class);49 Assert.notNull(applicationContext, "ApplicationContext must not be null");50 this.startupFailure = startupFailure;51 }52 /**53 * Verifies that the application context contains a bean with the given name.54 * <p>55 * Example: <pre class="code">56 * assertThat(context).hasBean("fooBean"); </pre>57 * @param name the name of the bean58 * @return {@code this} assertion object.59 * @throws AssertionError if the application context did not start60 * @throws AssertionError if the application context does not contain a bean with the61 * given name62 */63 public ApplicationContextAssert<C> hasBean(String name) {64 if (this.startupFailure != null) {65 throwAssertionError(new BasicErrorMessageFactory(66 "%nExpecting:%n <%s>%nto have bean named:%n <%s>%nbut context failed to start",67 getApplicationContext(), name));68 }69 if (findBean(name) == null) {70 throwAssertionError(new BasicErrorMessageFactory(71 "%nExpecting:%n <%s>%nto have bean named:%n <%s>%nbut found no such bean",72 getApplicationContext(), name));73 }74 return this;75 }76 /**77 * Verifies that the application context contains a single bean with the given type.78 * <p>79 * Example: <pre class="code">80 * assertThat(context).hasSingleBean(Foo.class); </pre>81 * @param type the bean type82 * @return {@code this} assertion object.83 * @throws AssertionError if the application context did not start84 * @throws AssertionError if the application context does no beans of the given type85 * @throws AssertionError if the application context contains multiple beans of the86 * given type87 */88 public ApplicationContextAssert<C> hasSingleBean(Class<?> type) {89 if (this.startupFailure != null) {90 throwAssertionError(new BasicErrorMessageFactory(91 "%nExpecting:%n <%s>%nto have a single bean of type:%n <%s>%nbut context failed to start",92 getApplicationContext(), type));93 }94 String[] names = getApplicationContext().getBeanNamesForType(type);95 if (names.length == 0) {96 throwAssertionError(new BasicErrorMessageFactory(97 "%nExpecting:%n <%s>%nto have a single bean of type:%n <%s>%nbut found no beans of that type",98 getApplicationContext(), type));99 }100 if (names.length > 1) {101 throwAssertionError(new BasicErrorMessageFactory(102 "%nExpecting:%n <%s>%nto have a single bean of type:%n <%s>%nbut found:%n <%s>",103 getApplicationContext(), type, names));104 }105 return this;106 }107 /**108 * Verifies that the application context does not contain any beans of the given type.109 * <p>110 * Example: <pre class="code">111 * assertThat(context).doesNotHaveBean(Foo.class); </pre>112 * @param type the bean type113 * @return {@code this} assertion object.114 * @throws AssertionError if the application context did not start115 * @throws AssertionError if the application context contains any beans of the given116 * type117 */118 public ApplicationContextAssert<C> doesNotHaveBean(Class<?> type) {119 if (this.startupFailure != null) {120 throwAssertionError(new BasicErrorMessageFactory(121 "%nExpecting:%n <%s>%nnot to have any beans of type:%n <%s>%nbut context failed to start",122 getApplicationContext(), type));123 }124 String[] names = getApplicationContext().getBeanNamesForType(type);125 if (names.length > 0) {126 throwAssertionError(new BasicErrorMessageFactory(127 "%nExpecting:%n <%s>%nnot to have a beans of type:%n <%s>%nbut found:%n <%s>",128 getApplicationContext(), type, names));129 }130 return this;131 }132 /**133 * Verifies that the application context does not contain a beans of the given name.134 * <p>135 * Example: <pre class="code">136 * assertThat(context).doesNotHaveBean("fooBean"); </pre>137 * @param name the name of the bean138 * @return {@code this} assertion object.139 * @throws AssertionError if the application context did not start140 * @throws AssertionError if the application context contains a beans of the given141 * name142 */143 public ApplicationContextAssert<C> doesNotHaveBean(String name) {144 if (this.startupFailure != null) {145 throwAssertionError(new BasicErrorMessageFactory(146 "%nExpecting:%n <%s>%nnot to have any beans of name:%n <%s>%nbut context failed to start",147 getApplicationContext(), name));148 }149 try {150 Object bean = getApplicationContext().getBean(name);151 throwAssertionError(new BasicErrorMessageFactory(152 "%nExpecting:%n <%s>%nnot to have a bean of name:%n <%s>%nbut found:%n <%s>",153 getApplicationContext(), name, bean));154 }155 catch (NoSuchBeanDefinitionException ex) {156 }157 return this;158 }159 /**160 * Obtain the beans names of the given type from the application context, the names161 * becoming the object array under test.162 * <p>163 * Example: <pre class="code">164 * assertThat(context).getBeanNames(Foo.class).containsOnly("fooBean"); </pre>165 * @param <T> the bean type166 * @param type the bean type167 * @return array assertions for the bean names168 * @throws AssertionError if the application context did not start169 */170 public <T> AbstractObjectArrayAssert<?, String> getBeanNames(Class<T> type) {171 if (this.startupFailure != null) {172 throwAssertionError(new BasicErrorMessageFactory(173 "%nExpecting:%n <%s>%nto get beans names with type:%n <%s>%nbut context failed to start",174 getApplicationContext(), type));175 }176 return Assertions.assertThat(getApplicationContext().getBeanNamesForType(type))177 .as("Bean names of type <%s> from <%s>", type, getApplicationContext());178 }179 /**180 * Obtain a single bean of the given type from the application context, the bean181 * becoming the object under test. If no beans of the specified type can be found an182 * assert on {@code null} is returned.183 * <p>184 * Example: <pre class="code">185 * assertThat(context).getBean(Foo.class).isInstanceOf(DefaultFoo.class);186 * assertThat(context).getBean(Bar.class).isNull();</pre>187 * @param <T> the bean type188 * @param type the bean type189 * @return bean assertions for the bean, or an assert on {@code null} if the no bean190 * is found191 * @throws AssertionError if the application context did not start192 * @throws AssertionError if the application context contains multiple beans of the193 * given type194 */195 public <T> AbstractObjectAssert<?, T> getBean(Class<T> type) {196 if (this.startupFailure != null) {197 throwAssertionError(new BasicErrorMessageFactory(198 "%nExpecting:%n <%s>%nto contain bean of type:%n <%s>%nbut context failed to start",199 getApplicationContext(), type));200 }201 String[] names = getApplicationContext().getBeanNamesForType(type);202 if (names.length > 1) {203 throwAssertionError(new BasicErrorMessageFactory(204 "%nExpecting:%n <%s>%nsingle bean of type:%n <%s>%nbut found:%n <%s>",205 getApplicationContext(), type, names));206 }207 T bean = (names.length == 0 ? null208 : getApplicationContext().getBean(names[0], type));209 return Assertions.assertThat(bean).as("Bean of type <%s> from <%s>", type,210 getApplicationContext());211 }212 /**213 * Obtain a single bean of the given name from the application context, the bean214 * becoming the object under test. If no bean of the specified name can be found an215 * assert on {@code null} is returned.216 * <p>217 * Example: <pre class="code">218 * assertThat(context).getBean("foo").isInstanceOf(Foo.class);219 * assertThat(context).getBean("foo").isNull();</pre>220 * @param name the name of the bean221 * @return bean assertions for the bean, or an assert on {@code null} if the no bean222 * is found223 * @throws AssertionError if the application context did not start224 */225 public AbstractObjectAssert<?, Object> getBean(String name) {226 if (this.startupFailure != null) {227 throwAssertionError(new BasicErrorMessageFactory(228 "%nExpecting:%n <%s>%nto contain a bean of name:%n <%s>%nbut context failed to start",229 getApplicationContext(), name));230 }231 Object bean = findBean(name);232 return Assertions.assertThat(bean).as("Bean of name <%s> from <%s>", name,233 getApplicationContext());234 }235 /**236 * Obtain a single bean of the given name and type from the application context, the237 * bean becoming the object under test. If no bean of the specified name can be found238 * an assert on {@code null} is returned.239 * <p>240 * Example: <pre class="code">241 * assertThat(context).getBean("foo", Foo.class).isInstanceOf(DefaultFoo.class);242 * assertThat(context).getBean("foo", Foo.class).isNull();</pre>243 * @param <T> the bean type244 * @param name the name of the bean245 * @param type the bean type246 * @return bean assertions for the bean, or an assert on {@code null} if the no bean247 * is found248 * @throws AssertionError if the application context did not start249 * @throws AssertionError if the application context contains a bean with the given250 * name but a different type251 */252 @SuppressWarnings("unchecked")253 public <T> AbstractObjectAssert<?, T> getBean(String name, Class<T> type) {254 if (this.startupFailure != null) {255 throwAssertionError(new BasicErrorMessageFactory(256 "%nExpecting:%n <%s>%nto contain a bean of name:%n <%s> (%s)%nbut context failed to start",257 getApplicationContext(), name, type));258 }259 Object bean = findBean(name);260 if (bean != null && type != null && !type.isInstance(bean)) {261 throwAssertionError(new BasicErrorMessageFactory(262 "%nExpecting:%n <%s>%nto contain a bean of name:%n <%s> (%s)%nbut found:%n <%s> of type <%s>",263 getApplicationContext(), name, type, bean, bean.getClass()));264 }265 return Assertions.assertThat((T) bean).as(266 "Bean of name <%s> and type <%s> from <%s>", name, type,267 getApplicationContext());268 }269 private Object findBean(String name) {270 try {271 return getApplicationContext().getBean(name);272 }273 catch (NoSuchBeanDefinitionException ex) {274 return null;275 }276 }277 /**278 * Obtain a map bean names and instances of the given type from the application279 * context, the map becoming the object under test. If no bean of the specified type280 * can be found an assert on an empty {@code map} is returned.281 * <p>282 * Example: <pre class="code">283 * assertThat(context).getBeans(Foo.class).containsKey("foo");284 * </pre>285 * @param <T> the bean type286 * @param type the bean type287 * @return bean assertions for the beans, or an assert on an empty {@code map} if the288 * no beans are found289 * @throws AssertionError if the application context did not start290 */291 public <T> MapAssert<String, T> getBeans(Class<T> type) {292 if (this.startupFailure != null) {293 throwAssertionError(new BasicErrorMessageFactory(294 "%nExpecting:%n <%s>%nto get beans of type:%n <%s> (%s)%nbut context failed to start",295 getApplicationContext(), type, type));296 }297 return Assertions.assertThat(getApplicationContext().getBeansOfType(type))298 .as("Beans of type <%s> from <%s>", type, getApplicationContext());299 }300 /**301 * Obtain the failure that stopped the application context from running, the failure302 * becoming the object under test.303 * <p>304 * Example: <pre class="code">305 * assertThat(context).getFailure().containsMessage("missing bean");306 * </pre>307 * @return assertions on the cause of the failure308 * @throws AssertionError if the application context started without a failure309 */310 public AbstractThrowableAssert<?, ? extends Throwable> getFailure() {311 hasFailed();312 return assertThat(this.startupFailure);313 }314 /**315 * Verifies that the application has failed to start.316 * <p>317 * Example: <pre class="code"> assertThat(context).hasFailed();318 * </pre>319 * @return {@code this} assertion object.320 * @throws AssertionError if the application context started without a failure321 */322 public ApplicationContextAssert<C> hasFailed() {323 if (this.startupFailure == null) {324 throwAssertionError(new BasicErrorMessageFactory(325 "%nExpecting:%n <%s>%nto have failed%nbut context started successfully",326 getApplicationContext()));327 }328 return this;329 }330 /**331 * Verifies that the application has not failed to start.332 * <p>333 * Example: <pre class="code"> assertThat(context).hasNotFailed();334 * </pre>335 * @return {@code this} assertion object.336 * @throws AssertionError if the application context failed to start337 */338 public ApplicationContextAssert<C> hasNotFailed() {339 if (this.startupFailure != null) {340 throwAssertionError(new BasicErrorMessageFactory(341 "%nExpecting:%n <%s>%nto have not failed:%nbut context failed to start",342 getApplicationContext()));343 }344 return this;345 }346 protected final C getApplicationContext() {347 return this.actual;348 }349 protected final Throwable getStartupFailure() {350 return this.startupFailure;351 }352}...
create
Using AI Code Generation
1import org.assertj.core.api.Assertions;2import org.assertj.core.error.BasicErrorMessageFactory;3import org.assertj.core.error.ErrorMessageFactory;4import org.junit.Test;5public class Assertions_assertThatAssertionErrorIsThrownBy_Test {6 public void test() {7 ErrorMessageFactory errorMessageFactory = BasicErrorMessageFactory.instance("message");8 Assertions.assertThatAssertionErrorIsThrownBy(() -> {9 throw new AssertionError();10 }).withMessage("message");11 }12}13 at org.junit.Assert.assertEquals(Assert.java:115)14 at org.junit.Assert.assertEquals(Assert.java:144)15 at org.assertj.core.api.Assertions$AbstractThrowableAssert.isThrownBy(Assertions.java:931)16 at org.assertj.core.api.Assertions.assertThatAssertionErrorIsThrownBy(Assertions.java:1157)17 at Assertions_assertThatAssertionErrorIsThrownBy_Test.test(Assertions_assertThatAssertionErrorIsThrownBy_Test.java:20)
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!!