我正在尝试为我的SignIn Activity编写测试,其中我正在使用AsyncTask.
public class SignInActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
.........
new AsyncSignButton().execute();
}
class AsyncSignButton extends AsyncTask<Void, Void, Integer>{
.......
}
Run Code Online (Sandbox Code Playgroud)
为了测试,我尝试过使用IdlingResource,但我不明白如何将它与AsyncTask一起使用,而不是像示例中那样使用WebView,只是简单的AsyncTask.
这是我的测试代码:
public class Test extends ActivityInstrumentationTestCase2<SplashActivity> {
private SplashActivity mActivity;
public Test(){
super(SplashActivity.class);
}
public Test(Class<SplashActivity> activityClass) {
super(activityClass);
}
@Override
protected void setUp() throws Exception{
super.setUp();
mActivity = getActivity();
//how call it?
}
@LargeTest
public void testList() throws InterruptedException {
//wait AsyncTask before call
onView(withId(R.id.action_bar_accept_button)).perform(click());
}
public final class AsyncIdlingResource implements IdlingResource { …Run Code Online (Sandbox Code Playgroud) 如何使用枚举将JSON解析为模型?
这是我的枚举类:
enum class VehicleEnumEntity(val value: String) {
CAR("vehicle"),
MOTORCYCLE("motorcycle"),
VAN("van"),
MOTORHOME("motorhome"),
OTHER("other")
}
Run Code Online (Sandbox Code Playgroud)
我需要解析type一个枚举
"vehicle":{"data":{"type":"vehicle","id":"F9dubDYLYN"}}
编辑
我已经尝试过标准方法,只需将我的枚举传递给POJO,它总是为空
我进行了浓缩咖啡测试,希望应用程序在测试完成后保持准确的活动。我需要继续与应用程序交互。
@SmallTest
public void testOpenNavigationDrawer()
{
Espresso.onView(ViewMatchers.withId(com.eleks.espresso.example.app.R.id.content_frame)).perform(ViewActions.swipeRight());
ListView lvDrawerMenu = (ListView) getActivity().findViewById(com.eleks.espresso.example.app.R.id.lvDrawerMenu);
Preconditions.checkNotNull(lvDrawerMenu, "lvDrawerMenu is null");
final int count = lvDrawerMenu.getAdapter().getCount();
Preconditions.checkPositionIndex(2, count, "No 1 index " + count + " size");
Object obj = lvDrawerMenu.getItemAtPosition(2);
Espresso.onView(Matchers.allOf(ViewMatchers.withId(com.eleks.espresso.example.app.R.id.tvItem), ViewMatchers.hasSibling(ViewMatchers.withText(obj.toString())))).perform(ViewActions.click());
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?谢谢你!
如何在Espresso测试中打开标签?我试着做Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).perform(ViewActions.click());,但那不起作用.在该代码中,我打开了此选项卡的布局.有XML文件:
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/practice_tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/practice_settings_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/practice_results_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)
我应该使用什么ID来打开标签?
logcat中的错误:
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "LinearLayout{id=2131296384, res-name=practice_results_tab, visibility=GONE, width=0, height=0, has-focus=false, …Run Code Online (Sandbox Code Playgroud) 我用Retrofit 2,okhttp和okhttp:logging-interceptor创建了项目.
private static APIInterface apiInterface;
private static RestClient restClient;
private static HttpLoggingInterceptor interceptor;
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Accept", "application/json")
.header("X-Parse-Application-Id", Constants.PARSE_APP_ID)
.header("X-Parse-REST-API-Key", Constants.PARSE_REST_API)
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
interceptor = new HttpLoggingInterceptor(); // got crash here
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient.interceptors().add(interceptor);
Run Code Online (Sandbox Code Playgroud)
这是我的踪迹:
java.lang.VerifyError: com/squareup/okhttp/logging/HttpLoggingInterceptor
at com.rocker.rest.RestClient.setupRestClient(RestClient.java:62)
at com.rocker.rest.RestClient.<clinit>(RestClient.java:39)
at com.rocker.fragment.HistoryFragment.onCreateView(HistoryFragment.java:38)
Run Code Online (Sandbox Code Playgroud)
我没有使用平方的okio!
首先,我通过Repository类管理realm实例:
public class RealmRepository {
private Lock lock;
protected RealmRepository() {
lock = new ReentrantLock();
}
public <T> T execute(Executor<T> executor) {
Realm realm = null;
try {
lock.lock();
realm = Realm.getDefaultInstance();
return executor.execute(realm);
} finally {
if (realm != null && !realm.isClosed()) {
realm.close();
}
lock.unlock();
}
}
public interface Executor<T> {
T execute(Realm realm);
}
}
Run Code Online (Sandbox Code Playgroud)
而且这个RealmRepository扩展的唯一类就是我的控制器类.问题是当我第一次执行我的片段中的执行方法时,我得到了:
java.lang.IllegalStateException:此Realm实例已被关闭,使其无法使用.
但是在这个错误之后如果重载片段一切正常.在此第一个片段之前,执行方法从Services类调用并且运行良好.例如:即使首先执行它,此方法也能完美运行:
public Observable<ModuleRealm> getModule(String moduleTitle) {
return execute(realm -> realm.where(ModuleRealm.class)
.equalTo("code", moduleTitle)
.findAllAsync()
.asObservable()
.first()
.map(RealmResults::first));
}
Run Code Online (Sandbox Code Playgroud)
但是这个抛出异常:
public Observable<List<ProductCategoryRealm>> …Run Code Online (Sandbox Code Playgroud) 我试图用测试来覆盖演示者。演示者用例作为数据源。用例的 Execute 方法具有以下结构:fun execute(params: TInput? = null, onNext: (TOutput) -> Unit, onError: (Throwable) -> Unit).
我的问题是如何从 onNext 和 onError 模拟发射?
这是我的 PresenterTest:
class AssignmentsPresenterTest : UnitTest() {
@Mock lateinit var dataRepository: IDataRepository
@Mock lateinit var viewState: AssignmentsView
lateinit var execute: ExecuteUseCase
lateinit var presenter: AssignmentsPresenter
@Before fun setUp() {
execute = ExecuteUseCase(dataRepository)
presenter = AssignmentsPresenter()
presenter.attachView(viewState)
}
@Test fun `should return list of assignments`() {
given {
execute.execute(anyVararg(), { }, { })
}.willReturn(anyVararg())
}
}
Run Code Online (Sandbox Code Playgroud)
演示者代码:
@InjectViewState
class …Run Code Online (Sandbox Code Playgroud)