Best Assertj code snippet using org.assertj.core.error.BasicErrorMessageFactory.BasicErrorMessageFactory
Source:ApplicationContextAssert.java
...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}...
BasicErrorMessageFactory
Using AI Code Generation
1import org.assertj.core.api.Assertions;2import org.assertj.core.error.BasicErrorMessageFactory;3import org.junit.Test;4public class BasicErrorMessageFactoryTest {5 public void testBasicErrorMessageFactory() {6 String format = "format";7 Object[] arguments = new Object[] { "arg1", "arg2" };8 BasicErrorMessageFactory basicErrorMessageFactory = new BasicErrorMessageFactory(format, arguments);9 Assertions.assertThat(basicErrorMessageFactory).isNotNull();10 Assertions.assertThat(basicErrorMessageFactory.format).isEqualTo(format);11 Assertions.assertThat(basicErrorMessageFactory.arguments).isEqualTo(arguments);12 }13}
BasicErrorMessageFactory
Using AI Code Generation
1package org.assertj.core.error;2import static org.assertj.core.api.Assertions.assertThat;3import static org.assertj.core.error.BasicErrorMessageFactory.*;4import static org.assertj.core.error.ShouldNotBeNull.*;5import static org.assertj.core.error.ShouldNotBeEmpty.*;6import static org.assertj.core.error.ShouldHaveSize.*;7import static org.assertj.core.error.ShouldBeEmpty.*;8import static org.assertj.core.error.ShouldContain.*;9import static org.assertj.core.error.ShouldContainOnly.*;10import static org.assertj.core.error.ShouldContainSequence.*;11import static org.assertj.core.error.ShouldContainNull.*;12import static org.assertj.core.error.ShouldNotContain.*;13import static org.assertj.core.error.ShouldNotContainNull.*;14import static org.assertj.core.error.ShouldHaveSize.*;15import static org.assertj.core.error.ShouldHaveSameSizeAs.*;16import static org.assertj.core.error.ShouldHaveSameSizeAs.shouldHaveSameSizeAs;17import static org.assertj.core.error.ShouldContainNull.shouldContainNull;18import static org.assertj.core.error.ShouldContainOnly.shouldContainOnly;19import static org.assertj.core.error.ShouldContainSequence.shouldContainSequence;20import static org.assertj.core.error.ShouldContain.shouldContain;21import static org.assertj.core.error.ShouldNotBeEmpty.shouldNotBeEmpty;22import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;23import static org.assertj.core.error.ShouldNotContainNull.shouldNotContainNull;24import static org.assertj.core.error.ShouldNotContain.shouldNotContain;25import org.assertj.core.api.AssertionInfo;26import org.assertj.core.internal.TestDescription;27import org.junit.Test;28public class BasicErrorMessageFactoryTest {29 public void should_create_error_message_with_no_arguments() {30 ErrorMessageFactory factory = new BasicErrorMessageFactory("Hello");31 assertThat(factory.create(new TestDescription("Test"), new StandardRepresentation())).isEqualTo(32 "[Test] Hello");33 }34 public void should_create_error_message_with_one_argument() {35 ErrorMessageFactory factory = new BasicErrorMessageFactory("Hello %s", "World");36 assertThat(factory.create(new TestDescription("Test"), new StandardRepresentation())).isEqualTo(37 "[Test] Hello World");38 }39 public void should_create_error_message_with_two_arguments() {40 ErrorMessageFactory factory = new BasicErrorMessageFactory("Hello %s %s", "World", "!");41 assertThat(factory.create(new TestDescription("Test"), new StandardRepresentation())).isEqualTo(42 "[Test] Hello World !");43 }44 public void should_create_error_message_with_three_arguments() {45 ErrorMessageFactory factory = new BasicErrorMessageFactory("Helloport org.assertj.core.api.Assertions;46import org.assertj.core.error.BasicErrorMessageFactory;47import org.junit.Test;48public class BasicErrorMessageFactoryTest { to create an error message factory
BasicErrorMessageFactory
Using AI Code Generation
1 public void testBasicErrorMessageFactory() {2 String format = "format";3 Object[] arguments = new Object[] { "arg1", "arg2" };4 BasicErrorMessageFactory basicErrorMessageFactory = new BasicErrorMessageFactory(format, arguments);5 Assertions.assertThat(basicErrorMessageFactory).isNotNull();6 Assertions.assertThat(basicErrorMessageFactory.format).isEqualTo(format);7 Assertions.assertThat(basicErrorMessageFactory.arguments).isEqualTo(arguments);8 }9}
BasicErrorMessageFactory
Using AI Code Generation
1final String message = "should be";2final String expected = "expected";3final String actual = "actual";4final ErrorMessageFactory factory = new BasicErrorMessageFactory(message, expected, actual);5final String description = factory.create(new TextDescription("Test"), new StandardRepresentation());6System.out.println(description);7final String message = "%s";8final String expected = "expected";9final String actual = "actual";10final ErrorMessageFactory factory = new BasicErrorMessageFactory(message, expected, actual);11final String description = factory.create(new TextDescription("Test"), new StandardRepresentation());12System.out.println(description);13final String message = "%s";14final String expected = "%s";15final String actual = "%s";16final ErrorMessageFactory factory = new BasicErrorMessageFactory(message, expected, actual);
BasicErrorMessageFactory
Using AI Code Generation
1BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");2String errMsg = msg.create();3System.out.println(errMsg);4BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");5String errMsg = msg.create();6System.out.println(errMsg);7BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");8String errMsg = msg.create();9System.out.println(errMsg);10BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");11String errMsg = msg.create();12System.out.println(errMsg);13BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");14String errMsg = msg.create();15System.out.println(errMsg);16System.out.println(description);17final String message = "%s";18final String expected = "%s";19final String actual = "%s";20final String test = "%s";21final ErrorMessageFactory factory = new BasicErrorMessageFactory(message, expected, actual, test);22final String description = factory.create(new TextDescription("Test"), new StandardRepresentation());23System.out.println(description);
BasicErrorMessageFactory
Using AI Code Generation
1BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");2String errMsg = msg.create();3System.out.println(errMsg);4BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");5String errMsg = msg.create();6System.out.println(errMsg);7BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");8String errMsg = msg.create();9System.out.println(errMsg);10BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");11String errMsg = msg.create();12System.out.println(errMsg);13BasicErrorMessageFactory msg = new BasicErrorMessageFactory("Test Message", "Test Message");14String errMsg = msg.create();15System.out.println(errMsg);
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!!