0

I have an application that draws random characters from the alphabet and deals with them. I’m new to JUnit and try to use it. I want to test the application by providing predefined characters and testing if it deals with them right. How can I pass the characters to the tested project?

I tried setting System.property in the test case like: System.setProperty( "string_array", LETTERS );

And reading it in the tested project like: String z = System.getProperty("string_array");

But this doesn’t work.

Is there a solution, a workaround or am I totally on the wrong way?

Kai
  • 38,985
  • 14
  • 88
  • 103
Herrbert74
  • 2,578
  • 31
  • 51
  • We usually mock test context (with test data) in test project and pass it to application under test. It is rarely need to pass something from outside of test project. – yorkw May 05 '12 at 02:17

2 Answers2

0

I must admit I have not done much unit testing on Android yet. But in theory you should not change the code in the test subject (your project) to do unit testing. With JUnit you do white box testing, meaning you do not have to test the entire application at once but rather a specific method, or object. From JUnit you can instantiate project internal classes and feed the tested method with whatever data you like, and this is how you transfer data to the test subject.

At this time I have no code examples for you but You might want to read throu Android Testing Fundamentals, since they have some fancy add-on to the regular JUnit TestCase class.

Qben
  • 2,617
  • 2
  • 24
  • 36
0

The solution is setActivityIntent.

You can set up the test project by sending an intent to the tested Activity like this:

public void setUp() throws Exception {
    Intent i = new Intent();
    i.putExtra("testLetterz", LETTERS);
    setActivityIntent(i);
    solo = new Solo(getInstrumentation(), getActivity());
}

And then run the test like this:

@Smoke
public void testCheckOneWord() throws Exception {
    for(int i = 0; i < 5; i++) {
        int r = -1;
        Movable m;
        do {
            r++;
            m = (Movable) solo.getView(Movable.class, r);
        } while(!(m.getLetter() == LETTERS.charAt(i)));
        int x = m.getPosX();
        int y = m.getPosY();
        solo.drag(x, 10, y, 10+i*Movable.getDropSize(), 1);

    }
    solo.clickOnButton("Check");
    boolean expected = true;
    boolean actual = solo.searchText("2/10");
    assertEquals("The test is not found", expected, actual);
}

In the tested Activity you read the intent and you use a method that returns a string with random characters, but if testLetterz is not null, it returns testLetterz.

In this example I dragged the Views that contain the letters to a drop zone and then checked them if they are in a word list.

I used Robotium.

Herrbert74
  • 2,578
  • 31
  • 51