fixture-monkey icon indicating copy to clipboard operation
fixture-monkey copied to clipboard

생성자에 exception이 있을 경우, 객체 생성 방법 문의

Open kjh03160 opened this issue 5 months ago • 1 comments

안녕하세요. 생성자에 특정 필드의 값에 따라 exception을 발생시키는 객체의 생성 방법 문의드립니다.

    public class TestClass {
        private final int field1;
        private final String field2;
        
        public TestClass(int field1, String field2) {
            this.field1 = field1;
            if (field1 > 10 && (field2 == null || field2.isEmpty())) {
                throw new IllegalArgumentException("field2 cannot be null or empty when field1 is greater than 10");
            }
            this.field2 = field2;
        }
    }

위와 같이 생성자에 특정 조건에 따라 exception이 발생하는 경우, 해당 validation을 항상 통과할 수 있도록 하는 객체 생성 설정을 어떻게 할 수 있을까요? (setter는 없는 상황입니다!) fixtureMonkey 설정은 아래와 같이 하였으며, 현재 발생하는 에러 로그는 java.lang.IllegalArgumentException: Failed to generate type "TestClass"입니다

FixtureMonkey.builder()
        .plugin(new JavaxValidationPlugin())
        .objectIntrospector(new FailoverIntrospector(
            Arrays.asList(
                FieldReflectionArbitraryIntrospector.INSTANCE,
                ConstructorPropertiesArbitraryIntrospector.INSTANCE,
                BuilderArbitraryIntrospector.INSTANCE,
                BeanArbitraryIntrospector.INSTANCE
            )
        ))
        .defaultNotNull(true);

kjh03160 avatar Sep 01 '25 08:09 kjh03160

@kjh03160 님 안녕하세요.

죄송합니다, 제가 확인이 늦어 답변이 늦었네요. 해당 제약조건이 TestClass 타입에 항상 적용이 되는 것으로 이해했습니다.

방법은 두 가지가 있을 것 같습니다.

  1. JakartaValidationPlugin 사용 https://naver.github.io/fixture-monkey/v1-1-0-kor/docs/plugins/jakarta-validation-plugin/bean-validation/

  2. FixtureMonkey의 옵션으로 제공하는 register로 생성 값 제한 https://naver.github.io/fixture-monkey/v1-1-0-kor/docs/fixture-monkey-options/advanced-options-for-experts/#%ed%8a%b9%ec%a0%95-%ed%83%80%ec%9e%85%ec%97%90-%eb%8c%80%ed%95%9c-%ec%bb%a4%ec%8a%a4%ed%85%80-%ec%83%9d%ec%84%b1%ea%b8%b0-%eb%93%b1%eb%a1%9d%ed%95%98%ea%b8%b0

감사합니다.

seongahjo avatar Oct 10 '25 07:10 seongahjo