๐Ÿ“Œ [MarketBridge] 06. ๊ฐœ๋ฐœ์ผ์ง€ - ์ ˆ๋Œ€ ์‹ฑ๊ธ€ํ†ค์„ ๊ฐ„๊ณผํ•˜์ง€๋งˆ..!

๋ชจ๊น…ยท2024๋…„ 1์›” 31์ผ
0

๊ฐœ๋ฐœ์ผ์ง€ - MarketBridge

๋ชฉ๋ก ๋ณด๊ธฐ
6/8
post-thumbnail
post-custom-banner

๐Ÿ“– ์ ˆ๋Œ€ ์‹ฑ๊ธ€ํ†ค์„ ๊ฐ„๊ณผํ•˜์ง€๋งˆ..!

05 ๊ฐœ๋ฐœ์ผ์ง€๋ฅผ ๋ณด๊ณ  ์˜ฌ ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฐ๋‹น.

์•ž์—์„œ ๋งํ–ˆ๋‹ค์‹œํ”ผ ์ปค๋งจ๋“œ์™€ ์ฟผ๋ฆฌ๋ฅผ ๋ถ„๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— mock ๊ฐ์ฒด๋ฅผ ๋”ฐ๋กœ๋งŒ๋“ค์—ˆ๋‹ค. ์ž์—ฐ์Šค๋ ˆ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ๋ฅผ ์ฐธ์กฐ ํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์—ˆ๋‹ค.
์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ• ๊นŒ?

โœ๏ธ ์ฒซ ๋ฒˆ์งธ ์ƒ๊ฐ

public class BaseFakeOrderDetailRepository {

    protected Long autoGeneratedId = 0L;
    protected List<OrderDetail> data = new ArrayList<>();

    public void clear() {
        data.clear();
    }
}
public class FakeOrderDetailCommendRepository extends BaseFakeOrderDetailRepository implements OrderDetailCommendRepository {

    @Override
    public void save(OrderDetail orderDetail) {
        if (orderDetail.getId() == null || orderDetail.getId() == 0) {
            ReflectionTestUtils.setField(orderDetail, "id", ++super.autoGeneratedId, Long.class);
            super.data.add(orderDetail);
        } else {
            super.data.removeIf(item -> Objects.equals(item.getId(), orderDetail.getId()));
            super.data.add(orderDetail);
        }
    }
}
public class FakeOrderDetailQueryRepository extends BaseFakeOrderDetailRepository implements OrderDetailQueryRepository {

    @Override
    public List<OrderDetail> findByOrderNoAndProduct_IdIn(String orderNo, List<Long> productIds) {
        return super.data.stream()
                .filter(od -> od.getOrder().getOrderNo().equals(orderNo)
                        && productIds.contains(od.getProduct().getId()))
                .toList();
    }
}

์ด๋Ÿฐ์‹์œผ๋กœ ๋ถ€๋ชจ์˜ ํ•„๋“œ์— ๊ณต์œ ๊ฐ’์„ ๋„ฃ๊ณ  super๋กœ ์ ‘๊ทผํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์—ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ƒ๊ฐ๊ณผ ๋‹ฌ๋ฆฌ save๋ฅผ ํ•˜๋”๋ผ๋„ ์ ˆ~๋Œ€ queryRepository ์ชฝ์—์„œ ์ฐพ์•„ ์˜ฌ ์ˆ˜ ์—†์—ˆ๋‹ค. ์–ด๋””์„ ๊ฐ€ ๋™๊ธฐํ™”๊ฐ€ ์•ˆ๋˜๋Š” ๊ธฐ๋ถ„์ด ๋“ค์—ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์™œ์ง€์™œ์ง€? ๊ณ ๋ฏผ์„ ํ•˜๋˜ ๋„์ค‘ ์ด๊ฑด ๋„ˆ๋ฌด๋‚˜.. ๊ฐ„๋‹จํ•œ ์ž๋ฐ” ๊ธฐ๋ณธ์ด์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค.

๋ฐ”๋กœ ์ƒ์„ฑ์ž ํ˜ธ์ถœ์‹œ ๋ถ€๋ชจ์˜ ์ƒ์„ฑ์ž(super())๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.
์ด๊ฒŒ ์™œ? ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค๋ฉด ์ปค๋งจ๋“œ์—์„œ super()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์งˆ ๊ฒƒ์ด๋‹ค. ์ด์–ด์„œ ์ฟผ๋ฆฌ ์ชฝ์—์„œ super()๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜ ๋งŒ๋“ค์–ด์งˆ ๊ฒƒ์ด๋‹ค. ์ฆ‰ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ Repository์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋‹น์—ฐํžˆ ๋™๊ธฐํ™”๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋‹น์—ฐํ•˜๋‹ค...(์‚ฝ์งˆ ํ•œ์‹œ๊ฐ„์€ ํ•œ๋“ฏ,,์ด๋ž˜์„œ ๊ธฐ์ดˆ๊ฐ€ ์ค‘์š”ํ•˜๊ตฌ๋‚˜ ใ… ใ… )

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ• ๊นŒ?
๋ฐ”๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐ”๋กœ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค..! (์ง„์งœ ๊ธฐ์ดˆ์ธ๋ฐ ๋„ˆ๋ฌด๋„ˆ๋ฌด ๊ฐ„๊ณผํ•ด๋ฒ„๋ ธ๋‹ค..)

โœ๏ธ ๋‘ ๋ฒˆ์งธ ์ƒ๊ฐ

@Getter
public class BaseFakeOrderDetailRepository {

    @Getter
    private static final BaseFakeOrderDetailRepository instance = new BaseFakeOrderDetailRepository();

    private Long autoGeneratedId = 0L;
    private List<OrderDetail> data = new ArrayList<>();

    protected Long increaseId() {
        return ++autoGeneratedId;
    }

    public void clear() {
        data.clear();
    }
}
public class FakeOrderDetailCommendRepository extends BaseFakeOrderDetailRepository implements OrderDetailCommendRepository {

    @Override
    public void save(OrderDetail orderDetail) {
        if (orderDetail.getId() == null || orderDetail.getId() == 0) {
            ReflectionTestUtils.setField(orderDetail, "id", getInstance().increaseId(), Long.class);
            getInstance().getData().add(orderDetail);
        } else {
            getInstance().getData().removeIf(item -> Objects.equals(item.getId(), orderDetail.getId()));
            getInstance().getData().add(orderDetail);
        }
    }
}
public class FakeOrderDetailQueryRepository extends BaseFakeOrderDetailRepository implements OrderDetailQueryRepository {

    @Override
    public List<OrderDetail> findByOrderNoAndProduct_IdIn(String orderNo, List<Long> productIds) {
        return getInstance().getData().stream()
                .filter(od -> od.getOrder().getOrderNo().equals(orderNo)
                        && productIds.contains(od.getProduct().getId()))
                .toList();
    }
}

์•ž์—์„œ ๋งํ–ˆ๋‹ค์‹œํ”ผ ๋ถ€๋ชจ๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค. ์ด๋Ÿฌ๋ฉด ์ž์‹ ์ธ์Šคํ„ด์Šค์—์„œ ๋ชจ๋‘ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ €์žฅํ•˜๊ณ  ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค!!!

์‹ฑ๊ธ€ํ†ค์— ์ฃผ์˜ํ•  ์ ์€ ๋ญ๋Šฌ๋ญ๋‰˜ํ•ด๋„ ๋™์‹œ์„ฑ ๋ฌธ์ œ์ด๋‹ค.
์‹ฑ๊ธ€ํ†ค์—์„œ ์ ˆ~๋Œ€ ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ง€๊ธˆ ์ฝ”๋“œ์—๋Š” ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋Š”๊ฐ€?
์ด๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋งํ•˜๊ณ  ์‹ถ๋‹ค. ๋™์‹œ์— ์ž‘์—…์ด ๋˜์ง€ ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋™์‹œ์„ฑ ๋ฌธ์ œ๋ฅผ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

์‹ค์ œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋• ๋งค์šฐ๋งค์šฐ ์กฐ์‹ฌํ•ด์•ผํ•œ๋‹ค..

ํ”„๋กœ์ ํŠธ๊ฐ€ ๋๋‚˜๊ณ  ๋””์ž์ธ ํŒจํ„ด์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ๋ณธ์ธ ๋ธ”๋กœ๊ทธ์˜ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค..!

profile
๋ฉˆ์ถ”์ง€ ์•Š๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€