1 package junitparams; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 6 import junitparams.internal.Utils; 7 8 import static junitparams.JUnitParamsRunner.*; 9 import static junitparams.internal.Utils.*; 10 import static org.assertj.core.api.Assertions.assertThat; 11 import static org.junit.Assert.fail; 12 13 14 @RunWith(JUnitParamsRunner.class) 15 public class ObjectStringificationTest { 16 17 @Test stringifyString()18 public void stringifyString() throws Exception { 19 assertThat(stringify("exampleName", 0)).isEqualTo("[0] exampleName"); 20 } 21 22 @Test stringifyClassWithToStringAndOneParam()23 public void stringifyClassWithToStringAndOneParam() throws Exception { 24 ClassWithToString obj = new ClassWithToString("exampleName"); 25 26 assertThat(stringify(obj, 0)).isEqualTo("[0] exampleName"); 27 } 28 29 @Test stringifyClassWithToStringAndManyParams()30 public void stringifyClassWithToStringAndManyParams() throws Exception { 31 ClassWithToString obj1 = new ClassWithToString("one"); 32 ClassWithToString obj2 = new ClassWithToString("two"); 33 34 assertThat(stringify(new Object[]{obj1, obj2}, 0)).isEqualTo("[0] one, two"); 35 } 36 37 @Test stringifyClassWithToStringInSuperclass()38 public void stringifyClassWithToStringInSuperclass() throws Exception { 39 ClassWithToString obj = new ClassWithToString("exampleName"); 40 41 assertThat(Utils.stringify(obj, 0)).isEqualTo("[0] exampleName"); 42 } 43 44 private class ClassWithToString { 45 private String description; 46 ClassWithToString(String description)47 public ClassWithToString(String description) { 48 this.description = description; 49 } 50 51 @Override toString()52 public String toString() { 53 return description; 54 } 55 } 56 57 @Test stringifyArray()58 public void stringifyArray() { 59 Double[] nullArray = null; 60 int[] primitiveArray = {1, 2, 3}; 61 String[] array = {"one", "two", null}; 62 Object[] mixed = $( 63 $(nullArray, "stringOne", primitiveArray, "stringTwo", array) 64 ); 65 66 assertThat(Utils.stringify(mixed)).isEqualTo("null, stringOne, [1, 2, 3], stringTwo, [one, two, null]"); 67 } 68 69 @Test 70 @Parameters shouldCreateParameterObjectsOnce(Object object)71 public void shouldCreateParameterObjectsOnce(Object object) { 72 assertThat(object).isInstanceOf(A.class); 73 } 74 parametersForShouldCreateParameterObjectsOnce()75 public Object[] parametersForShouldCreateParameterObjectsOnce() { 76 return new Object[]{new A()}; 77 } 78 79 static class A { 80 static int instances = 0; 81 String test = "test"; 82 A()83 A() { 84 if (++instances > 1) { 85 fail(); 86 } 87 } 88 89 @Override toString()90 public String toString() { 91 return "[0] exampleName" + super.toString(); 92 } 93 } 94 } 95