Skip to content

Commit

Permalink
1.9.0: Rewrite tests from JUnit4 to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Jul 7, 2024
1 parent 338c5e4 commit 7766d49
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 47 deletions.
22 changes: 22 additions & 0 deletions .run/tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="tests" type="JUnit" factoryName="JUnit">
<module name="pdf-test.test" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.codeborne.pdftest.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.codeborne.pdftest" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="moduleWithDependencies" />
</option>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

=== 1.9.0 (released 07.07.2024)
* Add soft assertions for AssertJ
* Rewrite tests from JUnit4 to JUnit5

=== 1.8.1 (released 17.02.2023)
* Add matcher `matchesText(regex)`
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dependencies {
implementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2', transitive: false
implementation group: 'org.assertj', name: 'assertj-core', version: '3.26.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
testImplementation 'org.junit.vintage:junit-vintage-engine:5.10.3'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.3'
}

repositories {
Expand Down
1 change: 1 addition & 0 deletions gradle/tests.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test {
useJUnitPlatform()
include 'com/codeborne/pdftest/**/*'
systemProperties['file.encoding'] = 'UTF-8'
testLogging.showStandardStreams = true
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/codeborne/pdftest/CreatePdfTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codeborne.pdftest;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
Expand All @@ -12,7 +12,6 @@
import java.nio.file.Paths;

import static com.codeborne.pdftest.PDF.containsText;
import static com.codeborne.pdftest.PDF.matchesText;
import static org.hamcrest.MatcherAssert.assertThat;

public class CreatePdfTest {
Expand All @@ -21,7 +20,7 @@ public void fromFile() throws IOException {
File file = new File("src/test/resources/50quickideas.pdf");
assertThat(new PDF(file), containsText("50 Quick Ideas"));
}

@Test
public void fromUrl() throws IOException {
URL url = getClass().getClassLoader().getResource("50quickideas.pdf");
Expand Down
16 changes: 10 additions & 6 deletions src/test/java/com/codeborne/pdftest/InvalidPdfTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.codeborne.pdftest;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

public class InvalidPdfTest {
@Test(expected = NoSuchFileException.class)
public void throws_IOException_ifFailedToReadPdfFile() throws IOException {
new PDF(new File("src/test/resources/invalid-file.pdf"));
@Test
public void throws_IOException_ifFailedToReadPdfFile() {
File missingFile = new File("src/test/resources/invalid-file.pdf");
assertThatThrownBy(() -> new PDF(missingFile))
.isInstanceOf(NoSuchFileException.class)
.hasMessage(missingFile.getAbsolutePath());
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/codeborne/pdftest/MatchRegexTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codeborne.pdftest;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URL;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/codeborne/pdftest/PDFInformationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codeborne.pdftest;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.text.ParseException;
Expand All @@ -10,15 +10,15 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

public class PDFInformationTest {
@Test
public void canGetInformationFromPdf() throws IOException, ParseException {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Tallinn"));

PDF pdf = new PDF(getClass().getClassLoader().getResource("50quickideas.pdf"));
assertThat(pdf.author, equalTo("Gojko Adzic"));
assertThat(pdf.creationDate.getTime(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codeborne.pdftest;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/codeborne/pdftest/SoftAssertionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.codeborne.pdftest;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.opentest4j.MultipleFailuresError;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.containsExactText;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

public class SoftAssertionsTest {
@Test
public void canUseSoftAssertions() throws IOException {
PDF pdf = new PDF(getClass().getClassLoader().getResource("minimal.pdf"));

assertThatThrownBy(() -> {
assertAll("minimal PDF",
() -> assertThat(pdf, containsExactText("one")),
() -> assertThat(pdf, containsExactText("two")),
() -> assertThat(pdf, containsExactText("three")),
() -> assertThat(pdf, containsExactText("four"))
);
})
.isInstanceOf(MultipleFailuresError.class)
.hasMessageStartingWith("minimal PDF (4 failures)")
.isInstanceOfSatisfying(MultipleFailuresError.class, (MultipleFailuresError e) -> {
Assertions.assertThat(e.getFailures()).hasSize(4);
Assertions.assertThat(e.getFailures().get(0)).hasMessageContaining("Expected: a PDF containing \"one\"");
Assertions.assertThat(e.getFailures().get(1)).hasMessageContaining("Expected: a PDF containing \"two\"");
Assertions.assertThat(e.getFailures().get(2)).hasMessageContaining("Expected: a PDF containing \"three\"");
Assertions.assertThat(e.getFailures().get(3)).hasMessageContaining("Expected: a PDF containing \"four\"");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.codeborne.pdftest.assertj;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.codeborne.pdftest.assertj;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.codeborne.pdftest.assertj.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;


public class ContainsTextCaseInsensitiveTest {
@Test
public void canAssertThatPdfContainsTextIgnoringCase() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.codeborne.pdftest.assertj;

import com.codeborne.pdftest.PDF;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.codeborne.pdftest.assertj.Assertions.assertThat;
import static java.util.Objects.requireNonNull;
Expand All @@ -13,7 +13,7 @@ public class ContainsTextTest {
private PDF fiftyIdeasPdf;
private PDF minimalPdf;

@Before
@BeforeEach
public void setUp() throws Exception {
fiftyIdeasPdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf")));
minimalPdf = new PDF(requireNonNull(getClass().getClassLoader().getResource("minimal.pdf")));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.codeborne.pdftest.assertj;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.codeborne.pdftest.assertj;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.opentest4j.MultipleFailuresError;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.codeborne.pdftest.matchers;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.containsExactText;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

public class ContainsExactTextTest {
@Test
Expand Down Expand Up @@ -41,7 +41,7 @@ public void errorDescription() throws IOException {
fail("expected AssertionError");
}
catch (AssertionError expected) {
assertThat(expected.getMessage(),
assertThat(expected.getMessage(),
is("\nExpected: a PDF containing \"Goodbye word\"\n but: was \"Hello World\n\""));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.codeborne.pdftest.matchers;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.containsTextCaseInsensitive;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

public class ContainsTextCaseInsensitiveTest {
@Test
Expand Down
18 changes: 10 additions & 8 deletions src/test/java/com/codeborne/pdftest/matchers/ContainsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

import com.codeborne.pdftest.PDF;
import com.codeborne.pdftest.assertj.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Objects;

import static com.codeborne.pdftest.PDF.containsText;
import static com.codeborne.pdftest.PDF.doesNotContainText;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.Matchers.is;

public class ContainsTextTest {

private PDF fiftyIdeasPdf;
private PDF minimalPdf;

@Before
@BeforeEach
public void setUp() throws Exception {
fiftyIdeasPdf = new PDF(
Objects.requireNonNull(getClass().getClassLoader().getResource("50quickideas.pdf"))
Expand Down Expand Up @@ -78,8 +77,11 @@ public void pdfShouldContainMultipleTexts() {
assertThat(fiftyIdeasPdf, containsText("50", "Quick", "Ideas"));
}

@Test(expected = AssertionError.class)
@Test
public void shouldFailWhenOneTextIsMissing() {
assertThat(fiftyIdeasPdf, containsText("50", "Quick", "Applications"));
assertThatThrownBy(() -> assertThat(fiftyIdeasPdf, containsText("50", "Quick", "Applications")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected: a PDF containing \"50\", \"Quick\", \"Applications\"")
.hasMessageContaining("but: was \"50 Quick Ideas");
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.codeborne.pdftest.matchers;

import com.codeborne.pdftest.PDF;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.codeborne.pdftest.PDF.doesNotContainExactText;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

public class DoesNotContainExactTextTest {
@Test
Expand All @@ -33,7 +33,7 @@ public void errorDescription() throws IOException {
fail("expected AssertionError");
}
catch (AssertionError expected) {
assertThat(expected.getMessage(),
assertThat(expected.getMessage(),
is("\nExpected: a PDF not containing exactly \"Hello World\"\n but: was \"Hello World\n\""));
}
}
Expand Down
Loading

0 comments on commit 7766d49

Please sign in to comment.