Best Assertj code snippet using org.assertj.core.internal.Iterables.assertContainsOnlyOnce
Source:Iterables_assertContainsOnlyOnce_Test.java
...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}...
assertContainsOnlyOnce
Using AI Code Generation
1import org.assertj.core.internal.Iterables;2import org.junit.Test;3import java.util.Arrays;4public class AssertJTest {5 public void testAssertContainsOnlyOnce() {6 Iterables iterables = new Iterables();7 iterables.assertContainsOnlyOnce(null, Arrays.asList("a", "b"), "c");8 }9}10 at org.assertj.core.internal.Iterables.assertContainsOnlyOnce(Iterables.java:192)11 at AssertJTest.testAssertContainsOnlyOnce(AssertJTest.java:12)12public void assertContainsOnlyOnce(AssertionInfo info, Iterable<?> actual, Object value) {13 assertNotNull(info, actual);14 if (getOccurrences(actual, value) != 1) {15 throw failures.failure(info, shouldContainOnlyOnce(actual, value));16 }17 }18Recommended Posts: AssertJ | assertContainsOnlyOnce() method in ListAssert class19AssertJ | assertContainsOnlyOnce() method in ObjectArrayAssert class20AssertJ | assertContainsOnlyOnce() method in MapAssert class21AssertJ | assertContainsOnlyOnce() method in AbstractIterableAssert class22AssertJ | assertContainsOnlyOnce() method in AbstractListAssert class23AssertJ | assertContainsOnlyOnce() method in AbstractObjectArrayAssert class24AssertJ | assertContainsOnlyOnce() method in AbstractMapAssert class25AssertJ | assertContainsOnlyOnce() method in AbstractCharSequenceAssert class26AssertJ | assertContainsOnlyOnce() method in AbstractObjectAssert class27AssertJ | assertContainsOnlyOnce() method in AbstractComparableAssert class28AssertJ | assertContainsOnlyOnce() method in AbstractAtomicReferenceAssert class29AssertJ | assertContainsOnlyOnce() method in AbstractAtomicBooleanAssert class30AssertJ | assertContainsOnlyOnce() method in AbstractAtomicIntegerArrayAssert class
assertContainsOnlyOnce
Using AI Code Generation
1package com.journaldev.junit;2import static org.assertj.core.api.Assertions.assertThat;3import static org.assertj.core.api.Assertions.assertThatThrownBy;4import static org.assertj.core.api.Assertions.fail;5import static org.assertj.core.api.Assertions.within;6import static org.assertj.core.api.Assertions.withinPercentage;7import static org.assertj.core.api.Assertions.withinPrecision;8import static org.assertj.core.api.Assertions.withinStrictOffset;9import static org.assertj.core.api.Assertions.withinTolerance;10import static org.assertj.core.api.Assertions.withinTolerancePercentage;11import static org.assertj.core.api.Assertions.withinToleranceRatio;12import static org.assertj.core.api.Assertions.withinToleranceRatioPercentage;13import static org.assertj.core.api.Assertions.withinToleranceRatioStrictOffset;14import static org.assertj.core.api.Assertions.withinToleranceStrictOffset;15import java.util.ArrayList;16import java.util.List;17import org.junit.Test;18public class AssertJAssertionsTest {19 public void testAssertJAssertions() {20 List<String> list = new ArrayList<>();21 list.add("One");22 list.add("Two");23 list.add("Three");24 list.add("Four");25 list.add("Five");26 assertThat(list).contains("One", "Three");27 assertThat(list).containsOnly("One", "Two", "Three", "Four", "Five");28 assertThat(list).containsOnlyOnce("One", "Two", "Three", "Four", "Five");29 assertThat(list).containsExactly("One", "Two", "Three", "Four", "Five");30 assertThat(list).containsExactlyInAnyOrder("Five", "Four", "Three", "Two", "One");31 assertThat(list).containsSequence("One", "Two", "Three");32 assertThat(list).containsSubsequence("One", "Two", "Three");33 assertThat(list).doesNotContain("Six");34 assertThat(list).does
assertContainsOnlyOnce
Using AI Code Generation
1import org.assertj.core.internal.Iterables;2import org.assertj.core.internal.Iterables.assertContainsOnlyOnce;3Iterables iterables = new Iterables();4List<Integer> list = new ArrayList<>();5list.add(1);6list.add(2);7list.add(3);8list.add(4);9list.add(5);10iterables.assertContainsOnlyOnce("The list should contain only once the number 3", list, 3);11List<Integer> list2 = new ArrayList<>();12list2.add(1);13list2.add(2);14list2.add(3);15list2.add(4);16list2.add(5);17list2.add(3);18iterables.assertContainsOnlyOnce("The list should contain only once the number 3", list2, 3);19List<Integer> list3 = new ArrayList<>();20list3.add(1);21list3.add(2);22list3.add(3);23list3.add(4);24list3.add(5);25list3.add(6);26iterables.assertContainsOnlyOnce("The list should contain only once the number 3", list3, 3);27List<Integer> list4 = new ArrayList<>();28list4.add(1);29list4.add(2);30list4.add(3);31list4.add(4);32list4.add(5);33iterables.assertContainsOnlyOnce("The list should contain only once the number 3", list4, 6);34List<Integer> list5 = new ArrayList<>();35list5.add(1
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!!