Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle custom validators with FixtureMonkey and JakartaValidationPlugin? #1125

Open
Volodymyr-Petrunin opened this issue Dec 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@Volodymyr-Petrunin
Copy link

Volodymyr-Petrunin commented Dec 19, 2024

Hi, I’m trying to write some tests using FixtureMonkey, and I’ve encountered an issue. My DTOs have custom validators, and I’m using JakartaValidationPlugin with FixtureMonkey.

Here’s a simplified version of my test setup:

@SpringBootTest
@DisplayName("User Service Tests")
class UserServiceTest {

    private static final FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
            .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
            .plugin(new JakartaValidationPlugin())
            .defaultNotNull(true)
            .build();

    private static ResponseUserDto expectedResponseDto;
    private static RequestUserDto expectedRequestDto;

    @BeforeAll
    static void setUp() {
        expectedResponseDto = fixtureMonkey.giveMeOne(ResponseUserDto.class);
        expectedRequestDto = fixtureMonkey.giveMeOne(RequestUserDto.class);
    }
}

The issue arises because some of my validators depend on a repository for validation logic. For example, I have a custom UniqueEmailValidator.

When I run the tests, I get the following error:

HV000064: Unable to instantiate ConstraintValidator: my.awesome.package.validation.UniqueEmailValidator.
jakarta.validation.ValidationException: HV000064: Unable to instantiate ConstraintValidator: my.awesome.package.validation.UniqueEmailValidator.
    at org.hibernate.validator.internal.util.privilegedactions.NewInstance.run(NewInstance.java:44)

my.awesome.package.validation.UniqueEmailValidator.<init>()
java.lang.NoSuchMethodException: my.awesome.package.validation.UniqueEmailValidator.<init>()

What I’ve Learned So Far

I understand that this error occurs because FixtureMonkey invokes Jakarta Validation when generating the DTOs, and the validator’s constructor requires a UserRepository (via @Autowired), which isn’t initialized in this context.

My Goal

I’d like to disable or bypass specific validations (e.g., UniqueEmailValidator) when generating data with FixtureMonkey. I’m looking for the best way to achieve this while keeping the rest of the validation logic intact.

What’s the recommended approach to handle this situation? Should I configure FixtureMonkey, mock the validator, or is there another solution?

@Volodymyr-Petrunin Volodymyr-Petrunin added the question Further information is requested label Dec 19, 2024
@seongahjo
Copy link
Contributor

@Volodymyr-Petrunin
Hi.

In your case, I recommend using the arbitraryValidator option in FixtureMonkeyBuilder to change the default ArbitraryValidator used for validation.

By default, if you are using the JakartaValidationPlugin, the JakartaArbitraryValidator will use Validation.buildDefaultValidatorFactory() to find the validator. You can make your custom validator that excludes specific validations (UniqueEmailValidator).

OR You can use the validOnly API in ArbitraryBuilder. If you set validOnly to false, it will disable validation.

Let me know if it does not work in either case.

Thank you.

@Volodymyr-Petrunin
Copy link
Author

Thank you for responding so quickly!
I didn’t try creating a custom ArbitraryValidator because validOnly(false) works fine for me.

감사합니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants