简体   繁体   English

如何从我的应用程序中的 androidTest(插桩测试)测试类访问资源?

[英]How do I access resources from my application inside of an androidTest (instrumented test) test class?

Please forgive me if I've completely misunderstood the time and place in which I'm supposed to use instrumented tests.如果我完全误解了我应该使用仪器测试的时间和地点,请原谅我。

I am currently in the process of writing the CategoryButtonInstrumentedTest class for the CategoryButton class I wrote prior.我目前正在为我之前编写的 CategoryButton 类编写 CategoryButtonInstrumentedTest 类。

CategoryButtonInstrumentatedTest.java CategoryButtonInstrumentatedTest.java

public class CategoryButtonInstrumentedTest {

    private Context context;
    private CategoryButton button;
    private UnscrollableListView listView;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        button = (CategoryButton) ((Activity) context).findViewById(R.id.button);
        button.connect(listView);
    }

    @Test
    public void onClickTest() {
        boolean start = listView.isMaximized();
        button.performClick();
        boolean end = listView.isMaximized();
        assertNotEquals(start, end);
    }
}

CategoryButton.java类别按钮.java

public class CategoryButton extends androidx.appcompat.widget.AppCompatButton implements View.OnClickListener {

    private static final String TAG = CategoryButton.class.getSimpleName();

    private UnscrollableListView listView;

    public CategoryButton(Context context) {
        super(context);
    }

    public CategoryButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CategoryButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View view) {
        //swap the visibility of the list view
        if (listView != null) {
            if (!listView.isMaximized()) {
                listView.maximize();
            } else {
                listView.minimize();
            }
        } else {
            Log.d(TAG, "Error: please connect an unscrollable list view to the category button");
        }
    }

    public void connect(UnscrollableListView listView) {
        this.listView = listView;
    }
}

The CategoryButton class simply overrides onClick() method from the Button super class. CategoryButton 类简单地覆盖了 Button 超类的 onClick() 方法。 Additionally, CategoryButton can connect to an UnscrollableListView using the connect(UnscrollableListView listView) method.此外,CategoryButton 可以使用 connect(UnscrollableListView listView) 方法连接到 UnscrollableListView。 This allows the CategoryButton to toggle the visibility of the UnscrollableListView it's connected to using the onClick() method.这允许 CategoryButton 使用 onClick() 方法切换它所连接的 UnscrollableListView 的可见性。

How can I create a new CategoryButton inside my CategoryButtonInstrumentationTest class so that I can test its functionality?如何在我的 CategoryButtonInstrumentationTest 类中创建一个新的 CategoryButton 以便我可以测试它的功能?

Here is what I've tried to do.这是我尝试做的。

context = InstrumentationRegistry.getInstrumentation().getTargetContext();
button = (CategoryButton) ((Activity) context).findViewById(R.id.button);

I get the context, which from what I understand contains all the application's classes and resources, and then try to reference the button from my MainActivity with id of button.我得到了上下文,据我所知,它包含所有应用程序的类和资源,然后尝试从我的 MainActivity 中引用按钮 ID 为按钮的按钮。 Unfortunately, I'm getting a casting error when I attempt to cast ContextImpl to an activity.不幸的是,当我尝试将 ContextImpl 转换为活动时遇到转换错误。 So I'm pretty sure my second row is completely wrong.所以我很确定我的第二行是完全错误的。

I also wanted to ask if instrumented tests are supposed to be in cases like this?我还想问一下,在这种情况下是否应该进行仪器测试? Would I use a UI test?我会使用 UI 测试吗?

Thank you.谢谢你。 I would greatly appreciate any assistance.我将不胜感激任何帮助。

Note: I am using junit 4.12.注意:我使用的是 junit 4.12。

Maybe it's because you're not importing the class?也许是因为您没有导入课程? You left out what you had imported, but it could be that you're not accessing the file due to lack of import.您遗漏了导入的内容,但可能是由于缺少导入而无法访问该文件。 Here's an article about it.这是一篇关于它的文章。

https://www.android-examples.com/create-and-call-function-in-android-activity-from-another-programming-file/ https://www.android-examples.com/create-and-call-function-in-android-activity-from-another-programming-file/

import com.android_examples.com.yourpackagename.CategoryButtonInstrumentatedTest;
public class MainActivity extends Activity {};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM