멀티 모듈 구성

의존성

요구사항

쿠폰 발급 로직

@Entity
public class Coupon {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Long userId;

    public Coupon() {

    }

    public Coupon(Long userId) {
        this.userId = userId;
    }

    public Long getId() {
        return id;
    }
}

public interface CouponRepository extends JpaRepository<Coupon, Long> {
}

@Service
public class ApplyService {
    private final CouponRepository couponRepository;

    public ApplyService(CouponRepository couponRepository) {
        this.couponRepository = couponRepository;
    }

    @Transactional
    public void apply(Long userId) {
        long count = couponRepository.count();
        if (count > 100) {
            return;
        }
        couponRepository.save(new Coupon(userId));
    }
}

@SpringBootTest
class ApplyServiceTest {

    @Autowired private ApplyService applyService;
    @Autowired private CouponRepository couponRepository;

    @Test
    public void 한번만_응모() {
        applyService.apply(1L);
        long count = couponRepository.count();
        assertThat(count).isEqualTo(1);
    }

}