├── .gitignore
├── src
├── test
│ └── java
│ │ └── com
│ │ └── rabriel
│ │ ├── FutureTest.java
│ │ ├── HappyFlowTest.java
│ │ └── ExceptionsAreThrownTest.java
└── main
│ └── java
│ └── com
│ └── rabriel
│ └── GivenWhenThen.java
├── given-when-then.iml
├── LICENSE
├── README.md
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | /.idea
3 |
--------------------------------------------------------------------------------
/src/test/java/com/rabriel/FutureTest.java:
--------------------------------------------------------------------------------
1 | package com.rabriel;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.concurrent.ExecutorService;
6 | import java.util.concurrent.Executors;
7 |
8 | import static com.rabriel.GivenWhenThen.given;
9 |
10 | /**
11 | * Created by Rabriel on 12/4/2016.
12 | */
13 | public class FutureTest {
14 | private ExecutorService executor = Executors.newSingleThreadExecutor();
15 | @Test
16 | public void basicGivenFutureFlowTest(){
17 | GivenWhenThen.given(executor.submit(() -> 1))
18 | .when("multiplying by 2", givenValue -> 2*givenValue)
19 | .then("value should be even", whenValue -> whenValue%2 == 0);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/given-when-then.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Gabriiel Deliu
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # given-when-then
2 | basic given when then test helper [Java 8]
3 |
4 | # install
5 | Add the following maven dependecy (Sample for version 0.1.2):
6 | ```xml
7 |
8 | com.rabriel
9 | given-when-then
10 | 0.1.2
11 |
12 | ```
13 |
14 | # how to use
15 | ```java
16 | import static com.rabriel.GivenWhenThen.given;
17 |
18 | @Test
19 | public void basicFlowTest() {
20 | given(1)
21 | .when("multiplying by 2", givenValue -> 2 * givenValue)
22 | .then("value should be even", whenValue -> whenValue % 2 == 0);
23 | }
24 |
25 | @Test
26 | public void basicMultiWhenFlowTest() {
27 | given(1)
28 | .when("multiplying by 2", givenValue -> 2 * givenValue)
29 | .when("High squared", givenValue -> 2 * givenValue)
30 | .then("value should be a square", whenValue -> whenValue == Math.pow(Math.sqrt(whenValue), 2));
31 | }
32 |
33 | @Test
34 | public void multiTypeFlowTest() {
35 | LocalDateTime localDateTime = LocalDateTime.now();
36 | DayOfWeek expectedDay = localDateTime.getDayOfWeek();
37 |
38 | given(localDateTime)
39 | .when("retrieving current day", date -> date.getDayOfWeek())
40 | .then("days should match", day -> expectedDay == day);
41 | }
42 |
43 | @Test
44 | public void assertFlowTest() {
45 | Integer primeNumber = 17;
46 | given("a prime number", primeNumber)
47 | .when("finding dividers naively", number -> IntStream.rangeClosed(1, number)
48 | .boxed().filter(value -> number % value == 0).collect(Collectors.toList()))
49 | .then(dividers -> {
50 | assertEquals("should have two dividers", 2, dividers.size());
51 | assertEquals("first divider should be 1", 1, (int) dividers.get(0));
52 | assertEquals(String.format("first divider should be %d", primeNumber), primeNumber, dividers.get(1));
53 | });
54 | }
55 |
56 | @Test
57 | public void basicGivenFutureFlowTest() {
58 | given(executor.submit(() -> 1))
59 | .when("multiplying by 2", givenValue -> 2 * givenValue)
60 | .then("value should be even", whenValue -> whenValue % 2 == 0);
61 | }
62 | ```
63 | # see also
64 | [A Functional Approach to Given When Then Using Java 8](https://dzone.com/articles/a-new-approach-to-given-when-then)
65 |
66 | [Javascript version: given-when-then-js](https://github.com/gabriel-deliu/given-when-then-js)
67 |
68 | # thanks to
69 | Strategy & Technology (http://www.s-and-t.com/) for allowing the project to start.
70 |
--------------------------------------------------------------------------------
/src/main/java/com/rabriel/GivenWhenThen.java:
--------------------------------------------------------------------------------
1 | package com.rabriel;
2 |
3 | import java.util.concurrent.ExecutionException;
4 | import java.util.concurrent.Future;
5 | import java.util.function.Consumer;
6 | import java.util.function.Function;
7 | import java.util.function.Predicate;
8 |
9 | /**
10 | * Created by gabriel on 19/10/2016.
11 | * Class uses to provide Given When Then test flow methods
12 | */
13 | public class GivenWhenThen {
14 |
15 | public static final String WHEN_FUNCTION_FAILED = "When function failed.";
16 | public static final String FAILED = " - failed";
17 | public static final String THEN_NOT_SATISFIED = "Then not satisfied.";
18 | public static final String THEN_FUNCTION_FAILED = "Then function failed.";
19 | public static final String FUTURE_FAILED = "Given future failed.";
20 | private T received;
21 |
22 | private GivenWhenThen(T received){
23 | this.received = received;
24 | }
25 |
26 | private GivenWhenThen(Future received) {
27 |
28 | if(received == null){
29 | this.received = null;
30 | return;
31 | }
32 |
33 | try {
34 | this.received = received.get();
35 | } catch (InterruptedException | ExecutionException e) {
36 | throw new RuntimeException(FUTURE_FAILED, e);
37 | }
38 | }
39 |
40 | public GivenWhenThen when(Function whenFunction){
41 | return when(null, whenFunction);
42 | }
43 |
44 | public GivenWhenThen when(String message, Function whenFunction){
45 |
46 | try {
47 | return new GivenWhenThen<>(whenFunction.apply(received));
48 | }
49 | catch (Exception ex){
50 | throw new RuntimeException(message == null ? WHEN_FUNCTION_FAILED : message + FAILED, ex);
51 | }
52 | }
53 |
54 | public void then(Predicate thenFunction){
55 | then(null, thenFunction);
56 | }
57 |
58 | public void then(String failMessage, Predicate thenFunction){
59 | boolean testResult = thenFunction.test(received);
60 | if(!testResult) {
61 | throw new RuntimeException(failMessage == null ? THEN_NOT_SATISFIED : failMessage);
62 | }
63 | }
64 |
65 | public void then(Consumer thenFunction){
66 | then(null, thenFunction);
67 | }
68 |
69 | public void then(String message, Consumer thenFunction){
70 | try {
71 | thenFunction.accept(received);
72 | }
73 | catch (Exception ex){
74 | throw new RuntimeException(message == null ? THEN_FUNCTION_FAILED : message + FAILED, ex);
75 | }
76 | }
77 |
78 | public static GivenWhenThen given(E receivedObj){
79 | return given(null, receivedObj);
80 | }
81 |
82 | public static GivenWhenThen given(String message, E receivedObj){
83 | return new GivenWhenThen<>(receivedObj);
84 | }
85 |
86 | public static GivenWhenThen given(Future receivedObj){
87 | return given(null, receivedObj);
88 | }
89 |
90 | public static GivenWhenThen given(String message, Future receivedObj){
91 | return new GivenWhenThen<>(receivedObj);
92 | }
93 |
94 | }
--------------------------------------------------------------------------------
/src/test/java/com/rabriel/HappyFlowTest.java:
--------------------------------------------------------------------------------
1 | package com.rabriel;
2 |
3 | import org.junit.Test;
4 |
5 | import java.time.DayOfWeek;
6 | import java.time.LocalDateTime;
7 | import java.util.concurrent.ExecutionException;
8 | import java.util.stream.Collectors;
9 | import java.util.stream.IntStream;
10 |
11 | import static com.rabriel.GivenWhenThen.given;
12 | import static junit.framework.TestCase.assertEquals;
13 | /**
14 | * Created by Rabriel on 11/27/2016.
15 | */
16 | public class HappyFlowTest {
17 |
18 | @Test
19 | public void testPassedValueisMatchedGivenWhen(){
20 |
21 | int passedIntValue = 22;
22 | String passedStringValue = "Test Value";
23 |
24 | GivenWhenThen.given(passedIntValue).when("testing passed int value in 'when'",
25 | receivedValue -> { assertEquals("given passed int value should match when received value",
26 | passedIntValue, (int) receivedValue); return true; });
27 | GivenWhenThen.given(passedStringValue).when("testing passed string value in 'when'",
28 | receivedValue -> { assertEquals("given passed string value should match when received value",
29 | passedStringValue, receivedValue); return true; });
30 |
31 | }
32 |
33 | @Test
34 | public void testPassedValueIsMatchedWhenThen() throws ExecutionException, InterruptedException {
35 | String whenValue = "Test Value";
36 |
37 | GivenWhenThen.given(null)
38 | .when(whenReceivedValue -> whenValue)
39 | .then("testing passed string value in 'then'",thenReceivedValue ->
40 | { assertEquals("when passed value should match then received value",whenValue, thenReceivedValue); });
41 |
42 | }
43 |
44 | @Test
45 | public void basicFlowTest(){
46 | GivenWhenThen.given(1)
47 | .when("multiplying by 2", givenValue -> 2*givenValue)
48 | .then("value should be even", whenValue -> whenValue%2 == 0);
49 | }
50 |
51 | @Test
52 | public void basicMultiWhenFlowTest(){
53 | GivenWhenThen.given(1)
54 | .when("multiplying by 2", givenValue -> 2*givenValue)
55 | .when("High squared", givenValue -> 2*givenValue)
56 | .then("value should be a square", whenValue -> whenValue == Math.pow(Math.sqrt(whenValue),2));
57 | }
58 |
59 | @Test
60 | public void multiTypeFlowTest(){
61 | LocalDateTime localDateTime = LocalDateTime.now();
62 | DayOfWeek expectedDay = localDateTime.getDayOfWeek();
63 |
64 | GivenWhenThen.given(localDateTime)
65 | .when("retrieving current day", LocalDateTime::getDayOfWeek)
66 | .then("days should match", day -> expectedDay == day);
67 | }
68 | @Test
69 | public void assertFlowTest(){
70 | Integer primeNumber = 17;
71 | GivenWhenThen.given("a prime number", primeNumber)
72 | .when("finding dividers naively", number -> IntStream.rangeClosed(1, number)
73 | .boxed().filter(value -> number%value == 0).collect(Collectors.toList()))
74 | .then(dividers -> {
75 | assertEquals("should have two dividers", 2, dividers.size());
76 | assertEquals("first divider should be 1", 1, (int) dividers.get(0));
77 | assertEquals(String.format("first divider should be %d", primeNumber), primeNumber, dividers.get(1));
78 | });
79 | }
80 |
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/src/test/java/com/rabriel/ExceptionsAreThrownTest.java:
--------------------------------------------------------------------------------
1 | package com.rabriel;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.concurrent.ExecutorService;
6 | import java.util.concurrent.Executors;
7 | import java.util.function.Consumer;
8 | import java.util.function.Predicate;
9 |
10 | import static junit.framework.TestCase.assertEquals;
11 |
12 | import static com.rabriel.GivenWhenThen.given;
13 |
14 | /**
15 | * Created by Rabriel on 11/28/2016.
16 | */
17 | public class ExceptionsAreThrownTest {
18 |
19 | public static final String EVERYTHING_WILL_BE_FINE = "everything will be fine";
20 | public static final String BAD_THINGS_HAPPEN = "bad things happen";
21 | private ExecutorService executor = Executors.newSingleThreadExecutor();
22 |
23 | @Test(expected=RuntimeException.class)
24 | public void testExceptionIsThrownFromWhen(){
25 | GivenWhenThen.given(null)
26 | .when(whenReceivedValue -> {throw new NullPointerException(BAD_THINGS_HAPPEN); });
27 | }
28 |
29 | @Test
30 | public void testExceptionMessageFromWhen(){
31 | try{
32 | GivenWhenThen.given(null)
33 | .when(whenReceivedValue -> {throw new NullPointerException(BAD_THINGS_HAPPEN); });
34 | } catch (RuntimeException ex){
35 | assertEquals("Exception message should match.",GivenWhenThen.WHEN_FUNCTION_FAILED, ex.getMessage());
36 | }
37 |
38 | try{
39 | GivenWhenThen.given(null)
40 | .when(EVERYTHING_WILL_BE_FINE, whenReceivedValue -> {throw new NullPointerException("bad things happen"); });
41 | } catch (RuntimeException ex){
42 | assertEquals("Exception message should match.",EVERYTHING_WILL_BE_FINE + GivenWhenThen.FAILED, ex.getMessage());
43 | }
44 |
45 | }
46 |
47 | @Test
48 | public void testExceptionMessageFromThen(){
49 | try{
50 | GivenWhenThen.given(null)
51 | .when(value -> null)
52 | .then((Consumer