|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.function.Function; |
| 14 | +import java.util.stream.IntStream; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.DynamicTest; |
| 18 | +import org.junit.jupiter.api.TestFactory; |
| 19 | +import org.junit.jupiter.api.function.ThrowingConsumer; |
| 20 | + |
| 21 | +public class DynamicTestsExample { |
| 22 | + |
| 23 | + @TestFactory |
| 24 | + Collection<DynamicTest> dynamicTestsWithCollection() { |
| 25 | + return Arrays.asList( |
| 26 | + DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))), |
| 27 | + DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2)))); |
| 28 | + } |
| 29 | + |
| 30 | + @TestFactory |
| 31 | + Iterable<DynamicTest> dynamicTestsWithIterable() { |
| 32 | + return Arrays.asList( |
| 33 | + DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))), |
| 34 | + DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2)))); |
| 35 | + } |
| 36 | + |
| 37 | + @TestFactory |
| 38 | + Iterator<DynamicTest> dynamicTestsWithIterator() { |
| 39 | + return Arrays.asList( |
| 40 | + DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))), |
| 41 | + DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2)))) |
| 42 | + .iterator(); |
| 43 | + } |
| 44 | + |
| 45 | + @TestFactory |
| 46 | + Stream<DynamicTest> dynamicTestsFromIntStream() { |
| 47 | + return IntStream.iterate(0, n -> n + 2).limit(10).mapToObj( |
| 48 | + n -> DynamicTest.dynamicTest("test" + n, () -> assertTrue(n % 2 == 0))); |
| 49 | + } |
| 50 | + |
| 51 | + @TestFactory |
| 52 | + Stream<DynamicTest> dynamicTestsFromStream() { |
| 53 | + |
| 54 | + // sample input and output |
| 55 | + List<String> inputList = |
| 56 | + new ArrayList<>(Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com")); |
| 57 | + List<String> outputList = |
| 58 | + new ArrayList<>(Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156")); |
| 59 | + |
| 60 | + // input generator that generates inputs using inputList |
| 61 | + Iterator<String> inputGenerator = new Iterator<String>() { |
| 62 | + |
| 63 | + String current; |
| 64 | + int size = inputList.size(); |
| 65 | + int index = 0; |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean hasNext() { |
| 69 | + if(index == size) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + current = inputList.get(index); |
| 73 | + index++; |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String next() { |
| 79 | + return current; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // a display name generator that creates a different name based on the input |
| 84 | + Function<String, String> displayNameGenerator = (input) -> "Resolving: " + input; |
| 85 | + |
| 86 | + // the test executor, which actually has the logic of how to execute the test case |
| 87 | + DomainNameResolver resolver = new DomainNameResolver(); |
| 88 | + ThrowingConsumer<String> testExecutor = (input) -> { |
| 89 | + int id = inputList.indexOf(input); |
| 90 | + assertEquals(outputList.get(id), resolver.resolveDomain(input)); |
| 91 | + }; |
| 92 | + |
| 93 | + // combine everything and return a Stream of DynamicTest |
| 94 | + return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor); |
| 95 | + } |
| 96 | + |
| 97 | + @TestFactory |
| 98 | + Stream<DynamicTest> dynamicTestsFromStreamInJava8() { |
| 99 | + |
| 100 | + DomainNameResolver resolver = new DomainNameResolver(); |
| 101 | + |
| 102 | + List<String> inputList = |
| 103 | + new ArrayList<>(Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com")); |
| 104 | + List<String> outputList = |
| 105 | + new ArrayList<>(Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156")); |
| 106 | + |
| 107 | + return inputList.stream().map(dom -> DynamicTest.dynamicTest("Resolving: " + dom, () -> { |
| 108 | + int id = inputList.indexOf(dom); |
| 109 | + assertEquals(outputList.get(id), resolver.resolveDomain(dom)); |
| 110 | + })); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + class DomainNameResolver { |
| 115 | + |
| 116 | + private Map<String, String> ipByDomainName = new HashMap<>(); |
| 117 | + |
| 118 | + DomainNameResolver() { |
| 119 | + this.ipByDomainName.put("www.somedomain.com", "154.174.10.56"); |
| 120 | + this.ipByDomainName.put("www.anotherdomain.com", "211.152.104.132"); |
| 121 | + this.ipByDomainName.put("www.yetanotherdomain.com", "178.144.120.156"); |
| 122 | + } |
| 123 | + |
| 124 | + public String resolveDomain(String domainName) { |
| 125 | + return ipByDomainName.get(domainName); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments