简体   繁体   中英

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.

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

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. Additionally, CategoryButton can connect to an UnscrollableListView using the connect(UnscrollableListView listView) method. This allows the CategoryButton to toggle the visibility of the UnscrollableListView it's connected to using the onClick() method.

How can I create a new CategoryButton inside my CategoryButtonInstrumentationTest class so that I can test its functionality?

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. Unfortunately, I'm getting a casting error when I attempt to cast ContextImpl to an activity. 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?

Thank you. I would greatly appreciate any assistance.

Note: I am using 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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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