-1

I have begin incorporating unit tests (junit 4.12) into my Android project.

One scenario I have encountered is determining whether a view has been added correctly.

For example in my ViewToTest class:

private TextView _text;

/**
 * Shows all text.
 */
public void showText()
{
    addView(_text);
}

/**
 * Hides all text.
 */
public void hideAllText()
{
    removeView(_text);
}

From what I understand with unit testing I am supposed to test the behaviour not the method, but in this case, the behaviour is the method (or correct me if I am wrong).

My question is 2 parts:

  1. How do I test that _text was added to parent view?
  2. How do I handle the above scenario when the view is private?

For part 2 I know I could create a getter for the view but to me that defeats the point. I would only be adding that for the test!

All advice welcome thanks!

Aggressor
  • 13,323
  • 24
  • 103
  • 182

1 Answers1

0

There's a document that describes key concepts related to Android app testing. But basically indicates that you should use:

Unit tests

Use this approach to run unit tests that have no dependencies on the Android framework or have dependencies that mock objects can satisfy.

Integration Tests

Components within your app only: This type of test verifies that the target app behaves as expected when a user performs a specific action or enters a specific input in its activities. For example, it allows you to check that the target app returns the correct UI output in response to user interactions in the app’s activities. UI testing frameworks like Espresso allow you to programmatically simulate user actions and test complex intra-app user interactions.

Cross-app Components: this type of test verifies the correct behavior of interactions between different user apps or between user apps and system apps. For example, you might want to test that your app behaves correctly when the user performs an action in the Android Settings menu. UI testing frameworks that support cross-app interactions, such as UI Automator, allow you to create tests for such scenarios.

For your test type is more useful Espresso framework. It's very simple to use. Here you have a lesson of how to test UI in a single app.

Community
  • 1
  • 1
Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37