Skip to content

Commit f42259a

Browse files
committed
Adds unit testing examples with JUnit and AssertJ
Adds recursive function implementation of showList
1 parent 29cbed5 commit f42259a

File tree

7 files changed

+123
-2
lines changed

7 files changed

+123
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222
hs_err_pid*
2323
.gradle
2424
build/
25+
26+
#intelij
27+
.idea

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ repositories {
2323
// In this section you declare the dependencies for your production and test code
2424
dependencies {
2525
testCompile 'junit:junit:4.12'
26+
testCompile 'org.assertj:assertj-core:3.8.0'
2627
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed May 24 17:28:23 CEST 2017
1+
#Thu Jun 01 17:55:03 CEST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package recursion;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by mwypych on 01.06.17.
7+
*/
8+
public class ListUtility {
9+
10+
public static String showList(Integer[] number) {
11+
if (number.length == 0) {
12+
return "";
13+
}
14+
return head(number) + showList(tail(number));
15+
}
16+
17+
public static <T> T head(T[] list) {
18+
return list[0];
19+
}
20+
21+
public static <T> T[] tail(T[] list) {
22+
return Arrays.copyOfRange(list, 1, list.length);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package unittesting;
2+
3+
/**
4+
* Created by mwypych on 01.06.17.
5+
*/
6+
public class UtilityFactorial {
7+
public static int factorial(int value) {
8+
if (value < 0) {
9+
throw new IllegalArgumentException("value cannot be less than 0");
10+
}
11+
if (value == 0) {
12+
return 1;
13+
}
14+
return value * factorial(value-1);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package unittesting;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
7+
import static unittesting.UtilityFactorial.factorial;
8+
9+
/**
10+
* Created by mwypych on 01.06.17.
11+
*/
12+
public class UtilityFactorialAssertJTest {
13+
@Test
14+
public void factorialOfNegativeThrowsAnException() {
15+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
16+
factorial(-20);
17+
});
18+
}
19+
20+
@Test
21+
public void factorialOfZeroIsOne() {
22+
assertThat(factorial(0)).isEqualTo(1);
23+
}
24+
25+
@Test
26+
public void factorialOfOneIsOne() {
27+
assertThat(factorial(1)).isEqualTo(1);
28+
}
29+
30+
@Test
31+
public void factorialOfNineIs362880() {
32+
assertThat(factorial(9)).isEqualTo(362880);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package unittesting;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.rules.ExpectedException;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static unittesting.UtilityFactorial.factorial;
9+
10+
/**
11+
* Created by mwypych on 01.06.17.
12+
*/
13+
public class UtilityFactorialJUnitTest {
14+
@Rule
15+
public ExpectedException thrown = ExpectedException.none();
16+
17+
@Test(expected = IllegalArgumentException.class)
18+
public void factorialOfNegativeThrowsAnException__1() {
19+
factorial(-20);
20+
}
21+
22+
@Test
23+
public void factorialOfNegativeThrowsAnException__2() {
24+
thrown.expect(IllegalArgumentException.class);
25+
factorial(-20);
26+
}
27+
28+
@Test
29+
public void factorialOfZeroIsOne() {
30+
assertEquals("factorial(0)", 1, factorial(0));
31+
}
32+
33+
@Test
34+
public void factorialOfOneIsOne() {
35+
assertEquals("factorial(1)", 1, factorial(1));
36+
}
37+
38+
@Test
39+
public void factorialOfNineIs362880() {
40+
assertEquals("factorial(9)", 362880, factorial(9));
41+
}
42+
}

0 commit comments

Comments
 (0)