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

[pg.503] Ch.12 테스트코드에서 ProductDto의 생성자 누락 #6

Open
zakard114 opened this issue Mar 4, 2024 · 0 comments
Open

Comments

@zakard114
Copy link

zakard114 commented Mar 4, 2024

pg.499 상단부에 명시된 ProductDto의 추후 생성자 생성에 대한 약속이 이행되지않아 pg.503의 코드 ProductDto productDto = new ProductDto("pencil", 300, 20);이 작동되지 않을것이다.

이 실습의 목적은 productDto에 entity를 Dto로, 그리고 Dto를 entity로 변환시키는 메서드를 각각 작성하여 기존에 modelMapper에 의존적인 코드를 대체/리펙터링하는데 있다. 필요할 경우, 깃허브 내의 12-3의 src부분을 참고하자.

  • 업데이트 노트: pg.523부터 리펙터링을 위한 생성자와 toEntity 그리고 toDto생성에 대해 다루므로 참고하자.

핵심코드는 다음과 같다. 이들을 productDto에 추가하자.

public static Product toEntity(ProductDto productDto) {
Product product = new Product(
productDto.getId(),
productDto.getName(),
productDto.getPrice(),
productDto.getAmount()
);

    return product;
}

public static ProductDto toDto(Product product){
    ProductDto productDto = new ProductDto(
            product.getId(),
            product.getName(),
            product.getPrice(),
            product.getAmount()
    );
    return productDto;
}

코드를 살펴보면 product와 productDto를 외부로부터 매개변수로 받아 각각 entity와 dto형태로 바꿔 반환하는 메커니즘을 가지고 있다. 문제는 우리는 아직 각각의 클래스에 생성자를 생성하지 않았으므로 이 코드는 작동하지 않는다는 사실이다. 이제 product와 productDto에 생성자를 각각 생성해보자.

productDto의 생성자는 다음과 같다.

public ProductDto() {
}

public ProductDto(String name, Integer price, Integer amount) {
    this.name = name;
    this.price = price;
    this.amount = amount;
}

public ProductDto(Long id, String name, Integer price, Integer amount) {
    this.id = id;
    this.name = name;
    this.price = price;
    this.amount = amount;
}

이렇게 여러 타입의 매개변수를 각각 다른 갯수로 가진, 같은 이름의 생성자 또는 메서드 형태를 다형성 또는 다형성의 하위개념인 오버로딩이라고 부른다(오버로드는 영어권에서 과적이라는 뜻으로 사용되므로 이 경우 적절한 표현이다).

product에도 생성자를 다음과 같이 생성해주자. (ProductDto에서 Alt+Insert를 누른후, 필요한 부분들을 ctrl+좌클릭하여 각각 마킹하거나 shift를 누른 상태에서 하단 amount에 좌클릭 하여 일괄 마킹한 후 모달창 하단의 OK를 누르면 손쉽게 생성할 수 있다. 물론 수동으로 작성해도 무방하다.)

public Product() {
}

public Product(Long id, String name, Integer price, Integer amount) {
    this.id = id;
    this.name = name;
    this.price = price;
    this.amount = amount;
}

이제 모든 준비가 끝났다. 마지막으로 필요한건 SimpleProductService에서 기존에 사용하던 modelMapper를 주석처리하거나 제거하고 product와 productDto에서 우리가 직접 생성한 toEntity()와 toDto()로 모두 교체해주는 작업이다.

이 부분에 관하여 필요하다면 깃허브 12-3의 SimpleProductService를 참고하자.

이후 pg.503의 테스트 코드를 작성한 후 실행한다.

1

테스트가 통과되었다.

후기: 이를 통해 한편으로 우리는 modelMapper가 얼마나 편리한 도구인지 알게 되었다.

@zakard114 zakard114 changed the title [pg.503] ProductDto의 생성자 누락 [pg.503] Ch.12 테스트코드에서 ProductDto의 생성자 누락 Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant