|
| 1 | +package com.baeldung.testing.jgotesting; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import static org.hamcrest.Matchers.equalToIgnoringCase; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | +import org.jgotesting.rule.JGoTestRule; |
| 7 | +import static org.jgotesting.Assert.*; // same methods as org.junit.Assert.* |
| 8 | +import static org.jgotesting.Check.*; // ditto, with different names |
| 9 | +import static org.jgotesting.Testing.*; |
| 10 | +import org.jgotesting.Checker; |
| 11 | +import org.junit.Ignore; |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +public class JGoTestingUnitTest { |
| 16 | + @Rule |
| 17 | + public final JGoTestRule test = new JGoTestRule(); |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenComparingIntegers_thenEqual() { |
| 21 | + int anInt = 10; |
| 22 | + |
| 23 | + assertEquals(anInt, 10); |
| 24 | + checkEquals(anInt, 10); |
| 25 | + } |
| 26 | + |
| 27 | + @Ignore |
| 28 | + @Test |
| 29 | + public void whenComparingNumbers_thenLoggedMessage() { |
| 30 | + log("There was something wrong when comparing numbers"); |
| 31 | + |
| 32 | + int anInt = 10; |
| 33 | + int anotherInt = 10; |
| 34 | + |
| 35 | + checkEquals(anInt, 10); |
| 36 | + checkTrue("First number should be bigger", 10 > anotherInt); |
| 37 | + checkSame(anInt, anotherInt); |
| 38 | + } |
| 39 | + |
| 40 | + @Ignore |
| 41 | + @Test |
| 42 | + public void whenComparingNumbers_thenFormattedMessage() { |
| 43 | + int anInt = 10; |
| 44 | + int anotherInt = 10; |
| 45 | + |
| 46 | + logf("There was something wrong when comparing numbers %d and %d", anInt, anotherInt); |
| 47 | + |
| 48 | + checkEquals(anInt, 10); |
| 49 | + checkTrue("First number should be bigger", 10 > anotherInt); |
| 50 | + checkSame(anInt, anotherInt); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenComparingStrings_thenMultipleAssertions() { |
| 55 | + String aString = "This is a string"; |
| 56 | + String anotherString = "This Is A String"; |
| 57 | + |
| 58 | + test.check(aString, equalToIgnoringCase(anotherString)) |
| 59 | + .check(aString.length() == 16) |
| 60 | + .check(aString.startsWith("This")); |
| 61 | + } |
| 62 | + |
| 63 | + @Ignore |
| 64 | + @Test |
| 65 | + public void whenComparingStrings_thenMultipleFailingAssertions() { |
| 66 | + String aString = "the test string"; |
| 67 | + String anotherString = "The Test String"; |
| 68 | + |
| 69 | + checkEquals("Strings are not equal!", aString, anotherString); |
| 70 | + checkTrue("String is longer than one character", aString.length() == 1); |
| 71 | + checkSame("Strings are not the same", aString, anotherString); |
| 72 | + } |
| 73 | + |
| 74 | + @Ignore |
| 75 | + @Test |
| 76 | + public void givenFile_whenDoesnotExists_thenTerminated() throws Exception { |
| 77 | + File aFile = new File("a_dummy_file.txt"); |
| 78 | + terminateIf(aFile.exists(), is(false)); |
| 79 | + |
| 80 | + // This doesn't get executed |
| 81 | + checkEquals(aFile.getName(), "a_dummy_file.txt"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void givenChecker_whenComparingStrings_thenEqual() throws Exception { |
| 86 | + Checker<String> aChecker = s -> s.matches("\\d+"); |
| 87 | + |
| 88 | + String aString = "1235"; |
| 89 | + test.check(aString, aChecker); |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
0 commit comments