naver / fixture-monkey

Let Fixture Monkey generate test instances including edge cases automatically
https://naver.github.io/fixture-monkey
Apache License 2.0
560 stars 89 forks source link

FieldReflectionArbitraryIntrospector 사용시 Getter, Setter 없이 Fixture 생성 질문드립니다. #961

Closed YunNote closed 4 weeks ago

YunNote commented 6 months ago

Describe your question

생성할 클래스는 인자가 없는 생성자(또는 기본 생성자)와 getter 또는 setter 중 하나를 가져야 합니다. 라고 설명이 되어있는것으로 확인하였습니다.

테스트시 getter, setter가 없이 기본생성자만 있어도 값이 설정이되는것 같습니다.

해당 부분 제가 잘못이해하였는지 궁금합니다.

fixtureMonkey 버전은 1.0.14 사용중입니다.

seongahjo commented 6 months ago

@YunNote 님 안녕하세요. FieldReflectionArbitraryIntrospector는 리플렉션을 사용해 필드를 설정해주는 ArbitraryIntrospector라서 객체는 생성될 수 있으나 getter, setter 가 있어야 필드를 정상적으로 설정해줍니다.

아래 테스트를 통해 확인했을 때도 null로 생성이 됩니다. 혹시 어떤 경우에 기본 생성자만 있을 때 필드도 설정이 되는지 재현 가능한 케이스를 공유해주실 수 있을까요?

public class NoGetterNoSetter {
    private final String string;
    private final String integer;

    public NoGetterNoSetter(String string, String integer) {
        this.string = string;
        this.integer = integer;
    }
}
@Test
    void noGetterNoSetter(){
        FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
            .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE)
            .build();

        NoGetterNoSetter actual = fixtureMonkey.giveMeOne(NoGetterNoSetter.class);

        then(actual).isNull();
    }
YunNote commented 6 months ago

@seongahjo 님 안녕하세요

image

위 사진처럼 코드를 생성하였습니다.

NoGetterNoSetter 클래스처럼 final 및 생성자 추가 후 실행시에는 null로 생성되지 않는것 확인하였습니다.

반드시 final로 사용하였을때문 생성이 안되는것이지 궁금합니다!

seongahjo commented 6 months ago

@YunNote 말씀하신 것처럼 다음과 같이 final이 아닌 필드를 가진 경우에 필드 값을 제어할 수 있는 FieldAccessor를 생성 가능하니까 리플렉션 사용이 가능하겠네요. image

저희 팀 내부에서는 가변 객체를 잘 사용안해서 확인을 못하고 가이드를 잘못드렸군요. 확인 감사합니다! 혹시 괜찮으시면 문서에 수정 부탁드려도 괜찮을까요?

YunNote commented 6 months ago

@seongahjo 님 안녕하세요 먼저 빠르게 확인해주셔서 감사합니다!

해당 FieldReflectionArbitraryIntrospector 설명에 대한 부분 말씀해주시는 것일까요?? 가능하다면 수정해서 PR 요청 따로 드리면 되는지 가이드라인이 궁금합니다! (처음입니다 ..)

seongahjo commented 6 months ago

@YunNote 넵 맞습니다. FieldReflectionArbitraryIntrospector의 설명을 final이 아닌 변수가 있으면 사용 가능하다 정도로 변경하면 어떨까 싶습니다.

아래 참조하시면 어떻게 문서 기여하는지 나와있습니다! https://github.com/naver/fixture-monkey/blob/main/CONTRIBUTING.md#how-to-contribute-to-the-document

문서가 지금 영어/한글가 있어서 두곳에 반영이 되야할 것 같습니다.

YunNote commented 6 months ago

@seongahjo 넵 감사합니다!

작성하여 요청드리겠습니다 ! 감사합니다

seongahjo commented 4 weeks ago

이슈가 해결되어 닫습니다.