Best Assertj code snippet using org.assertj.core.error.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
1package com.javatpoint; 2import org.assertj.core.api.AbstractAssert; 3import org.assertj.core.error.BasicErrorMessageFactory; 4public class EmployeeAssert extends AbstractAssert<EmployeeAssert, Employee>{ 5public EmployeeAssert(Employee actual) { 6super(actual, EmployeeAssert.class); 7} 8public static EmployeeAssert assertThat(Employee actual) { 9return new EmployeeAssert(actual); 10} 11public EmployeeAssert hasName(String name) { 12if(actual.getName().equals(name)) { 13return this; 14} 15failWithMessage("Expected name to be <%s> but was <%s>", name, actual.getName()); 16return this; 17} 18public EmployeeAssert hasAge(int age) { 19if(actual.getAge() == age) { 20return this; 21} 22failWithMessage("Expected age to be <%s> but was <%s>", age, actual.getAge()); 23return this; 24} 25public EmployeeAssert hasSalary(double salary) { 26if(actual.getSalary() == salary) { 27return this; 28} 29failWithMessage("Expected salary to be <%s> but was <%s>", salary, actual.getSalary()); 30return this; 31} 32public EmployeeAssert hasDesignation(String designation) { 33if(actual.getDesignation().equals(designation)) { 34return this; 35} 36failWithMessage("Expected designation to be <%s> but was <%s>", designation, actual.getDesignation()); 37return this; 38} 39}40package com.javatpoint; 41import static org.assertj.core.api.Assertions.assertThat; 42import org.junit.Test; 43public class EmployeeTest { 44public void testAssertThat() { 45Employee employee = new Employee(); 46employee.setName("John"); 47employee.setAge(30); 48employee.setSalary(1000); 49employee.setDesignation("Developer"); 50assertThat(employee).hasName("John").hasAge(30).hasSalary(1000).hasDesignation("Developer"); 51} 52}53package com.javatpoint; 54public class Employee { 55private String name; 56private int age; 57private double salary; 58private String designation; 59public String getName() { 60return name; 61} 62public void setName(String name) { 63this.name = name; 64} 65public int getAge() { 66return age; 67} 68public void setAge(int age) { 69this.age = age; 70}
BasicErrorMessageFactory
Using AI Code Generation
1import org.assertj.core.error.BasicErrorMessageFactory;2import org.assertj.core.error.ErrorMessageFactory;3public class CustomErrorMessageFactory extends BasicErrorMessageFactory {4 public CustomErrorMessageFactory(String message, Object... arguments) {5 super(message, arguments);6 }7 public static ErrorMessageFactory customErrorMessage(String message, Object... arguments) {8 return new CustomErrorMessageFactory(message, arguments);9 }10}11We can now create our custom error message factory by using the customErrorMessage() method. The following code snippet shows how to create a custom error message factory:12import org.assertj.core.error.BasicErrorMessageFactory;13import org.assertj.core.error.ErrorMessageFactory;14public class CustomErrorMessageFactory extends BasicErrorMessageFactory {15 public CustomErrorMessageFactory(String message, Object... arguments) {16 super(message, arguments);17 }18 public static ErrorMessageFactory customErrorMessage(String message, Object... arguments) {19 return new CustomErrorMessageFactory(message, arguments);20 }21}22public class AssertJCustomErrorMessageFactory {23 public static void main(String[] args) {24 String actual = "This is a test";25 String expected = "This is not a test";26 assertThat(actual).as("Error message").isEqualTo(expected);27 }28}
BasicErrorMessageFactory
Using AI Code Generation
1import org.assertj.core.api.AbstractThrowableAssert;2import org.assertj.core.api.Assertions;3import org.assertj.core.error.BasicErrorMessageFactory;4import org.assertj.core.internal.Objects;5import org.assertj.core.util.VisibleForTesting;6import org.junit.Test;7import org.junit.runner.RunWith;8import org.junit.runners.JUnit4;9@RunWith(JUnit4.class)10public class BasicErrorMessageFactoryTest {11 public void should_create_error_message() {12 String message = "error message";13 String errorMessage = new BasicErrorMessageFactory(message).create(null, null);14 Assertions.assertThat(errorMessage).isEqualTo(message);15 }16 public void should_create_error_message_with_arguments() {17 String message = "error message with %s and %s";18 Object[] arguments = { "arg1", "arg2" };19 String errorMessage = new BasicErrorMessageFactory(message, arguments).create(null, null);20 Assertions.assertThat(errorMessage).isEqualTo("error message with arg1 and arg2");21 }22 public void should_be_equal_to_same_error_message_factory() {23 String message = "error message";24 BasicErrorMessageFactory factory = new BasicErrorMessageFactory(message);25 Assertions.assertThat(factory).isEqualTo(factory);26 }27 public void should_be_equal_to_error_message_factory_with_same_message() {28 String message = "error message";29 BasicErrorMessageFactory factory1 = new BasicErrorMessageFactory(message);30 BasicErrorMessageFactory factory2 = new BasicErrorMessageFactory(message);31 Assertions.assertThat(factory1).isEqualTo(factory2);32 }33 public void should_not_be_equal_to_error_message_factory_with_different_message() {34 BasicErrorMessageFactory factory1 = new BasicErrorMessageFactory("error message");35 BasicErrorMessageFactory factory2 = new BasicErrorMessageFactory("other error message");36 Assertions.assertThat(factory1).isNotEqualTo(factory2);37 }38 public void should_not_be_equal_to_null() {39 BasicErrorMessageFactory factory = new BasicErrorMessageFactory("error message");40 Assertions.assertThat(factory).isNotEqualTo(null);41 }
BasicErrorMessageFactory
Using AI Code Generation
1import org.assertj.core.error.BasicErrorMessageFactory;2import java.util.Arrays;3import java.util.List;4import static java.lang.String.format;5import static org.assertj.core.api.Assertions.assertThat;6import static org.assertj.core.error.ShouldContain.shouldContain;7import static org.assertj.core.error.ShouldNotContain.shouldNotContain;8import static org.assertj.core.util.Lists.newArrayList;9import static org.assertj.core.util.Objects.areEqual;10public class BasicErrorMessageFactoryTest {11 public static void main(String args[]) {12 List<String> list = newArrayList("Yoda", "Luke", "Leia");13 assertThat(list).doesNotContain("Han");14 assertThat(list).contains("Han");15 }16 public static ErrorMessageFactory shouldNotContain(Object actual, Object values, ComparisonStrategy comparisonStrategy) {17 return new BasicErrorMessageFactory(shouldNotContain(actual, values, comparisonStrategy).create(), actual, values);18 }19 public static ErrorMessageFactory shouldContain(Object actual, Object values, ComparisonStrategy comparisonStrategy) {20 return new BasicErrorMessageFactory(shouldContain(actual, values, comparisonStrategy).create(), actual, values);21 }22}23 at org.junit.Assert.assertEquals(Assert.java:115)24 at org.junit.Assert.assertEquals(Assert.java:144)25 at org.assertj.core.internal.Failures.failure(Failures.java:78)26 at org.assertj.core.internal.Failures.failure(Failures.java:70)27 at org.assertj.core.internal.Objects.assertEqual(Objects.java:102)28 at org.assertj.core.internal.Objects.assertEqual(Objects.java:91)29 at org.assertj.core.internal.Iterables.assertContains(Iterables.java:180)30 at org.assertj.core.api.AbstractIterableAssert.contains(AbstractIterableAssert.java:222)31 at org.assertj.core.api.AbstractIterableAssert.contains(AbstractIterableAssert.java:213)32 at org.assertj.core.api.AbstractIterableAssert.contains(AbstractIterableAssert.java:39)33 at BasicErrorMessageFactoryTest.main(BasicErrorMessageFactoryTest.java:16)34 at org.junit.Assert.assertEquals(Assert.java:115)35 at org.junit.Assert.assertEquals(Assert.java:144)36 at org.assertj.core.internal.Failures.failure(Failures.java:78)
BasicErrorMessageFactory
Using AI Code Generation
1import org.assertj.core.error.BasicErrorMessageFactory;2import org.assertj.core.error.ErrorMessageFactory;3public class MyErrorMessageFactory extends BasicErrorMessageFactory {4 private static final String SHOULD_CONTAIN = "%nExpecting:%n <%s>%nto contain:%n <%s>%nbut could not find:%n <%s>";5 public static ErrorMessageFactory shouldContain(String actual, String expected, String missing) {6 return new MyErrorMessageFactory(actual, expected, missing);7 }8 private MyErrorMessageFactory(String actual, String expected, String missing) {9 super(SHOULD_CONTAIN, actual, expected, missing);10 }11}12import org.assertj.core.api.AbstractStringAssert;13import org.assertj.core.api.AssertionInfo;14import org.assertj.core.internal.Failures;15import org.assertj.core.internal.Strings;16import org.assertj.core.util.VisibleForTesting;17public class MyStringAssert extends AbstractStringAssert<MyStringAssert> {18 Strings strings = Strings.instance();19 public MyStringAssert(String actual) {20 super(actual, MyStringAssert.class);21 }22 public MyStringAssert contains(String expected, String missing) {23 strings.assertContains(info, actual, expected, missing);24 return myself;25 }26 protected void failWithMessage(String failureMessage) {27 throw Failures.instance().failure(info, MyErrorMessageFactory.shouldContain(actual, expected, missing));28 }29}30import org.assertj.core.api.Assertions;31import org.junit.Test;32public class MyStringAssertTest {33 public void test() {34 Assertions.assertThat("abc").contains("a", "b");35 }36}37import org.assertj.core.api.Assertions;38import org.junit.Test;39public class MyErrorMessageFactoryTest {40 public void test() {41 Assertions.assertThat("abc").contains("a", "b");42 }43}
BasicErrorMessageFactory
Using AI Code Generation
1public void testAssertThatWithCustomErrorMessage() {2 assertThat(5).as("Test failed because of %s", "5 is not equal to 4").isEqualTo(4);3}4public void testAssertThatWithCustomErrorMessage() {5 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);6}7public void testAssertThatWithCustomErrorMessage() {8 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);9}10public void testAssertThatWithCustomErrorMessage() {11 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);12}13public void testAssertThatWithCustomErrorMessage() {14 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);15}16public void testAssertThatWithCustomErrorMessage() {17 assertThat(
BasicErrorMessageFactory
Using AI Code Generation
1public void testAssertThatWithCustomErrorMessage() {2 assertThat(5).as("Test failed because of %s", "5 is not equal to 4").isEqualTo(4);3}4public void testAssertThatWithCustomErrorMessage() {5 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);6}7public void testAssertThatWithCustomErrorMessage() {8 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);9}10public void testAssertThatWithCustomErrorMessage() {11 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);12}13public void testAssertThatWithCustomErrorMessage() {14 assertThat(5).as(new BasicErrorMessageFactory("Test failed because of %s", "5 is not equal to 4")).isEqualTo(4);15}16public void testAssertThatWithCustomErrorMessage() {17 assertThat(lic class BasicErrorMessageFactoryTest {18 public static void main(String args[]) {19 List<String> list = newArrayList("Yoda", "Luke", "Leia");20 assertThat(list).doesNotContain("Han");21 assertThat(list).contains("Han");22 }23 public static ErrorMessageFactory shouldNotContain(Object actual, Object values, ComparisonStrategy comparisonStrategy) {24 return new BasicErrorMessageFactory(shouldNotContain(actual, values, comparisonStrategy).create(), actual, values);25 }26 public static ErrorMessageFactory shouldContain(Object actual, Object values, ComparisonStrategy comparisonStrategy) {27 return new BasicErrorMessageFactory(shouldContain(actual, values, comparisonStrategy).create(), actual, values);28 }29}30 at org.junit.Assert.assertEquals(Assert.java:115)31 at org.junit.Assert.assertEquals(Assert.java:144)32 at org.assertj.core.internal.Failures.failure(Failures.java:78)33 at org.assertj.core.internal.Failures.failure(Failures.java:70)34 at org.assertj.core.internal.Objects.assertEqual(Objects.java:102)35 at org.assertj.core.internal.Objects.assertEqual(Objects.java:91)36 at org.assertj.core.internal.Iterables.assertContains(Iterables.java:180)37 at org.assertj.core.api.AbstractIterableAssert.contains(AbstractIterableAssert.java:222)38 at org.assertj.core.api.AbstractIterableAssert.contains(AbstractIterableAssert.java:213)39 at org.assertj.core.api.AbstractIterableAssert.contains(AbstractIterableAssert.java:39)40 at BasicErrorMessageFactoryTest.main(BasicErrorMessageFactoryTest.java:16)41 at org.junit.Assert.assertEquals(Assert.java:115)42 at org.junit.Assert.assertEquals(Assert.java:144)43 at org.assertj.core.internal.Failures.failure(Failures.java:78)
BasicErrorMessageFactory
Using AI Code Generation
1import org.assertj.core.error.BasicErrorMessageFactory;2import org.assertj.core.error.ErrorMessageFactory;3public class MyErrorMessageFactory extends BasicErrorMessageFactory {4 private static final String SHOULD_CONTAIN = "%nExpecting:%n <%s>%nto contain:%n <%s>%nbut could not find:%n <%s>";5 public static ErrorMessageFactory shouldContain(String actual, String expected, String missing) {6 return new MyErrorMessageFactory(actual, expected, missing);7 }8 private MyErrorMessageFactory(String actual, String expected, String missing) {9 super(SHOULD_CONTAIN, actual, expected, missing);10 }11}12import org.assertj.core.api.AbstractStringAssert;13import org.assertj.core.api.AssertionInfo;14import org.assertj.core.internal.Failures;15import org.assertj.core.internal.Strings;16import org.assertj.core.util.VisibleForTesting;17public class MyStringAssert extends AbstractStringAssert<MyStringAssert> {18 Strings strings = Strings.instance();19 public MyStringAssert(String actual) {20 super(actual, MyStringAssert.class);21 }22 public MyStringAssert contains(String expected, String missing) {23 strings.assertContains(info, actual, expected, missing);24 return myself;25 }26 protected void failWithMessage(String failureMessage) {27 throw Failures.instance().failure(info, MyErrorMessageFactory.shouldContain(actual, expected, missing));28 }29}30import org.assertj.core.api.Assertions;31import org.junit.Test;32public class MyStringAssertTest {33 public void test() {34 Assertions.assertThat("abc").contains("a", "b");35 }36}37import org.assertj.core.api.Assertions;38import org.junit.Test;39public class MyErrorMessageFactoryTest {40 public void test() {41 Assertions.assertThat("abc").contains("a", "b");42 }43}
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!!