03 當季限定
挑戰測試不同時間的限制
情境
店家為了善用店面,決定在春夏時間賣冰,秋冬的時候賣薑母鴨,但是依賴了系統時間,如何才能測試非當季的邏輯呢?
評分
- 測試1~12月的情境
問題點
- 解決系統時間的依賴問題
package tw.com.bluemoon.androidtest;
import java.util.Calendar;
/**
* Created by Chris on 2015/7/19.
*/
public class Shop {
private Calendar c;
public void sell() {
c = Calendar.getInstance();
int month = c.get(Calendar.MONTH) + 1;
if(isSpring(month) || isSummer(month)){
System.out.println("賣冰");
}else{
System.out.println("賣薑母鴨");
}
}
//3-5
private boolean isSpring(int month) {
return month > 2 && month < 6;
}
//6-8
private boolean isSummer(int month) {
return month > 5 && month < 9;
}
}
import java.util.Calendar;
/**
* Created by Chris on 2015/7/19.
*/
public class Shop {
private Calendar c;
public void sell() {
c = Calendar.getInstance();
int month = c.get(Calendar.MONTH) + 1;
if(isSpring(month) || isSummer(month)){
System.out.println("賣冰");
}else{
System.out.println("賣薑母鴨");
}
}
//3-5
private boolean isSpring(int month) {
return month > 2 && month < 6;
}
//6-8
private boolean isSummer(int month) {
return month > 5 && month < 9;
}
}