Skip to content

Commit

Permalink
Use JUnitParams for parameterised tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCorder authored and joel-costigliola committed Dec 12, 2015
1 parent dab83a3 commit a8e4c49
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 174 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -64,6 +64,12 @@
<version>0.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Expand Up @@ -13,18 +13,24 @@
package org.assertj.core.error;

import static org.assertj.core.api.Assertions.assertThat;

import static org.assertj.core.util.Arrays.array;

import org.assertj.core.description.Description;
import org.assertj.core.error.DescriptionFormatter;
import org.assertj.core.internal.TestDescription;
import org.junit.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

/**
* Tests for <code>{@link DescriptionFormatter#format(Description)}</code>.
*
* @author Alex Ruiz
* @author Dan Corder
*/
@RunWith(JUnitParamsRunner.class)
public class DescriptionFormatter_format_Test {

private static DescriptionFormatter formatter;
Expand All @@ -38,4 +44,17 @@ public static void setUpOnce() {
public void should_format_description_if_value_is_not_empty_or_null() {
assertThat(formatter.format(new TestDescription("Leia"))).isEqualTo("[Leia] ");
}

@Test
@Parameters(method = "testDescriptionGenerator")
public void should_return_empty_String(TestDescription testDescription) {
assertThat(formatter.format(testDescription)).isEmpty();
}

@SuppressWarnings("unused")
private Object testDescriptionGenerator() {
return array(null,
new TestDescription(null),
new TestDescription(""));
}
}

This file was deleted.

Expand Up @@ -14,38 +14,30 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;
import static org.assertj.core.util.Lists.newArrayList;
import static org.mockito.Mockito.*;

import java.util.List;
import static org.assertj.core.util.Arrays.array;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import org.assertj.core.description.Description;
import org.assertj.core.internal.TestDescription;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.*;
import org.junit.Before;
import org.junit.ComparisonFailure;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.*;
import org.junit.runners.Parameterized.Parameters;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

/**
* Tests for <code>{@link ShouldBeEqual#newAssertionError(Description, org.assertj.core.presentation.Representation)}</code>.
*
* @author Alex Ruiz
* @author Dan Corder
*/
@RunWith(Parameterized.class)
@RunWith(JUnitParamsRunner.class)
public class ShouldBeEqual_newAssertionError_Test {

private final String formattedDescription;

@Parameters
public static List<Object[]> parameters() {
return newArrayList(new Object[][] { { "[Jedi]" }, { "[Jedi] " } });
}

public ShouldBeEqual_newAssertionError_Test(String formattedDescription) {
this.formattedDescription = formattedDescription;
}

private Description description;
private ShouldBeEqual factory;
private DescriptionFormatter formatter;
Expand All @@ -59,10 +51,21 @@ public void setUp() {
}

@Test
public void should_create_ComparisonFailure_if_JUnit4_is_present_and_trim_spaces_in_formatted_description() {
when(formatter.format(description)).thenReturn(formattedDescription);
@Parameters(method = "formattedDescriptionGenerator")
public void should_create_ComparisonFailure_if_JUnit4_is_present_and_trim_spaces_in_formatted_description(String formattedDescription) {
// GIVEN
given(formatter.format(description)).willReturn(formattedDescription);
// WHEN
AssertionError error = factory.newAssertionError(description, new StandardRepresentation());
assertThat(error.getClass()).isEqualTo(ComparisonFailure.class);
assertThat(error.getMessage()).isEqualTo("[Jedi] expected:<\"[Yoda]\"> but was:<\"[Luke]\">");
// THEN
assertThat(error).isInstanceOf(ComparisonFailure.class)
.hasMessage("[Jedi] expected:<\"[Yoda]\"> but was:<\"[Luke]\">");
}

@SuppressWarnings("unused")
private Object formattedDescriptionGenerator() {
// We need to use explicit Object[]s here to stop JUnitParams stripping whitespace
return array(array("[Jedi]"),
array("[Jedi] "));
}
}

This file was deleted.

Expand Up @@ -13,22 +13,30 @@
package org.assertj.core.internal.strings;

import static org.assertj.core.error.ShouldBeEqualIgnoringWhitespace.shouldBeEqualIgnoringWhitespace;
import static org.assertj.core.test.CharArrays.arrayOf;
import static org.assertj.core.test.ErrorMessages.charSequenceToLookForIsNull;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.test.TestFailures.failBecauseExpectedAssertionErrorWasNotThrown;
import static org.assertj.core.util.Arrays.array;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.AssertionInfo;
import org.assertj.core.internal.StringsBaseTest;
import org.junit.Test;
import org.junit.runner.RunWith;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

/**
* Tests for <code>{@link org.assertj.core.internal.Strings#assertEqualsIgnoringWhitespace(org.assertj.core.api.AssertionInfo, CharSequence, CharSequence)} </code>.
*
* @author Alex Ruiz
* @author Joel Costigliola
* @author Alexander Bischof
* @author Dan Corder
*/
@RunWith(JUnitParamsRunner.class)
public class Strings_assertEqualsIgnoringWhitespace_Test extends StringsBaseTest {

@Test
Expand All @@ -50,7 +58,7 @@ public void should_fail_if_actual_is_not_null_and_expected_is_null() {
}

@Test
public void should_fail_if_both_Strings_are_not_equal_regardless_of_case() {
public void should_fail_if_both_Strings_are_not_equal_ignoring_whitespace() {
AssertionInfo info = someInfo();
try {
strings.assertEqualsIgnoringWhitespace(info, "Yoda", "Luke");
Expand All @@ -61,6 +69,28 @@ public void should_fail_if_both_Strings_are_not_equal_regardless_of_case() {
failBecauseExpectedAssertionErrorWasNotThrown();
}

@Test
@Parameters(method = "equalIgnoringWhitespaceGenerator")
public void should_pass_if_both_Strings_are_equal_ignoring_whitespace(String actual, String expected) {
strings.assertEqualsIgnoringWhitespace(someInfo(), actual, expected);
}

@SuppressWarnings("unused")
private Object equalIgnoringWhitespaceGenerator() {
// We need to use explicit Object[]s here to stop JUnitParams stripping whitespace
return array(array("my foo bar", "my foo bar"),
array(" my foo bar ", "my foo bar"),
array(" my\tfoo bar ", " my foo bar"),
array(" my foo bar ", "my foo bar"),
array(" my foo bar ", " my foo bar "),
array(" ", " "),
array(" my\tfoo bar ", new String(arrayOf(' ', 'm', 'y', ' ', 'f', 'o', 'o', ' ', 'b', 'a', 'r'))),
array(" my\tfoo bar ", " my\tfoo bar "), // same
array(null, null), // null
array(" \t \t", " "),
array(" abc", "abc "));
}

private void verifyFailureThrownWhenStringsAreNotEqualIgnoringWhitespace(AssertionInfo info, String actual,
String expected) {
verify(failures).failure(info, shouldBeEqualIgnoringWhitespace(actual, expected));
Expand Down

0 comments on commit a8e4c49

Please sign in to comment.