2

Setup:

        <java.version>17</java.version>
        <spring-boot.version>2.7.0</spring-boot.version>

        <junit-jupiter-engine.version>5.8.2</junit-jupiter-engine.version>
        <mockito-junit-jupiter.version>4.6.1</mockito-junit-jupiter.version>

        <archunit.version>1.0.0-rc1</archunit.version>

In my recent Java 17-Project Arch Unit Tests are used.

It works fine so far and I'd like to extend the tests by blacklisting specific variable names.

Target: The question is not about "does it makes sense" - it is about "how to do it".


Sample Examples to get into my mood:

a) Blacklisting an Uuid.class to be named 'uuid'.

  • Developers therefore have to use the allowed name 'id' or think of a more specific name regarding their purpose.

Sample Class:

import java.io.Serializable;
import java.util.UUID;

public record myClassRecordClass(
    UUID id,
    UUID uuid,               <-- forbidden
    UUID pupilId,
    UUID teacherId,
    String className
) implements Serializable {}

b) Blacklisting an 'until now used term' within a business case, as it has to be renamed.

  • Falling back into the 'old habit/naming convention' shall be prohibited.

Nov. 2022: As far as I know classes, fields and members can be checked via ArchUnit, but not variable names.

In case the following PR might cover the issue ... https://github.com/TNG/ArchUnit/issues/768 ... somewhen in the future.

Nevertheless, did I miss a better solution? Is there any other way to tackle the issue?

Best wishes :)

d.braun1991
  • 151
  • 1
  • 13
  • 1
    FYI: [Arch Unit 1.0.0](https://github.com/TNG/ArchUnit/releases/tag/v1.0.0) has been released more than 6 weeks ago! – Manfred Nov 17 '22 at 00:14
  • And note that [ArchUnit#768](https://github.com/TNG/ArchUnit/issues/768) is not a PR, but a just feature request. – Manfred Nov 18 '22 at 07:49

1 Answers1

3

For the specific case of records, the parameter names are also field names, and therefore caught by

@ArchTest
ArchRule no_field_named_uuid = noFields()
    .that().haveRawType(UUID.class)
    .should().haveName("uuid")
    .because("developers have to use the allowed name 'id' or " +
            "think of a more specific name regarding their purpose");
Manfred
  • 2,269
  • 1
  • 5
  • 14