새로운 할인 정책으로 변경, 개발
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
public class RateDiscountPolicy implements DiscountPolicy { private int discountPercent = 10; //10% 할인
@Override
public int discount(Member member, int price) {
if (member.getGrade() == Grade.VIP) {
return price * discountPercent / 100;
} else {
return 0; }
} }
테스트 코드 작성
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
class RateDiscountPolicyTest {
RateDiscountPolicy discountPolicy = new RateDiscountPolicy();
@Test
@DisplayName("VIP는 10% 할인이 적용되어야 한다")
void vip_o(){
//given
Member member = new Member(1L,"memberVIP", Grade.VIP);
//when
int discount = discountPolicy.discount(member,10000);
//then
assertThat(discount).isEqualTo(1000);
}
@Test
@DisplayName("VIP가 아니면 10% 할인이 적용되지 않아야 한다")
void vip_x(){
//given
Member member = new Member(2L,"memberBASIC", Grade.BASIC);
//when
int discount = discountPolicy.discount(member,10000);
//then
//Assertions.assertThat(discount).isEqualTo(1000);//할인금액은 0이 되야함.
assertThat(discount).isEqualTo(0);//할인금액은 0이 되야함.
}
}
Last updated