Last active
September 2, 2016 09:18
-
-
Save lilobase/dd925786f34b03cc0eddd26c0caa1a1a to your computer and use it in GitHub Desktop.
Property Based Testing Java with Quickcheck, Code made for the PBT session at SoCraTes Soltau 2016
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.tngtech.java.junit.dataprovider.*; | |
import org.junit.*; | |
import org.junit.runner.*; | |
import static org.assertj.core.api.Assertions.assertThat; | |
@RunWith(DataProviderRunner.class) | |
public class FizzBuzzParametrizedTest { | |
@DataProvider | |
public static Object[][] times3() { | |
return new Object[][]{{3},{6}}; | |
} | |
@Test | |
@UseDataProvider("times3") | |
public void it_returns_fizz_when_times_3(int n) { | |
assertThat(FizzBuzzTDD.fizzBuzz(n)).isEqualTo("Fizz"); | |
} | |
@DataProvider | |
public static Object[][] times5() { | |
return new Object[][]{{5},{10}}; | |
} | |
@Test | |
@UseDataProvider("times5") | |
public void it_returns_buzz_when_times_5(int n) { | |
assertThat(FizzBuzzTDD.fizzBuzz(n)).isEqualTo("Buzz"); | |
} | |
@DataProvider | |
public static Object[][] times15() { | |
return new Object[][]{{15},{30}}; | |
} | |
@Test | |
@UseDataProvider("times15") | |
public void it_returns_fizzbuzz_when_times_15(int n) { | |
assertThat(FizzBuzzTDD.fizzBuzz(n)).isEqualTo("FizzBuzz"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.pholser.junit.quickcheck.*; | |
import com.pholser.junit.quickcheck.runner.*; | |
import org.junit.runner.*; | |
import static org.assertj.core.api.Java6Assertions.*; | |
import static org.hamcrest.core.IsEqual.*; | |
import static org.junit.Assume.*; | |
@RunWith(JUnitQuickcheck.class) | |
public class FizzBuzzPropertiesTest { | |
@Property | |
public void multiple_of_three_contains_fizz(int n) { | |
assumeThat(n%3, equalTo(0)); | |
assertThat(FizzBuzzTDD.fizzBuzz(n)).contains("Fizz"); | |
} | |
@Property | |
public void multiple_of_five_contains_buzz(int n) { | |
assumeThat(n%5, equalTo(0)); | |
assertThat(FizzBuzzTDD.fizzBuzz(n)).contains("Buzz"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FizzBuzzTDD { | |
public static String fizzBuzz(int i) { | |
if(i%15 == 0) return "FizzBuzz"; | |
if(i%3 == 0) return "Fizz"; | |
if(i%5 == 0) return "Buzz"; | |
return String.valueOf(i); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | |
public class FizzBuzzTDDTest { | |
@org.junit.Test | |
public void it_returns_1_for_1() { | |
assertThat(FizzBuzzTDD.fizzBuzz(1)).isEqualTo("1"); | |
} | |
@Test | |
public void it_returns_2_for_2() { | |
assertThat(FizzBuzzTDD.fizzBuzz(2)).isEqualTo("2"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment