The presentation will tell which frameworks you can use to test your application starting from local unit tests and end with functional testing.
Tools that are described are JUnit, Mockito, PowerMock, Hamcrest, Espresso, Robolectric.
CachedAppRepository @Before fun setUp() { repository = mock() thread = mock() postExecution = mock() sut = GetInstalledAppsUseCase(repository, thread, postExecution) } @Test fun `test use case is built correctly`() { sut.buildUseCaseObservable(None) verify(repository).getInstalledApps() }
// mock all the static methods in a class called "Static" PowerMockito.mockStatic(Static::class.java) // use Mockito to set up your expectation Mockito.`when`(Static.firstStaticMethod(param)).thenReturn(value) // execute your test classCallStaticMethodObj.execute() // Always use PowerMockito.verifyStatic(Class) first // to start verifying behavior PowerMockito.verifyStatic(Static::class.java, Mockito.times(2)) // IMPORTANT: Call the static method you want to verify Static.firstStaticMethod(param) } }
the Activity called by Android the first time the Activity is created. * * @return Activity controller instance. */ public ActivityController<T> setup() { return create().start().postCreate(null).resume().visible(); }
val activity = Robolectric.setupActivity(MyActivity::class.java) val button = activity.findViewById(R.id.button) as Button val results = activity.findViewById(R.id.results) as TextView button.performClick() assertThat(results.getText().toString()) .isEqualTo("Robolectric Rocks!") } }
Tests by Steve Freeman, Nat Pryce (Book) • Tomek Kaczanowski - Practical Unit Testing with JUnit and Mockito (Book) • XUnit Test Patterns Refactoring Test Code (Book)