Search code examples
androidjunit3

How do I pass parameters to the tested project?


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?


Solution

  • 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.