Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

HTML
<table width="100%" class="navigation">
<tr>
<td width="33%" align="left"><a href="http://confluence.jetbrains.com/display/IntelliJIDEA/Make+the+Application+Interactive">Previous</a></td|http://confluence.jetbrains.com/display/IntelliJIDEA/Getting+Started+with+Android+Development+in+IntelliJ+IDEA%22%3EPrevious%3C/a%3E%3C/td]>
<td width="33%" align="center"><a href="http://confluence.jetbrains.com/display/IntelliJIDEA/Getting+Started+with+Android+Development+in+IntelliJ+IDEA">Top</a></td|http://confluence.jetbrains.com/display/IntelliJIDEA/Getting+Started+with+Android+Development+in+IntelliJ+IDEA%22%3ETop%3C/a%3E%3C/td]>
<td width="33%" align="right"><a href="http://confluence.jetbrains.com/display/IntelliJIDEA/Package+the+Application+for+the+App+Store">Next</a></td>
</tr>
</table>

...

Wiki Markup
{redirect:https://www.jetbrains.com/idea/help/testing-android-applications.html

...

|delay

...

=0}

In Android, unit testing is based on JUnit and plain use of JUnit is enough to test features that are exclusively based on Java code. In order to test Android specific functionality, however, you need a bunch of wrapper classes just built on top of JUnit. IntelliJ IDEA streamlines most of the tasks around the build of an Android test project: all you do is interacting with the IDE through convenient dialog boxes and wizards.

...

You open the MyActivity class and select the portion of the code in method

TapDroid
span
classid
Wiki Markup
{span:class=id}TapDroid{span}
that refers to the production of the display message.

Rewrite the

TapDroid
span
classid
Wiki Markup
{span:class=id}TapDroid{span}
 method in such a way it calls into a newly created public helper method: the getStringForDisplay
span
classid
Wiki Markup
{span:class=id}getStringForDisplay{span}
method. Here's the code to enter:

Code Block
private void TapDroid() {
    counter++;
    String temp = getStringForDisplay(counter);
    message.setText(String.format("You touched the droid %s", temp));
}

public String getStringForDisplay(int count) {
    String temp;
    switch(count)
    {
        case 1:
            temp = "once";
            break;
        case 2:
            temp = "twice";
            break;
        default:
            temp = String.format("%d times", count);
    }
    return temp;
}

The

getStringForDisplay
span
classid
Wiki Markup
{span:class=id}getStringForDisplay{span}
 method is now much easier to test and the body of the TapDroid
span
classid
Wiki Markup
{span:class=id}TapDroid{span}
 method has been greatly simplified.

Note
iconfalse

IntelliJ IDEA has a lot of features as far as code refactoring is concerned. You might want to explore all of them from the

Refactor
span
classshortcut
Wiki Markup
{span:class=shortcut}Refactor{span}
 menu.

2. Create a test project

...

The newly created module has a test file named

MyActivityTest
span
classid
Wiki Markup
{span:class=id}MyActivityTest{span}
 right in the src folder. You can add more test files by simply adding more Java classes as shown below:

...

The test class inherits from extends

span
classid
ActivityInstrumentationTestCase2<T>
 where T
span
classid
Wiki Markup
{span:class=id}ActivityInstrumentationTestCase2<T>{span}
 where
Wiki Markup
{span:class=id}T{span}
 is the name of the activity you're willing to test. Note that the adding the constructor is required as there's no default constructor defined on the parent class.

...

You right click in the editor of the MyActivityTest class and click on

Generate
span
classshortcut
Wiki Markup
{span:class=shortcut}Generate{span}
. In alternative, you hit Alt + Insert
span
classshortcut
Wiki Markup
{span:class=shortcut}Alt + Insert{span}
. You get a context menu from where you select Test Method
span
classshortcut
Wiki Markup
{span:class=shortcut}Test Method{span}
.

IntelliJ IDEA creates a new method stub named

testName
span
classid
Wiki Markup
{span:class=id}testName{span}
 and it makes it quick and easy for you to change the Name suffix into something more meaningful in the context.

...

In order to run tests, you need to have a build configuration that just runs tests. A default configuration is created for you when you set up a test module. To edit settings, you click on

Run → Edit configurations
span
classshortcut
Wiki Markup
{span:class=shortcut}Run → Edit configurations{span}
, select the one you're interested in and proceed with edits.

...