为什么要扩展Application课程?
它对我有什么用?
为什么要这么做?
我读过它可以用来声明全局变量,是全部还是其他任何应用程序?
我有一个JUnit测试,我希望等待一段时间,同步.我的JUnit测试看起来像这样:
@Test
public void testExipres(){
SomeCacheObject sco = new SomeCacheObject();
sco.putWithExipration("foo", 1000);
//WAIT FOR 2 SECONDS
assertNull(sco.getIfNotExipred("foo"));
}
Run Code Online (Sandbox Code Playgroud)
我尝试了Thread.currentThread().wait(),但它抛出了IllegalMonitorStateException(如预期的那样).是否有一些技巧,或者我需要一个不同的显示器?
我已经开发了一个使用rxJava改造的Android应用程序,现在我正在尝试使用Mockito设置单元测试,但我不知道如何模拟api响应以创建不做真正的测试电话但有假响应.
例如,我想测试方法syncGenres对我的SplashPresenter工作正常.我的课程如下:
public class SplashPresenterImpl implements SplashPresenter {
private SplashView splashView;
public SplashPresenterImpl(SplashView splashView) {
this.splashView = splashView;
}
@Override
public void syncGenres() {
Api.syncGenres(new Subscriber<List<Genre>>() {
@Override
public void onError(Throwable e) {
if(splashView != null) {
splashView.onError();
}
}
@Override
public void onNext(List<Genre> genres) {
SharedPreferencesUtils.setGenres(genres);
if(splashView != null) {
splashView.navigateToHome();
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
Api类就像:
public class Api {
...
public static Subscription syncGenres(Subscriber<List<Genre>> apiSubscriber) {
final Observable<List<Genre>> call = ApiClient.getService().syncGenres();
return call
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(apiSubscriber);
} …Run Code Online (Sandbox Code Playgroud) 我从活动代码中分离了改进的api调用方法,我想对这些方法进行单元测试,例如:接口:
public interface LoginService {
@GET("/auth")
public void basicLogin(Callback<AuthObject> response);
}
Run Code Online (Sandbox Code Playgroud)
这是执行调用的方法,在主活动中,我通过事件总线获取对象.
public class AuthAPI {
private Bus bus;
LoginService loginService;
public AuthAPI(String username, String password) {
this.bus = BusProvider.getInstance().getBus();
loginService = ServiceGenerator.createService(LoginService.class,
CommonUtils.BASE_URL,
username,
password);
}
public void Login() {
loginService.basicLogin(new Callback<AuthObject>() {
@Override
public void success(AuthObject authObject, Response response) {
bus.post(authObject);
}
@Override
public void failure(RetrofitError error) {
AuthObject authObject = new AuthObject();
authObject.setError(true);
bus.post(authObject);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在这里测试
@RunWith(MockitoJUnitRunner.class)
public class AuthCallTest extends TestCase {
AuthAPI …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Robolectric 和 Mockito 编写单元测试。我有一个改造 observable,我想用模拟响应进行测试。我在这里尝试过类似的链接
但我得到了一个java.lang.NullPointerException. 这是我的代码示例:
private MainActivity mainActivity;
@Mock
private FoursquareCalls mockApi;
@Captor
private ArgumentCaptor<Action1<FoursquareListResponse>> cb;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class);
mainActivity = controller.get();
controller.create();
}
@Test
public void shouldFillAdapterWithReposFromApi() throws Exception {
mockApi.getListFoodTrucks(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
Mockito.verify(mockApi).getListFoodTrucks(Mockito.anyString(), Mockito.anyString(), Mockito
.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())
.subscribe(cb.capture());
SearchFragment searchFragment = (SearchFragment) mainActivity.getFragmentManager().findFragmentById(R.id
.fragment_container);
FoursquareListResponse testRespo = new FoursquareListResponse();
testRespo.foodtruckReponse = new FoodTruckResponseModel();
testRespo.foodtruckReponse.listFoodtruck = new ArrayList<>();
testRespo.foodtruckReponse.listFoodtruck.add(new …Run Code Online (Sandbox Code Playgroud) 我想做一个单元测试验证,如果function1()还是function2()被调用.我之前没有使用过回调,你能告诉我怎么做吗?
public void sendData(HttpService service, Document userData) {
Call<String> call = service.updateDocument(getId(), userData);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
function1(response.code());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
function2();
}
});
}
Run Code Online (Sandbox Code Playgroud)