Create tests for StringUtils#getStackTrace and StringUtils#getDifference

- Create tests
- Make StringUtils final with private constructor (utility class)
This commit is contained in:
ljacqu 2015-11-25 23:22:43 +01:00
parent 920a982794
commit a462780059
2 changed files with 35 additions and 1 deletions

View File

@ -10,10 +10,14 @@ import java.io.StringWriter;
/** /**
* Utility class for String operations. * Utility class for String operations.
*/ */
public class StringUtils { public final class StringUtils {
public static final String newline = System.getProperty("line.separator"); public static final String newline = System.getProperty("line.separator");
private StringUtils() {
// Utility class
}
/** /**
* Get the difference of two strings. * Get the difference of two strings.
* *

View File

@ -6,7 +6,9 @@ import java.net.MalformedURLException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -97,4 +99,32 @@ public class StringUtilsTest {
// then // then
assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format")); assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format"));
} }
@Test
public void shouldPrintStackTrace() {
// given
MalformedURLException ex = new MalformedURLException("Unrecognized URL format");
// when
String result = StringUtils.getStackTrace(ex);
// then
assertThat(result, stringContainsInOrder(getClass().getName()));
}
@Test
public void shouldGetDifferenceWithNullString() {
// given/when/then
assertThat(StringUtils.getDifference(null, "test"), equalTo(1.0));
assertThat(StringUtils.getDifference("test", null), equalTo(1.0));
assertThat(StringUtils.getDifference(null, null), equalTo(1.0));
}
@Test
public void shouldGetDifferenceBetweenTwoString() {
// given/when/then
assertThat(StringUtils.getDifference("test", "taste"), equalTo(0.4));
assertThat(StringUtils.getDifference("test", "bear"), equalTo(0.75));
assertThat(StringUtils.getDifference("test", "something"), greaterThan(0.88));
}
} }