Code Examples for

ScalaCheck: The Definitive Guide

Return to chapter index

2 ScalaCheck versus JUnit: A Complete Example

  • 2.1 The class under test
  • 2.2 Using JUnit
  • 2.3 Using ScalaCheck
  • 2.4 Conclusion
  • 2.1 The class under test


    import java.util.StringTokenizer; public class StringUtils { public static String truncate(String s, int n) { if(s.length() <= n) return s; else return s.substring(0, n) + "..."; } public static String[] tokenize( String s, char delim ) { String delimStr = new Character(delim).toString(); StringTokenizer st = new StringTokenizer( s, delimStr); String[] tokens = new String[st.countTokens()]; int i = 0; while(st.hasMoreTokens()) { tokens[i] = st.nextToken(); i++; } return tokens; } public static boolean contains( String s, String subString ) { return s.indexOf(subString) != -1; } }

    2.2 Using JUnit


    import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; @RunWith(JUnit4.class) public class StringUtilsTest { @Test public void testTruncateShortString() { String s = StringUtils.truncate("abc", 5); assertEquals("abc", s); } @Test public void testTruncateLongString() { String s = StringUtils.truncate("Hello World", 8); assertEquals("Hello Wo...", s); } @Test public void testTokenize() { String[] tokens = StringUtils.tokenize( "foo;bar;42", ';'); String[] expected = { "foo", "bar", "42" }; assertTrue(java.util.Arrays.equals(tokens, expected)); } @Test public void testTokenizeSingle() { String[] tokens = StringUtils.tokenize( "Hello World", ','); String[] expected = { "Hello World" }; assertTrue(java.util.Arrays.equals(tokens, expected)); } @Test public void testContainsTrue() { assertTrue(StringUtils.contains("abc", "bc")); } @Test public void testContainsFalse() { assertFalse(StringUtils.contains("abc", "42")); } }
    $ javac -cp junit-4.11.jar \ StringUtils.java StringUtilsTest.java $ java -cp .:junit-4.11.jar:hamcrest-core-1.3.jar \ org.junit.runner.JUnitCore StringUtilsTest JUnit version 4.11 ...... Time: 0.006 OK (6 tests)

    2.3 Using ScalaCheck


    import org.scalacheck.Properties import org.scalacheck.Prop import org.scalacheck.Gen.{listOf, alphaStr, numChar} object StringUtilsProps extends Properties("StringUtils") { property("truncate") = Prop.forAll { (s: String, n: Int) => val t = StringUtils.truncate(s, n) (s.length <= n && t == s) || (s.length > n && t == s.take(n)+"...") } property("tokenize") = Prop.forAll(listOf(alphaStr), numChar) { (ts, d) => val str = ts.mkString(d.toString) StringUtils.tokenize(str, d).toList == ts } property("contains") = Prop.forAll { (s1: String, s2: String, s3: String) => StringUtils.contains(s1+s2+s3, s2) } }
    $ javac StringUtils.java $ scalac -cp .:scalacheck.jar StringUtilProps.scala $ scala -cp .:scalacheck.jar StringUtilProps ! StringUtils.truncate: Exception raised on property evaluation. > ARG_0: "" > ARG_1: -1 > ARG_1_ORIGINAL: -1110151355 > Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 java.lang.String.substring(String.java:1911) StringUtils.truncate(StringUtils.java:7) StringUtilsProps$$anonfun$1.apply(StringUtilsProps.scala:9) StringUtilsProps$$anonfun$1.apply(StringUtilsProps.scala:8) org.scalacheck.Prop$$anonfun$forAll$10$$anonfun$apply$25 .apply(Prop.scala:759) ! StringUtils.tokenize: Falsified after 5 passed tests. > ARG_0: List("") > ARG_0_ORIGINAL: List("", "yHa", "vlez", "Oyex", "lhz") > ARG_1: 2 + StringUtils.contains: OK, passed 100 tests.
    property("truncate") = Prop.forAll { (s: String, n: Int) => lazy val t = StringUtils.truncate(s, n) if (n < 0) Prop.throws( classOf[StringIndexOutOfBoundsException] ) { t } else (s.length <= n && t == s) || (s.length > n && t == s.take(n)+"...") }
    import Prop.BooleanOperators property("truncate") = Prop.forAll { (s: String, n: Int) => (n >= 0) ==> { val t = StringUtils.truncate(s, n) (s.length <= n && t == s) || (s.length > n && t == s.take(n)+"...") } }
    public static String truncate(String s, int n) { if(n < 0) return ""; else if(s.length() <= n) return s; else return s.substring(0, n) + "..."; }
    property("truncate") = Prop.forAll { (s: String, n: Int) => val t = StringUtils.truncate(s, n) if(n < 0) t == "" else (s.length <= n && t == s) || (s.length > n && t == s.take(n)+"...") }
    $ scala -cp .:scalacheck.jar StringUtilProps + StringUtils.truncate: OK, passed 100 tests. ! StringUtils.tokenize: Falsified after 3 passed tests. > ARG_0: List("") > ARG_0_ORIGINAL: List("", "") > ARG_1: 9 + StringUtils.contains: OK, passed 100 tests.
    property("tokenize") = { import Prop.AnyOperators Prop.forAll(listOf(alphaStr), numChar) { (ts, d) => val str = ts.mkString(d.toString) StringUtils.tokenize(str, d).toList ?= ts } }
    $ scala -cp .:scalacheck.jar StringUtilProps + StringUtils.truncate: OK, passed 100 tests. ! StringUtils.tokenize: Falsified after 3 passed tests. > Labels of failing property: Expected List("") but got List() > ARG_0: List("") > ARG_0_ORIGINAL: List("", "E", "zd") > ARG_1: 4 + StringUtils.contains: OK, passed 100 tests.

    2.4 Conclusion

    For more information about ScalaCheck: The Definitive Guide, please visit:

    http://www.artima.com/shop/scalacheck

    and:

    http://booksites.artima.com/scalacheck

    Copyright © 2013-2014 Artima, Inc. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.