d.braun1991
d.braun1991

Reputation: 515

ArchUnit test sensitive variable naming

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'.

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.


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 :)

Upvotes: 3

Views: 812

Answers (1)

Manfred
Manfred

Reputation: 3142

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");

Upvotes: 4

Related Questions