Android - Junit Testing Models/controllers (which Only Use Java Imports) Without The Android Emulator Deploy
Solution 1:
My Test-Classes used to be:
publicclassMyUnitTestextendsandroid.test.AndroidTestCase
{
publicvoidaTest(){
...
assertSomething(...);
...
}
...
}
And I used to run them with Run As -> Android JUnit Test.
I've changed it by the following steps:
- First I've followed the instructions here.
- Then in all my Test-Classes I've removed the
extends android.test.AndroidTestCase
. - Added the following two imports:
import junit.framework.Assert;
andimport org.junit.Test;
(WARNING: Make sure you usejunit.framework.Assert
instead oforg.junit.Assert
). - Added
@Test
before every Test-Method. - Changed all
assertSomething(...);
toAssert.assertSomething(...);
and allfail(...);
toAssert.fail(...);
- Removed some Android-Logs I still had in some of my Model/Controller classes..
Now my Test-Classes are:
import junit.framework.Assert;
import org.junit.Test;
publicclassMyUnitTest
{
@TestpublicvoidaTest(){
...
Assert.assertSomething(...);
...
}
...
}
And now I'm able to run the Project by Run As -> JUnit Test.
UPDATE:
When you have this Project up and running, testing all UnitTests at the same time in Eclipse by right-clicking the Project -> Run as -> JUnit Test isn't a problem. But when you try to Test individual UnitTest-classes (like the ones you've just added), the same error as in my question might occur.
These are the steps when you want to add a Java-only UnitTest-class in your Test-Project, which only uses Java libraries and enabling individual testing of the Test-classes without getting the error:
- Add a class (right-click a folder inside src) -> New -> Class
- Add the
import junit.framework.Assert;
andimport org.junit.Test;
like mentioned above. - Create some UnitTest-methods with the synthax like above (so including the
@Test
andAssert.assertSomething(...);
) - Right-click the new UnitTest -> Run as -> JUnitTest
- Select Eclipse JUnit Launcher
- Now the error will occur that is displayed at the bottom of my question-post
- Right-click the Test-Project -> Properties -> Run/Debug Settings
- Select the new UnitTest that gives the error -> Edit...
- Go to the Classpath tab -> Under Bootstrap Entries remove Google APIs [Android 4.4.2]. (This might be different in your case, but you should remove the
Android API
orAndroid Google API
.) - After you've removed it click Apply -> OK -> Apply again at the Run/Debug Settings -> OK
Now you are successfully able to use Run As -> JUnit Test on this individual Test-class, instead of being forced to Test the entire Project every time.
I hope this helps anyone trying to make JUnit Tests for an Android project. PS: I also have a second Test-Project with the JUnit Tests that require Android libraries. Just make sure none of the classes you want to test uses Android Libraries (like Logcat-messages, Test-Toasts, or something like Patterns.WEB_URL.matcher(url).matches())
[android.util.Patterns
] which validates a String URL) are being used inside your JUnit-only Test Project.
Post a Comment for "Android - Junit Testing Models/controllers (which Only Use Java Imports) Without The Android Emulator Deploy"