Skip to content

Commit 84c5da4

Browse files
authored
instrumented test added into junit post
1 parent 9fcd8a9 commit 84c5da4

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

_drafts/2019-01-14-junit.md

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,85 @@ class PrintTestRule : TestRule {
315315

316316
`AndroidX Test` zawiera zestaw gotowych zasad dla JUnit, które zwiększają elastyczność, redukują powtarzający się kod oraz wspomagają testowanie komponentów Android. Wykorzystywane są przede wszystkim testach UI przy użyciu `Espresso`. `ActivityTestRule` dostarcza do klasy testowej żądanej Aktywności (`Activity`), która jest dostępna w całym cyklu życia klasy testowej, `ServiceTestRule` dostarcza Usługę (`Service`) natomiast `IntentsTestRule` dostarcza Intencję (`Intent`).
317317

318-
//TODO maybe
318+
>**Przykład**
319+
>Aktywność `GameActivity` umożliwia użytkownikowi prowadzenie rozgrywki opierając się na klasie `Game` co może wprowadzać potrzebę przetestowania klasy `Game` z poziomu aplikacji. W tym celu należy stworzyć testy instrumentalne (w pakieckie `androidTest`) wykonywane przez `AndroidJUnit4` i wykorzystać `ActivityTestRule` (lub bibliotekę `Robolectric`).
320+
321+
{% highlight kotlin %}
322+
@RunWith(AndroidJUnit4::class)
323+
class InstrumentedTest {
324+
325+
@Rule @JvmField
326+
val activityRule = ActivityTestRule(GameActivity::class.java)
327+
328+
//use only context
329+
@Test
330+
fun checkStringResourceFromContext() {
331+
val appContext = InstrumentationRegistry.getTargetContext()
332+
assertEquals("Game", appContext.getString(R.string.app_name))
333+
}
334+
335+
//use activity from rule
336+
@Test
337+
fun startAndStopGameFromActivity() {
338+
activityRule.activity.initDefaultGame()
339+
activityRule.activity.startGame()
340+
assertTrue(activityRule.activity.isGameRunning())
341+
activityRule.activity.stopGame()
342+
assertFalse(activityRule.activity.isGameRunning())
343+
}
344+
}
345+
346+
class MainActivity : AppCompatActivity() {
347+
348+
private val game = Game()
349+
350+
override fun onCreate(savedInstanceState: Bundle?) {
351+
super.onCreate(savedInstanceState)
352+
setContentView(R.layout.activity_main)
353+
//init view
354+
}
355+
356+
//imit real actions flow in one function
357+
fun startGame() {
358+
game.start()
359+
}
360+
361+
fun stopGame() {
362+
game.stop()
363+
}
364+
365+
fun initDefaultGame() {
366+
game.addPlayer(Game.Team.RED, "Johnnie")
367+
game.addPlayer(Game.Team.RED, "William")
368+
game.addPlayer(Game.Team.BLUE, "Jack")
369+
game.addPlayer(Game.Team.BLUE, "Jim")
370+
}
371+
372+
fun isGameRunning(): Boolean {
373+
return game.hasStarted()
374+
}
375+
}
376+
{% endhighlight %}
319377

320378
## Filtry
321-
`AndroidJUnitRunner` umożliwia stosowanie adnotacji dla testów instrumentalnych w celach informacyjnych. Adnotacja `@RequiresDevice` mówi, że test powinien zostać przeprowadzony tylko na urządzeniu fizycznym, `@SdkSupress` określa minimalne API natomiast `@SmallTest`, `@MediumTest` i `@LargeTest` informują o wielkości testu co przekłada się na czas jego wykonania.
379+
`AndroidJUnitRunner` umożliwia stosowanie adnotacji dla testów instrumentalnych w celach restrykcyjnych. Adnotacja `@RequiresDevice` mówi, że test powinien zostać przeprowadzony tylko na urządzeniu fizycznym, `@SdkSupress` określa minimalne API natomiast `@SmallTest`, `@MediumTest` i `@LargeTest` informują o wielkości testu co przekłada się na czas jego wykonania.
380+
381+
{% highlight kotlin %}
382+
@RunWith(AndroidJUnit4::class)
383+
class FilterInstrumentedTest {
384+
385+
@MediumTest @SdkSuppress(minSdkVersion = 21) @RequiresDevice
386+
@Test //runs only on devices with min 21 API
387+
fun checkAppTitle() {
388+
val appContext = InstrumentationRegistry.getTargetContext()
389+
assertEquals("Game", appContext.getString(R.string.app_name))
390+
}
391+
392+
@SmallTest @SdkSuppress(maxSdkVersion = 20)
393+
@Test //runs on devices and emulator with max 20 API
394+
fun checkOldAppTitle() {
395+
val appContext = InstrumentationRegistry.getTargetContext()
396+
assertEquals("Game Old", appContext.getString(R.string.app_name))
397+
}
398+
}
399+
{% endhighlight %}

0 commit comments

Comments
 (0)