Skip to content

Java 14 records with empty constructors violate beRecords() rule #998

@SoylentBob

Description

@SoylentBob

Given that I have an arch unit test like this:

package org.example;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.tngtech.archunit.core.importer.ClassFileImporter;
import org.junit.jupiter.api.Test;

public class RecordTest
{

  @Test
  public void recordTest() {
    var javaClasses = new ClassFileImporter().importPackages("org.example");

    classes().that()
        .haveNameMatching("(.*)Record")
        .should()
        .beRecords()
        .because("they should be simple data structures")
        .check(javaClasses);
  }
}

And a record like this in my project:

package org.example;

public record EmptyRecord()
{
}

When I run the arch unit test, then a violation is reported, even though EmptyRecord is a valid (but useless) Java 14 record:

Architecture Violation [Priority: MEDIUM] - Rule 'classes that have name matching '(.*)Record' should be records, because they should be simple data structures' was violated (1 times):
Class <org.example.EmptyRecord> is no record in (EmptyRecord.java:0)
java.lang.AssertionError: Architecture Violation [Priority: MEDIUM] - Rule 'classes that have name matching '(.*)Record' should be records, because they should be simple data structures' was violated (1 times):
Class <org.example.EmptyRecord> is no record in (EmptyRecord.java:0)
	at com.tngtech.archunit.lang.ArchRule$Assertions.assertNoViolation(ArchRule.java:94)
	at com.tngtech.archunit.lang.ArchRule$Assertions.check(ArchRule.java:86)
	at com.tngtech.archunit.lang.ArchRule$Factory$SimpleArchRule.check(ArchRule.java:165)
	at org.example.RecordTest.recordTest(RecordTest.java:20)

When the record is extended with a parameter, then the violation is resolved:

package org.example;

public record EmptyRecord(String someValue)
{
}

A full example to reproduce the issue can be found here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions