Head First: Android???????????
?????Github ???????[ 2016/12/9 13:41:40 ] ??????????????? Android
???????±????getModel() ?? getView() ???滹?????e?????????????Junit?????????
?????????????????????Mockito?????????????????Junit & Mockito??Java????????????????????????????????????????JVM??????Mockito????Android??????
???????????CreditCardPresenter д?????????? ???????????????Mockito?????з??? ??
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class CreditCardPresenterTest {
CreditCardPresenter creditCardPresenter;
@Mock
CreditCardContract.View creditCardView;
@Mock
CreditCardContract.Model creditCardModel;
List<CreditCard> creditCards;
@Before
public void setUp() throws Exception {
creditCardPresenter = new CreditCardPresenter();
creditCardPresenter.attachView(creditCardView);
creditCardPresenter.setModel(creditCardModel);
creditCards = new ArrayList<>();
}
public void testGetCreditCards() {
when(creditCardModel.getCreditCards()).thenReturn(Observable.create(new Observable.OnSubscribe<List<CreditCard>>() {
@Override
public void call(Subscriber<? super List<CreditCard>> subscriber) {
subscriber.onNext(creditCards);
subscriber.onCompleted();
}
}));
creditCardPresenter.getCreditCards();
verify(creditCardView).showCreditCards(creditCards);
verify(creditCardView).loadCompleted();
}
public void testGetCreditCardsOnError() {
final RuntimeException exception = new RuntimeException();
when(creditCardModel.getCreditCards()).thenReturn(Observable.create(new Observable.OnSubscribe<List<CreditCard>>() {
@Override
public void call(Subscriber<? super List<CreditCard>> subscriber) {
throw exception;
}
}));
creditCardPresenter.getCreditCards();
verify(creditCardView).showError(exception);
}
}
????????????????????д??????????????
???????????@Mock?????????mock?????????setUp?????????Mockito.mock()??????mock???????????????????????????????@RunWith(MockitoJUnitRunner.class)
????mock??????????????????????void????????????????з?????????????null??int ???????0??boolean???????false?????
?????????????????when(…).thenReturn(…)???mock?????????????????
????when(creditCardModel.getCreditCards()).thenReturn(Observable.create(new Observable.OnSubscribe<List<CreditCard>>() {
????@Override
????public void call(Subscriber<? super List<CreditCard>> subscriber) {
????subscriber.onNext(creditCards);
????subscriber.onCompleted();
????}
????}));
????????????????????????creditCardModel.getCreditCards()???????????
????Observable.create(new Observable.OnSubscribe<List<CreditCard>>() {
????@Override
????public void call(Subscriber<? super List<CreditCard>> subscriber) {
????subscriber.onNext(creditCards);
????subscriber.onCompleted();
????}
????})
?????????verify()??????У??????????????У?
????verify(creditCardView).showCreditCards(creditCards);
?????????????????? creditCardView.showCreditCards(creditCards)?????????????????????creditCards??????????????Ρ????????????????????????????
????verify()???к????????????????????????? verfy(creditCardView?? times(1)).showCreditCards(creditCards); У??????????Σ?times(1) ???????????????У?鷽???????????Ρ????????滻??nerver()??????????????????С?
???????Mockito?????????????????к?????÷??????????????
????Mockito????Щ???в????????????mock?????????final???????????????л???????????? SelfApplication.getContext() ???????????Application??????????????г????????????????????????JVM?????????Application??????????
????????????????Mockito?????PowerMock
???????????Mockito?????????mock?????????final?????????????PowerMock??mock??SelfApplication.getContext()???????????????????Application????
????PowerMockito.mockStatic(SelfApplication.class);
????PowerMockito.when(SelfApplication.getContext()).thenReturn(mock(SelfApplication.class));
????????????????????@PrepareForTest(SelfApplication.class)?? ??????????? @RunWith(PowerMockRunner.class) ?????????mock??
?????????? ????SelfApplication.getContext()???????????mock?????????????????when().thenReturn()?????????????????????
???????????Mockito ?? PowerMock ??????÷??????????????????????????????
????????????????????????????л??????????????????????д??????????????????д??????????????д?????????????????′????????????????????????????????????????????????????????????????ɡ?
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
iOS???????mocha??chai??sinon??istanbul???????????????????????????????????????д?????Java????????7??????Android?????????Robolectric3.0????(?)???Kiwi?????????????????????????????????Python?????????????????????????????????????Controller????????д?????????10???????????????????Angular????????????Component??Directive??Pipe ???ServiceAndroid????????????????????????????????--Mockito??????iOS UnitTest???????Vue?????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????