5. JUnit test(Cntd)
5. Set Assert Function
Assert API - http://junit.org/javadoc/latest/org/junit/Assert.html
Annotation - https://github.com/junit-team/junit/wiki
(ex : @Test(timeout=5000), @Test(expected=RuntimeException.class),
@Test(timeout=5000), @Ignore(value=”ignore”)
@Before, @After)
6. JUnit test(Cntd)
Using EasyMock
Using when external class isn’t completed.
Interface of incomplete class is need.
Download and add library :
http://easymock.org/
8. Android App Unit Test
Android test suites are based on JUnit. You
can use plain JUnit to test a class that
doesn't call the Android API, or Android's
JUnit extensions to test Android components.
9. Android App Unit Test(Cntd)
1. New – Project – Android Test Project
11. Android App Unit Test(Cntd)
3. New – Junit Testcase
Set Name, Superclass, Class Under Test
12. Android App Unit Test(Cntd)
Superclass for Activity
ActivityInstrumentationTestCase2
The ActivityInstrumentationTestCase2 test case class is designed to do functional testing of one or more Activities in
an application, using a normal system infrastructure. It runs the Activities in a normal instance of the application
under test, using a standard system Context. It allows you to send mock Intents to the activity under test, so you
can use it to test an activity that responds to multiple types of intents, or an activity that expects a certain type of
data in the intent, or both. Notice, though, that it does not allow mock Contexts or Applications, so you can not
isolate the test from the rest of a production system.
ActivityUnitTestCase
The ActivityUnitTestCase test case class tests a single activity in isolation. Before you start the activity, you can
inject a mock Context or Application, or both. You use it to run activity tests in isolation, and to do unit testing of
methods that do not interact with Android. You can not send mock Intents to the activity under test, although you
can call Activity.startActivity(Intent) and then look at arguments that were received.
SingleLaunchActivityTestCase
The SingleLaunchActivityTestCase class is a convenience class for testing a single activity in an environment that
doesn't change from test to test. It invokes setUp() and tearDown() only once, instead of once per method call. It
does not allow you to inject any mock objects.
This test case is useful for testing an activity that runs in a mode other than standard. It ensures that the test fixture
is not reset between tests. You can then test that the activity handles multiple calls correctly.
13. Android App Unit Test(Cntd)
Superclass for Service
ServiceTestCase extends the JUnit TestCase class with with methods for testing application permissions and for
controlling the application and Service under test. It also provides mock application and Context objects that
isolate your test from the rest of the system.
ServiceTestCase defers initialization of the test environment until you call ServiceTestCase.startService() or
ServiceTestCase.bindService(). This allows you to set up your test environment, particularly your mock objects,
before the Service is started.
Notice that the parameters to ServiceTestCase.bindService()are different from those for Service.bindService(). For
the ServiceTestCase version, you only provide an Intent. Instead of returning a boolean,
ServiceTestCase.bindService() returns an object that subclasses IBinder.
The setUp() method for ServiceTestCase is called before each test. It sets up the test fixture by making a copy of the
current system Context before any test methods touch it. You can retrieve this Context by calling
getSystemContext(). If you override this method, you must call super.setUp() as the first statement in the override.
The methods setApplication() and setContext(Context) setContext()} allow you to set a mock Context or mock
Application (or both) for the Service, before you start it. These mock objects are described in Mock object classes.
By default, ServiceTestCase runs the test method testAndroidTestCaseSetupProperly(), which asserts that the base
test case class successfully set up a Context before running.
14. Android App Unit Test(Cntd)
4. Write Testcase and Run As – Android Junit
Test
16. Android App Unit Test(Cntd)
Moking
Using android.test.mock.MockPackageManager
(http://developer.android.com/reference/android/test/mock/MockPac
kageManager.html)
OR Using EasyMock
(http://ncona.com/2013/11/writing-unit-test-for-android-with-
easymock/)
17. C/C++ Unit Test
CppUnit
http://nyolong.egloos.com/2250020
http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html
Google Test
https://code.google.com/p/googletest/wiki/Primer
http://salkuma.wordpress.com/2014/02/17/google-test-
%EC%82%AC%EC%9A%A9%EB%B2%95/
http://stinkfist.egloos.com/2262578
https://code.google.com/p/googletest/wiki/AdvancedGuide
https://code.google.com/p/googlemock/wiki/CookBook#Teaching_Google_Mock_How_
to_Print_Your_Values
http://www.slideshare.net/zone0000/c-7522148
http://lancerme.tistory.com/19
29. GoogleMock
Mock Framework for C++ Unit Test :
GoogleMock
(https://code.google.com/p/googlemock/wiki/ForDum
mies)
Can be used with Any Testing Framework