在UI线程上测试Android Junit4

在UI线程上测试Android Junit4,第1张

概述我正在使用Junit4语法,通过 android测试支持库( here)为Android编写单元测试.我想测试我的自定义视图.测试包括单击复选框并确保其他位置的值得到正确更新,但是当我尝试运行测试时出现此错误: android.util.AndroidRuntimeException: Animators may only be run on Looper threadsat android.a 我正在使用Junit4语法,通过 android测试支持库( here)为AndroID编写单元测试.我想测试我的自定义视图.测试包括单击复选框并确保其他位置的值得到正确更新,但是当我尝试运行测试时出现此错误:
androID.util.AndroIDRuntimeException: Animators may only be run on Looper threadsat androID.animation.ValueAnimator.start(ValueAnimator.java:1002)at androID.animation.ValueAnimator.start(ValueAnimator.java:1050)at androID.animation.ObjectAnimator.start(ObjectAnimator.java:829)at androID.graphics.drawable.AnimatedStateListDrawable$AnimationDrawableTransition.start(AnimatedStateListDrawable.java:294)at androID.graphics.drawable.AnimatedStateListDrawable.selectTransition(AnimatedStateListDrawable.java:226)at androID.graphics.drawable.AnimatedStateListDrawable.onStateChange(AnimatedStateListDrawable.java:145)at androID.graphics.drawable.Drawable.setState(Drawable.java:599)at androID.Widget.Compoundbutton.drawableStateChanged(Compoundbutton.java:438)at androID.vIEw.VIEw.refreshDrawableState(VIEw.java:16032)at androID.Widget.Compoundbutton.setChecked(Compoundbutton.java:143)at androID.Widget.Compoundbutton.toggle(Compoundbutton.java:113)

我没有定义任何自定义动画,我并不真正关心它们进行此测试,但我认为它是AndroID 5.0材质主题,可以创建一个复选框动画.

我假设抛出异常是因为测试不在UI线程上运行而且动画师无法动画.那么如何在UI线程上运行测试呢?

解决方法 使用InstrumentationRegistry.getInstrumentation().runOnMainSync():
/***    copyright (c) 2008-2015 CommonsWare,LLC    licensed under the Apache license,Version 2.0 (the "license"); you may not    use this file except in compliance with the license. You may obtain a copy    of the license at http://www.apache.org/licenses/liCENSE-2.0. Unless required    by applicable law or agreed to in writing,software distributed under the    license is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS    OF ANY KIND,either express or implIEd. See the license for the specific    language governing permissions and limitations under the license.    From _The Busy Coder's GuIDe to AndroID Development_        http://commonsware.com/AndroID */package com.commonsware.androID.abf.test;import androID.support.test.InstrumentationRegistry;import androID.support.test.runner.AndroIDJUnit4;import androID.test.AndroIDTestCase;import androID.test.UiThreadTest;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import com.commonsware.androID.abf.R;import junit.framework.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;@RunWith(AndroIDJUnit4.class)public class DemoContextTest {  private VIEw fIEld=null;  private VIEw root=null;  @Before  public voID init() {    InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {      @OverrIDe      public voID run() {        LayoutInflater inflater=LayoutInflater            .from(InstrumentationRegistry.getTargetContext());        root=inflater.inflate(R.layout.add,null);      }    });    root.measure(800,480);    root.layout(0,800,480);    fIEld=root.findVIEwByID(R.ID.Title);  }  @Test  public voID exists() {    init();    Assert.assertNotNull(fIEld);  }  @Test  public voID position() {    init();    Assert.assertEquals(0,fIEld.gettop());    Assert.assertEquals(0,fIEld.getleft());  }}

(自this sample project起)

总结

以上是内存溢出为你收集整理的在UI线程上测试Android Junit4全部内容,希望文章能够帮你解决在UI线程上测试Android Junit4所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134914.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存