简体   繁体   English

有什么方法可以测试使用JUnit访问ContentResolver的类? 还是我必须使用Robolectric?

[英]Is there any way to test classes that access ContentResolver with JUnit? Or do I have to use Robolectric?

I have a test class like this: 我有一个像这样的测试班:

public class PostRepositoryTest extends AndroidTestCase {

    Context context;

    @Before
    public void setUp() throws Exception {
        context = new Application();
    }

    @Test
    public void testFindAll() {
        PostRepository postRepository = PostRepository.get(context);
        List<Post> all = postRepository.findAll();
        assertEquals(0, all.size());
    }

}

findAll method just uses a ContentResolver to return results from a ContentResolver. findAll方法仅使用ContentResolver从ContentResolver返回结果。

public List<Post> findAll() {
    Cursor c = new PostSelection().query(context.getContentResolver());
    return listFromCursor(c);
}

Before trying this way... I was doing this by using AndroidInstrumentationTestCase2 and launching an activity to check all of those things, but I want to avoid that. 在尝试这种方式之前...我是通过使用AndroidInstrumentationTestCase2并启动一个活动来检查所有这些内容的方式来进行此操作的,但是我想避免这种情况。

I'd like to do this as an unit test. 我想以此作为单元测试。 Is it possible? 可能吗?

It depends whether you want to run this test case on a device or not. 这取决于您是否要在设备上运行此测试用例。 Go for Robolectric if you want to run it on local JVM instead of running on device. 如果要在本地JVM上而不是在设备上运行,请选择Robolectric。 If you want to run on device without AndroidInstrumentationTestCase2, then you can use AndroidJUnitRunner. 如果要在没有AndroidInstrumentationTestCase2的设备上运行,则可以使用AndroidJUnitRunner。 Its based on Junit4 specification. 它基于Junit4规范。 Read the link here: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html 在此处阅读链接: https : //developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

See the code samples for basic idea: https://github.com/googlesamples/android-testing/tree/master/unittesting/BasicUnitAndroidTest 请参阅代码示例以获取基本思想: https : //github.com/googlesamples/android-testing/tree/master/unittesting/BasicUnitAndroidTest

If you setup your test suite according to the above link then you don't need to extend any class or launch an activity. 如果您根据上述链接设置了测试套件,则无需扩展任何类或启动任何活动。 You can get context object using the code: 您可以使用以下代码获取上下文对象:

InstrumentationRegistry.getTargetContext();
or
InstrumentationRegistry.getContext();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JUnit测试两个都具有Java主要方法的类(客户端和服务器)? - How do I use JUnit to test two classes (a client and a server) that both have main methods in Java? 是否有任何扩展 JUnit4 测试类的约定? - Are there any conventions of extending JUnit4 test classes? 我如何使用JUnit在一个Java类中对测试方法进行单元化而又没有在模块中完成其他类? - How do I use JUnit to unit test methods in one Java class, without having completed other classes in the module? 有没有办法在JUnit测试中使用JMS? - Is there a way to use JMS in a JUnit test? 我如何为这样的方法编写 Junit 测试,对于上下文,我已经包含了一个当前测试 - How do I write Junit Test for method like this, for context I have included a current test I have 跨所有 Junit 测试类的使用方法 - Use method across all Junit Test classes 如何制作一个基于测试类列表有条件地运行测试的JUnit测试套件? - How do I make a JUnit Test Suite that runs tests conditionally based on a list of Test Classes? 如何使用JUnit测试ArrayList的getter和setter方法? - How do I use JUnit to test the getter and setter methods of an ArrayList? Java jUnit:在任何测试类之前运行的测试套件代码 - Java jUnit: Test suite code to run before any test classes 如果我没有实现Junit测试用例,可以在Junit中使用Assert类吗? - Can I use Assert class in Junit if I am not implementing Junit test cases and how to do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM