How to use ShouldContainsOnlyOnce class of org.assertj.core.error package

Best Assertj code snippet using org.assertj.core.error.ShouldContainsOnlyOnce

Source:Iterables_assertContainsOnlyOnce_Test.java Github

copy

Full Screen

...13package org.assertj.core.internal.iterables;14import java.awt.Rectangle;15import org.assertj.core.api.AssertionInfo;16import org.assertj.core.api.Assertions;17import org.assertj.core.error.ShouldContainsOnlyOnce;18import org.assertj.core.internal.ErrorMessages;19import org.assertj.core.internal.IterablesBaseTest;20import org.assertj.core.test.TestData;21import org.assertj.core.test.TestFailures;22import org.assertj.core.util.Arrays;23import org.assertj.core.util.FailureMessages;24import org.assertj.core.util.Lists;25import org.assertj.core.util.Sets;26import org.junit.jupiter.api.Test;27import org.mockito.Mockito;28/**29 * Tests for30 * <code>{@link Iterables#assertContainsOnlyOnce(org.assertj.core.api.AssertionInfo, Iterable, Object[])}</code>.31 *32 * @author William Delanoue33 */34public class Iterables_assertContainsOnlyOnce_Test extends IterablesBaseTest {35 @Test36 public void should_pass_if_actual_contains_given_values_only_once() {37 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array("Luke", "Yoda", "Leia"));38 }39 @Test40 public void should_pass_if_actual_contains_given_values_only_once_even_if_actual_type_is_not_comparable() {41 // Rectangle class does not implement Comparable42 Rectangle r1 = new Rectangle(1, 1);43 Rectangle r2 = new Rectangle(2, 2);44 iterables.assertContainsOnlyOnce(TestData.someInfo(), Lists.newArrayList(r1, r2, r2), Arrays.array(r1));45 }46 @Test47 public void should_pass_if_actual_contains_given_values_only_once_with_null_element() {48 actual.add(null);49 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array("Luke", null, "Yoda", "Leia", null));50 }51 @Test52 public void should_pass_if_actual_contains_given_values_only_once_in_different_order() {53 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array("Leia", "Yoda", "Luke"));54 }55 @Test56 public void should_fail_if_actual_contains_given_values_more_than_once() {57 AssertionInfo info = TestData.someInfo();58 actual.addAll(Lists.newArrayList("Luke", "Luke", null, null));59 Object[] expected = new Object[]{ "Luke", "Luke", "Yoda", "Han", null };60 try {61 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, expected);62 } catch (AssertionError e) {63 Mockito.verify(failures).failure(info, ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, Sets.newLinkedHashSet("Han"), Sets.newLinkedHashSet("Luke", null)));64 return;65 }66 TestFailures.failBecauseExpectedAssertionErrorWasNotThrown();67 }68 @Test69 public void should_fail_if_actual_does_not_contains_null_value() {70 AssertionInfo info = TestData.someInfo();71 actual.addAll(Lists.newArrayList("Luke", "Luke"));72 Object[] expected = new Object[]{ null };73 try {74 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, expected);75 } catch (AssertionError e) {76 Mockito.verify(failures).failure(info, ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, Sets.newLinkedHashSet(Arrays.array(((String) (null)))), Sets.newLinkedHashSet()));77 return;78 }79 TestFailures.failBecauseExpectedAssertionErrorWasNotThrown();80 }81 @Test82 public void should_pass_if_actual_contains_given_values_only_once_even_if_duplicated() {83 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array("Luke", "Luke", "Luke", "Yoda", "Leia"));84 }85 @Test86 public void should_pass_if_actual_and_given_values_are_empty() {87 actual.clear();88 iterables.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array());89 }90 @Test91 public void should_fail_if_array_of_values_to_look_for_is_empty_and_actual_is_not() {92 Assertions.assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> iterables.assertContainsOnlyOnce(someInfo(), actual, emptyArray()));93 }94 @Test95 public void should_throw_error_if_array_of_values_to_look_for_is_null() {96 Assertions.assertThatNullPointerException().isThrownBy(() -> iterables.assertContainsOnlyOnce(someInfo(), emptyList(), null)).withMessage(ErrorMessages.valuesToLookForIsNull());97 }98 @Test99 public void should_fail_if_actual_is_null() {100 Assertions.assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> iterables.assertContainsOnlyOnce(someInfo(), null, array("Yoda"))).withMessage(FailureMessages.actualIsNull());101 }102 @Test103 public void should_fail_if_actual_does_not_contain_given_values_only_once() {104 AssertionInfo info = TestData.someInfo();105 Object[] expected = new Object[]{ "Luke", "Yoda", "Han" };106 try {107 iterables.assertContainsOnlyOnce(info, actual, expected);108 } catch (AssertionError e) {109 Mockito.verify(failures).failure(info, ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, Sets.newLinkedHashSet("Han"), Sets.newLinkedHashSet()));110 return;111 }112 TestFailures.failBecauseExpectedAssertionErrorWasNotThrown();113 }114 // ------------------------------------------------------------------------------------------------------------------115 // tests using a custom comparison strategy116 // ------------------------------------------------------------------------------------------------------------------117 @Test118 public void should_pass_if_actual_contains_given_values_only_once_according_to_custom_comparison_strategy() {119 iterablesWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array("LUKE", "YODA", "Leia"));120 }121 @Test122 public void should_pass_if_actual_contains_given_values_only_once_in_different_order_according_to_custom_comparison_strategy() {123 iterablesWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(TestData.someInfo(), actual, Arrays.array("LEIA", "yoda", "LukE"));124 }125 @Test126 public void should_fail_if_actual_contains_given_values_more_than_once_according_to_custom_comparison_strategy() {127 AssertionInfo info = TestData.someInfo();128 actual.addAll(Lists.newArrayList("Luke", "Luke"));129 Object[] expected = Arrays.array("luke", "YOda", "LeIA");130 try {131 iterablesWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(TestData.someInfo(), actual, expected);132 } catch (AssertionError e) {133 Mockito.verify(failures).failure(info, ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, Sets.newLinkedHashSet(), Sets.newLinkedHashSet("luke"), comparisonStrategy));134 return;135 }136 TestFailures.failBecauseExpectedAssertionErrorWasNotThrown();137 }138 @Test139 public void should_fail_if_actual_contains_given_values_more_than_once_even_if_duplicated_according_to_custom_comparison_strategy() {140 AssertionInfo info = TestData.someInfo();141 actual.addAll(Lists.newArrayList("LUKE"));142 Object[] expected = Arrays.array("LUke", "LUke", "lukE", "YOda", "Leia", "Han");143 try {144 iterablesWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(TestData.someInfo(), actual, expected);145 } catch (AssertionError e) {146 Mockito.verify(failures).failure(info, ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, Sets.newLinkedHashSet("Han"), Sets.newLinkedHashSet("LUke", "lukE"), comparisonStrategy));147 return;148 }149 TestFailures.failBecauseExpectedAssertionErrorWasNotThrown();150 }151 @Test152 public void should_fail_if_actual_does_not_contain_given_values_only_according_to_custom_comparison_strategy() {153 AssertionInfo info = TestData.someInfo();154 Object[] expected = new Object[]{ "Luke", "Yoda", "Han" };155 try {156 iterablesWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(info, actual, expected);157 } catch (AssertionError e) {158 Mockito.verify(failures).failure(info, ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, Sets.newLinkedHashSet("Han"), Sets.newLinkedHashSet(), comparisonStrategy));159 return;160 }161 TestFailures.failBecauseExpectedAssertionErrorWasNotThrown();162 }163}...

Full Screen

Full Screen

Source:ShouldContainsOnlyOnce_create_Test.java Github

copy

Full Screen

...10 *11 * Copyright 2012-2015 the original author or authors.12 */13package org.assertj.core.error;14import static org.assertj.core.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce;15import static org.assertj.core.util.Lists.newArrayList;16import static org.assertj.core.util.Sets.newLinkedHashSet;17import static org.assertj.core.api.Assertions.assertThat;18import org.assertj.core.description.TextDescription;19import org.assertj.core.internal.ComparatorBasedComparisonStrategy;20import org.assertj.core.presentation.StandardRepresentation;21import org.assertj.core.util.CaseInsensitiveStringComparator;22import org.junit.Before;23import org.junit.Test;24/**25 * Tests for <code>{@link ShouldContainsOnlyOnce#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.26 * 27 * @author William Delanoue28 */29public class ShouldContainsOnlyOnce_create_Test {30 private ErrorMessageFactory factory;31 @Before32 public void setUp() {33 factory = shouldContainsOnlyOnce(newArrayList("Yoda", "Han", "Han"), newArrayList("Luke", "Yoda"),34 newLinkedHashSet("Luke"), newLinkedHashSet("Han"));35 }36 @Test37 public void should_create_error_message() {38 String message = factory.create(new TextDescription("Test"), new StandardRepresentation());39 assertThat(message).isEqualTo(String.format(40 "[Test] %nExpecting:%n" +41 " <[\"Yoda\", \"Han\", \"Han\"]>%n" +42 "to contain only once:%n" +43 " <[\"Luke\", \"Yoda\"]>%n" +...

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.error.ShouldContainsOnlyOnce;2import org.assertj.core.description.Description;3import org.assertj.core.description.TextDescription;4import org.assertj.core.presentation.StandardRepresentation;5import org.assertj.core.presentation.Representation;6import org.assertj.core.util.VisibleForTesting;7public class ShouldContainsOnlyOnceTest {8 Description description = new TextDescription("Test");9 Representation representation = new StandardRepresentation();10 String actual = "actual";11 String expected = "expected";12 public void should_create_error_message() {13 String message = ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected, 1).create(description, representation);14 assertThat(message).isEqualTo("[Test] " + String.format("[actual] should contain only once [expected] but found [1] times"));15 }16}

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.error.ShouldContainsOnlyOnce;2import org.assertj.core.description.Description;3import org.assertj.core.presentation.Representation;4import org.assertj.core.presentation.StandardRepresentation;5import org.assertj.core.util.VisibleForTesting;6import static org.assertj.core.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce;7import static org.assertj.core.error.ShouldContainsOnlyOnce.shouldNotContainsOnlyOnce;8import static org.assertj.core.util.Preconditions.checkNotNull;9import static org.assertj.core.util.Strings.join;10import static org.assertj.core.util.Strings.quote;11import static org.assertj.core.util.Strings.unquote;12import static org.assertj.core.util.Strings.unquoteAndEscape;13import static org.assertj.core.util.Strings.unquoteAndEscapeArray;14import org.assertj.core.api.AbstractCharSequenceAssert;15import org.assertj.core.api.AbstractCharSequenceAssertBaseTest;16import org.assertj.core.api.AbstractCharSequenceAssert_shouldNotContainOnlyOnce_Test;17import org.assertj.core.api.Assertions;18import org.assertj.core.api.CharSequenceAssert;19import org.assertj.core.api.CharSequenceAssertBaseTest;20import org.assertj.core.api.ConcreteAssert;21import org.assertj.core.api.StringAssert;22import org.assertj.core.api.StringAssertBaseTest;23import org.assertj.core.api.StringAssert_shouldContainOnlyOnce_Test;24import org.assertj.core.api.StringAssert_shouldNotContainOnlyOnce_Test;25import org.assertj.core.api.StringAssert_startsWith_Test;26import org.assertj.core.api.StringAssert_startsWithIgnoringCase_Test;27import org.assertj

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.error.ShouldContainOnlyOnce;3import org.assertj.core.internal.TestDescription;4import org.assertj.core.presentation.StandardRepresentation;5import org.assertj.core.util.Throwables;6public class ShouldContainsOnlyOnceExample {7 public static void main(String[] args) {8 try {9 Assertions.assertThat("Hello World!").containsOnlyOnce("o");10 } catch (AssertionError e) {11 System.out.println(Throwables.getStackTraceAsString(e));12 }13 }14}15at org.assertj.core.error.ShouldContainOnlyOnce.create(ShouldContainOnlyOnce.java:30)16at org.assertj.core.internal.Failures.failure(Failures.java:98)17at org.assertj.core.internal.Failures.failure(Failures.java:84)18at org.assertj.core.internal.Strings.assertContainsOnlyOnce(Strings.java:370)19at org.assertj.core.api.AbstractStringAssert.containsOnlyOnce(AbstractStringAssert.java:200)20at ShouldContainsOnlyOnceExample.main(ShouldContainsOnlyOnceExample.java:16)

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.error;2import org.assertj.core.error.BasicErrorMessageFactory;3import org.assertj.core.error.ErrorMessageFactory;4import org.assertj.core.util.VisibleForTesting;5public class ShouldContainsOnlyOnce extends BasicErrorMessageFactory {6 public static ErrorMessageFactory shouldContainsOnlyOnce(Object actual, Object expected, int actualCount, int expectedCount) {7 return new ShouldContainsOnlyOnce(actual, expected, actualCount, expectedCount);8 }9 private ShouldContainsOnlyOnce(Object actual, Object expected, int actualCount, int expectedCount) {10 super("%nExpecting:%n <%s>%nto contain only once:%n <%s>%nbut it was found %s times and %s times respectively", actual, expected, actualCount, expectedCount);11 }12}13package org.assertj.core.error;14import org.assertj.core.error.BasicErrorMessageFactory;15import org.assertj.core.error.ErrorMessageFactory;16import org.assertj.core.util.VisibleForTesting;17public class ShouldContainsOnlyOnce extends BasicErrorMessageFactory {18 public static ErrorMessageFactory shouldContainsOnlyOnce(Object actual, Object expected, int actualCount, int expectedCount) {19 return new ShouldContainsOnlyOnce(actual, expected, actualCount, expectedCount);20 }21 private ShouldContainsOnlyOnce(Object actual, Object expected, int actualCount, int expectedCount) {22 super("%nExpecting:%n <%s>%nto contain only once:%n <%s>%nbut it was found %s times and %s times respectively", actual, expected, actualCount, expectedCount);23 }24}25package org.assertj.core.error;26import org.assertj.core.error.BasicErrorMessageFactory;27import org.assertj.core.error.ErrorMessageFactory;28import org.assertj.core.util.VisibleForTesting;29public class ShouldContainsOnlyOnce extends BasicErrorMessageFactory {30 public static ErrorMessageFactory shouldContainsOnlyOnce(Object actual, Object expected, int actualCount, int expectedCount) {31 return new ShouldContainsOnlyOnce(actual, expected, actualCount, expectedCount);32 }33 private ShouldContainsOnlyOnce(Object actual, Object expected, int actualCount, int expectedCount) {34 super("%nExpecting:%n <%s>%nto contain only once:%

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1package org.example;2import java.util.List;3import java.util.ArrayList;4import java.util.Arrays;5import org.assertj.core.api.Assertions;6import org.assertj.core.error.ShouldContainsOnlyOnce;7public class App {8 public static void main(String[] args) {9 List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));10 List<Integer> expected = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));11 Assertions.assertThat(numbers).containsOnlyOnce(expected);12 }13}14package org.example;15import java.util.List;16import java.util.ArrayList;17import java.util.Arrays;18import org.assertj.core.api.Assertions;19import org.assertj.core.error.ShouldContainsOnlyOnce;20public class App {21 public static void main(String[] args) {22 List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));23 List<Integer> expected = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));24 Assertions.assertThat(numbers).containsOnlyOnce(expected.toArray());25 }26}

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.error;2import static org.assertj.core.api.BDDAssertions.then;3import java.util.Arrays;4import java.util.List;5import org.junit.jupiter.api.Test;6public class ShouldContainsOnlyOnceTest {7 public void should_create_error_message() {8 List<String> actual = Arrays.asList("a", "b", "c");9 List<String> expected = Arrays.asList("a", "b", "c", "d");10 String errorMessage = ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected).create();11 then(errorMessage).isEqualTo(String.format("%n" +12 " <[\"a\", \"b\", \"c\"]>%n"));13 }14}15package org.assertj.core.error;16import static org.assertj.core.api.BDDAssertions.then;17import java.util.Arrays;18import java.util.List;19import org.junit.jupiter.api.Test;20public class ShouldContainsOnlyOnceTest {21 public void should_create_error_message() {22 List<String> actual = Arrays.asList("a", "b", "c");23 List<String> expected = Arrays.asList("a", "b", "c", "d");24 String errorMessage = ShouldContainsOnlyOnce.shouldContainsOnlyOnce(actual, expected).create();25 then(errorMessage).isEqualTo(String.format("%n" +26 " <[\"a\", \"b\", \"c\"]>%n"));27 }28}

Full Screen

Full Screen

ShouldContainsOnlyOnce

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.*;2import org.assertj.core.error.*;3import java.util.*;4public class ShouldContainsOnlyOnce {5 public static void main(String args[]) {6 List<String> list = new ArrayList<String>();7 list.add("one");8 list.add("two");9 list.add("three");10 Assertions.assertThat(list).containsOnlyOnce("one","two","three");11 }12}

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.

Most used methods in ShouldContainsOnlyOnce

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful