Skip to content

Commit

Permalink
Finalize migration to JUnit 5 (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio authored Oct 2, 2024
1 parent 3cfdb78 commit 93f6300
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 589 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig: https://editorconfig.org/

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

# 2 space indentation for java, xml and yml files
[*.{java,xml,yml,sh}]
indent_style = space
indent_size = 2

# Maven POM code convention
[pom.xml]
max_line_length = 205
19 changes: 8 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Needed to properly bring in the Maven dependencies, see http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html -->
<surefire.useSystemClassLoader>false</surefire.useSystemClassLoader>
<!-- Dependency versions overriding -->
<junit-jupiter.version>5.11.1</junit-jupiter.version>
</properties>

<dependencyManagement>
Expand All @@ -35,12 +37,6 @@
<artifactId>guava</artifactId>
<version>33.3.1-jre</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -70,6 +66,12 @@
<artifactId>compile-testing</artifactId>
<version>0.21.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand All @@ -88,11 +90,6 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ public BaseAssertionGenerator() throws IOException {
* Creates a new <code>{@link BaseAssertionGenerator}</code> with the templates from the given directory.
*
* @param templatesDirectory path where to find templates
* @throws IOException if some template file could not be found or read
*/
public BaseAssertionGenerator(String templatesDirectory) throws IOException {
public BaseAssertionGenerator(String templatesDirectory) {
templateRegistry = DefaultTemplateRegistryProducer.create(templatesDirectory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,62 @@

import org.assertj.assertions.generator.data.cars.Car;
import org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AssertionGeneratorOverrideTemplateTest {
import static org.assertj.assertions.generator.Template.Type.ABSTRACT_ASSERT_CLASS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;

class AssertionGeneratorOverrideTemplateTest {

private BaseAssertionGenerator assertionGenerator;
private ClassToClassDescriptionConverter converter;
private GenerationHandler genHandle;

@Rule
public final GenerationPathHandler genHandle = new GenerationPathHandler(AssertionGeneratorOverrideTemplateTest.class,
Paths.get("src/test/resources"));

@Before
public void before() throws IOException {
assertionGenerator = genHandle.buildAssertionGenerator();
@BeforeEach
void before(@TempDir Path tempDir) throws IOException {
assertionGenerator = new BaseAssertionGenerator();
assertionGenerator.setDirectoryWhereAssertionFilesAreGenerated(tempDir.toFile());
converter = new ClassToClassDescriptionConverter();
genHandle = new GenerationHandler(tempDir, Paths.get("src/test/resources"));
}

@Test(expected = NullPointerException.class)
public void should_fail_if_custom_template_is_null() {
assertionGenerator.register(null);
@Test
void should_fail_if_custom_template_is_null() {
assertThatNullPointerException().isThrownBy(() -> assertionGenerator.register(null));
}

@Test(expected = NullPointerException.class)
public void should_fail_if_custom_template_content_is_null() {
assertionGenerator.register(new Template(Template.Type.ABSTRACT_ASSERT_CLASS, (File) null));
@Test
void should_fail_if_custom_template_content_is_null() {
assertThatNullPointerException().isThrownBy(() -> assertionGenerator.register(new Template(ABSTRACT_ASSERT_CLASS, (File) null)));
}

@Test(expected = RuntimeException.class)
public void should_fail_if_custom_template_content_cant_be_read() {
assertionGenerator.register(new Template(Template.Type.ABSTRACT_ASSERT_CLASS, new File("not_existing.template")));
@Test
void should_fail_if_custom_template_content_cant_be_read() {
assertThatRuntimeException().isThrownBy(() -> assertionGenerator.register(new Template(ABSTRACT_ASSERT_CLASS, new File("not_existing.template"))));
}

@Test
public void should_generate_assertion_with_custom_template() throws IOException {
void should_generate_assertion_with_custom_template() throws IOException {
assertionGenerator.register(new Template(Template.Type.HAS_FOR_WHOLE_NUMBER,
new File("customtemplates" + File.separator,
"custom_has_assertion_template_for_whole_number.txt")));

assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(Car.class));
genHandle.assertGeneratedAssertClass(Car.class, "CarAssert.expected.txt", true);
File expectedFile = genHandle.getResourcesDir().resolve("CarAssert.expected.txt").toAbsolutePath().toFile();
File actualFile = genHandle.fileGeneratedFor(Car.class);
// compile it!
genHandle.compileGeneratedFilesFor(Car.class);

assertThat(actualFile).hasSameTextualContentAs(expectedFile);
}

}
Loading

0 comments on commit 93f6300

Please sign in to comment.