java - 使用缓存和 ArrayAdapter 在 Android 应用程序上进行 JUnit 测试

标签 java android junit android-arrayadapter ui-thread

// use case 10b alternate version
// caches a read comment temporarily 
public void testCacheReadComment2() throws Throwable{
    runTestOnUiThread(new Runnable()
    {
        @Override
        public void run(){
            CommentBuilder commentBuilder = new commentBuilder();
            Comment comment = commentBuilder.createTopTestComment();
            //browse button on main screen
            ((Button)activity.findViewById(ca.ualberta.cs.team5geotopics.browseButton)).performClick();
            //the ListView for the custom adapter
            ListView listView = (ListView) activity.findViewById(ca.ualberta.cs.team5geotopics.commentList);
            //the custom adapter on the physical screen
            ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) listView.getAdapter();
            adapter.add(comment);
            adapter.notifyDataSetChanged();

            View view = adapter.getView(adapter.getPosition(comment), null, null);
            ViewAsserts.assertOnScreen(listView, view);
            //this is the button to view the Top Level comment in the list
            ViewAsserts.assertOnScreen(view, (Button) view.viewTopLevelComment);
            ((Button)view.viewTopLevelComment).performClick();

            // is there a way I can get references to the objects
            // already instantiated in the test thread?
            CacheController cc = activity.getCacheController();
            assertTrue(cc.getHistory().contains(comment));

        }
    });
}

我们正在使用测试驱动的开发风格来为我们的学校项目编写代码。在此测试中,我试图证明在用户查看适配器列表中的评论后,该评论会缓存在历史缓存中。我对某些事情有点困惑,我需要一些帮助,因为如果我知道我的测试用例中没有明显的缺陷就太好了。这些是我的问题:

View view = adapter.getView(adapter.getPosition(comment), null, null);

此行是否会返回与存储在数组适配器中的注释关联的 View ?我的 ArrayAdapter 将遵循 holder 模式,我不确定这是否是访问按钮的正确方法,我需要模仿用户查看评论。

CacheController cc = activity.getCacheController();

我有一个 CacheController 对象,它是在我们主要 Activity 中的 onCreate() 方法上实例化的。现在我想引用这个 CacheController 来查看历史记录是否正确更新。我只是制作一个新的 CacheController 并在测试方法中模仿 CacheController 的操作,但我想测试 UIthread< 上我的数据发生了什么。那么,如何在 UI 线程中引用对象?

最佳答案

View view = adapter.getView(adapter.getPosition(comment), null, null);

Will this line return the view that is associated with the comment stored in the array adapter?

我认为它应该可以工作,但我不明白您为什么要访问该 View 。

My ArrayAdapter is going to follow a holder patter and I'm not sure if this is the proper way to get access to the buttons I need to mimic the user viewing the comment.

ArrayAdapter 通常用于 ListView。你应该只是let ListView handle the click capturing and tell you which element was clicked .

So, how do I reference objects in the UI thread?

我现在想到了 2 个解决方案:

1)传递CacheController实例,例如:

public class YourClass {

    private final CacheController cacheController;

    public YourClass(final CacheController cacheController) {
        this.cacheController = cacheController;
    }

    public void testCacheReadComment2() throws Throwable {
        CacheController cc = this.cacheController;
    }
}

2) 单例:使 CacheController 静态并放置一个访问器,例如:

public class CacheController {

    private final CacheController instance = new CacheController();

    public static CacheController getCacheController() {
        return instance;
    }
}

在这两种情况下,您都应该注意潜在的多线程问题,因为您正在生成共享相同 CacheController 实例的新线程。

关于java - 使用缓存和 ArrayAdapter 在 Android 应用程序上进行 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21790856/

相关文章:

Android 不同的屏幕尺寸

linux - 我怎样才能得到封面来创建 cover_db?

java - ExpectedSystemExit 导致错误

java - JOOQ中的fieldIndex是什么

java - JAX-RPC WebService 未在 WSDL 中生成 java 数组

android - 屏幕关闭时保持 UI 线程运行

android - 输入在 Android 模拟器中不起作用

unit-testing - 设置 Mobile JUnit 测试以在 JUnit 下运行

java - 如何对调用 super 的重写方法进行单元测试?

java - Mockito 注入(inject)的 Web 模拟与使用 Spring Web 模拟 api 的不同