Skip to content

Commit 2d44868

Browse files
committed
junit post code added
1 parent 6e216e6 commit 2d44868

File tree

9 files changed

+328
-0
lines changed

9 files changed

+328
-0
lines changed

junit/Game.kt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class Game {
2+
3+
private val redTeam = mutableListOf<String>()
4+
private val blueTeam = mutableListOf<String>()
5+
private var redGoals = 0
6+
private var blueGoals = 0
7+
private var gameStarted = false
8+
9+
fun start() {
10+
if(!gameStarted && teamsSizesEquals()) {
11+
gameStarted = true
12+
redGoals = 0
13+
blueGoals = 0
14+
//do and allow for some actions
15+
}
16+
}
17+
18+
fun stop() {
19+
if(gameStarted) {
20+
gameStarted = false
21+
//do some other actions
22+
}
23+
}
24+
25+
fun clearTeams() {
26+
redTeam.clear()
27+
blueTeam.clear()
28+
}
29+
30+
fun addPlayer(team: Team, player: String): Boolean {
31+
if(allowToAddPlayer(player)) {
32+
when(team) {
33+
Team.RED -> { redTeam.add(player) }
34+
Team.BLUE -> { blueTeam.add(player) }
35+
}
36+
return true
37+
}
38+
else return false
39+
}
40+
41+
fun removePlayer(player: String): Boolean {
42+
if(allowToRemovePlayer(player)) {
43+
if(redTeam.contains(player)) redTeam.remove(player)
44+
else blueTeam.remove(player)
45+
return true
46+
}
47+
else return false
48+
}
49+
50+
fun goal(team: Team) {
51+
when(team) {
52+
Team.RED -> { redGoals++ }
53+
Team.BLUE -> { blueGoals++ }
54+
}
55+
}
56+
57+
fun getScore(): String {
58+
return "RED $redGoals:$blueGoals BLUE"
59+
}
60+
61+
fun getRedTeam(): List<String> {
62+
return redTeam
63+
}
64+
65+
fun getBlueTeam(): List<String> {
66+
return blueTeam
67+
}
68+
69+
fun hasStarted(): Boolean {
70+
return gameStarted
71+
}
72+
73+
private fun allowToAddPlayer(player: String): Boolean {
74+
return (!gameStarted && !redTeam.contains(player) && !blueTeam.contains(player))
75+
}
76+
77+
private fun allowToRemovePlayer(player: String): Boolean {
78+
return (!gameStarted && (redTeam.contains(player) || blueTeam.contains(player)))
79+
}
80+
81+
private fun teamsSizesEquals(): Boolean {
82+
return redTeam.size != 0 && blueTeam.size != 0 && redTeam.size.equals(blueTeam.size)
83+
}
84+
85+
enum class Team {
86+
RED, BLUE;
87+
}
88+
}

junit/GameActivity.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class GameActivity : AppCompatActivity() {
2+
3+
private val game = Game()
4+
5+
override fun onCreate(savedInstanceState: Bundle?) {
6+
super.onCreate(savedInstanceState)
7+
setContentView(R.layout.activity_game)
8+
//init view
9+
}
10+
11+
//imit real actions flow in one function
12+
fun startGame() {
13+
game.start()
14+
}
15+
16+
fun stopGame() {
17+
game.stop()
18+
}
19+
20+
fun initDefaultGame() {
21+
game.addPlayer(Game.Team.RED, "Johnnie")
22+
game.addPlayer(Game.Team.RED, "William")
23+
game.addPlayer(Game.Team.BLUE, "Jack")
24+
game.addPlayer(Game.Team.BLUE, "Jim")
25+
}
26+
27+
fun isGameRunning(): Boolean {
28+
return game.hasStarted()
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@RunWith(AndroidJUnit4::class)
2+
class FilterInstrumentedTest {
3+
4+
@MediumTest @SdkSuppress(minSdkVersion = 21) @RequiresDevice
5+
@Test //runs only on devices with min 21 API
6+
fun checkAppTitle() {
7+
val appContext = InstrumentationRegistry.getTargetContext()
8+
assertEquals("Game", appContext.getString(R.string.app_name))
9+
}
10+
11+
@SmallTest @SdkSuppress(maxSdkVersion = 20)
12+
@Test //runs on devices and emulator with max 20 API
13+
fun checkOldAppTitle() {
14+
val appContext = InstrumentationRegistry.getTargetContext()
15+
assertEquals("Game Old", appContext.getString(R.string.app_name))
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@RunWith(AndroidJUnit4::class)
2+
class InstrumentedTest {
3+
4+
@Rule @JvmField
5+
val activityRule = ActivityTestRule(GameActivity::class.java)
6+
7+
//use only context
8+
@Test
9+
fun checkStringResourceFromContext() {
10+
val appContext = InstrumentationRegistry.getTargetContext()
11+
assertEquals("Game", appContext.getString(R.string.app_name))
12+
}
13+
14+
//use activity from rule
15+
@Test
16+
fun startAndStopGameFromActivity() {
17+
activityRule.activity.initDefaultGame()
18+
activityRule.activity.startGame()
19+
assertTrue(activityRule.activity.isGameRunning())
20+
activityRule.activity.stopGame()
21+
assertFalse(activityRule.activity.isGameRunning())
22+
}
23+
}

junit/test/FullTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class FullTest {
2+
3+
//mock some dependencies
4+
val game = Game()
5+
6+
@Before
7+
fun init() {
8+
//clear game before every test
9+
game.clearTeams()
10+
}
11+
12+
@After
13+
fun uninit() {
14+
//clear game before every test
15+
game.stop()
16+
}
17+
//use @BeforeClass, @AfterClass in the same way
18+
19+
@Ignore //ignore this test temporary
20+
fun startGameWithDifferentTeamsSize() {
21+
game.addPlayer(Game.Team.RED, "Johnnie")
22+
game.addPlayer(Game.Team.BLUE, "Jack")
23+
game.addPlayer(Game.Team.BLUE, "Jim")
24+
game.start()
25+
assertFalse(game.hasStarted())
26+
}
27+
28+
@Test(timeout = 1000) //after 1000ms when test couldn't finished than just failed
29+
fun startAndStopGame() {
30+
game.addPlayer(Game.Team.RED, "Johnnie")
31+
game.addPlayer(Game.Team.RED, "William")
32+
game.addPlayer(Game.Team.BLUE, "Jack")
33+
game.addPlayer(Game.Team.BLUE, "Jim")
34+
game.start()
35+
assertTrue(game.hasStarted())
36+
game.stop()
37+
assertFalse(game.hasStarted())
38+
}
39+
40+
@Test
41+
fun modifyTeamsAndStartGame() {
42+
game.addPlayer(Game.Team.RED, "Johnnie")
43+
game.addPlayer(Game.Team.RED, "William")
44+
game.addPlayer(Game.Team.BLUE, "Jack")
45+
game.start()
46+
assertFalse(game.hasStarted())
47+
game.removePlayer("William")
48+
game.start()
49+
assertTrue(game.hasStarted())
50+
}
51+
}

junit/test/ParameterizedTest.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
@RunWith(Parameterized::class)
2+
class ParameterizedTest(val redGoals: Int, val blueGoals: Int, val scores: String) {
3+
4+
companion object {
5+
val game = Game()
6+
7+
@BeforeClass @JvmStatic
8+
fun initTeams() {
9+
game.addPlayer(Game.Team.RED, "Johnnie")
10+
game.addPlayer(Game.Team.RED, "William")
11+
game.addPlayer(Game.Team.BLUE, "Jack")
12+
game.addPlayer(Game.Team.BLUE, "Jim")
13+
}
14+
15+
@AfterClass @JvmStatic
16+
fun uninitTeams() {
17+
game.clearTeams()
18+
}
19+
20+
//create test data
21+
@Parameters @JvmStatic
22+
fun createData(): Collection<Array<Any>> {
23+
return listOf(
24+
arrayOf(0, 2, "RED 0:2 BLUE"),
25+
arrayOf(10, 5, "RED 10:5 BLUE"),
26+
arrayOf(3, 3, "RED 3:3 BLUE"))
27+
}
28+
}
29+
30+
//use auto parameterized test method
31+
@Test
32+
fun scoreAndCheckResultWithParameterized() {
33+
game.start()
34+
repeat(redGoals) { game.goal(Game.Team.RED) }
35+
repeat(blueGoals) { game.goal(Game.Team.BLUE) }
36+
assertEquals(scores, game.getScore())
37+
game.stop()
38+
}
39+
40+
//instead of manual parameterized
41+
@Test
42+
fun scoreAndCheckResultNormal() {
43+
//first test
44+
game.start()
45+
repeat(2) { game.goal(Game.Team.BLUE) }
46+
assertEquals("RED 0:2 BLUE", game.getScore())
47+
game.stop()
48+
49+
//second test
50+
game.start()
51+
repeat(10) { game.goal(Game.Team.RED) }
52+
repeat(5) { game.goal(Game.Team.BLUE) }
53+
assertEquals("RED 10:5 BLUE", game.getScore())
54+
game.stop()
55+
56+
//third test
57+
game.start()
58+
repeat(3) { game.goal(Game.Team.RED) }
59+
repeat(3) { game.goal(Game.Team.BLUE) }
60+
assertEquals("RED 3:3 BLUE", game.getScore())
61+
game.stop()
62+
}
63+
}

junit/test/PrintTestRule.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class PrintTestRule : TestRule {
2+
3+
private lateinit var base: Statement
4+
private lateinit var description: Description
5+
6+
override fun apply(base: Statement, description: Description): Statement {
7+
this.base = base
8+
this.description = description
9+
return PrintTestStatement(base)
10+
}
11+
12+
class PrintTestStatement(private val base: Statement) : Statement() {
13+
override fun evaluate() {
14+
println("Log before test action")
15+
base.evaluate()
16+
println("Log after testaction")
17+
}
18+
}
19+
}

junit/test/RuleTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class RuleTest {
2+
3+
@Rule @JvmField
4+
val rule = PrintTestRule() //this rule print message before and after every test
5+
6+
val game = Game()
7+
8+
@Test
9+
fun addPlayersWithSameNameToTheSameTeam() {
10+
game.addPlayer(Game.Team.RED, "Johnnie")
11+
game.addPlayer(Game.Team.RED, "William")
12+
game.addPlayer(Game.Team.RED, "Jack")
13+
game.addPlayer(Game.Team.RED, "William")
14+
assertEquals(3, game.getRedTeam().size)
15+
}
16+
17+
@Test
18+
fun addPlayersWithSameNameToTheAnotherTeam() {
19+
game.addPlayer(Game.Team.RED, "Johnnie")
20+
game.addPlayer(Game.Team.RED, "William")
21+
game.addPlayer(Game.Team.BLUE, "Jack")
22+
game.addPlayer(Game.Team.BLUE, "William")
23+
assertEquals(1, game.getBlueTeam().size)
24+
}
25+
}

junit/test/SmallTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class SmallTest {
2+
3+
@Test
4+
fun startGameWithDifferentTeamsSize() {
5+
val game = Game()
6+
game.addPlayer(Game.Team.RED, "Johnnie")
7+
game.addPlayer(Game.Team.BLUE, "Jack")
8+
game.addPlayer(Game.Team.BLUE, "Jim")
9+
game.start()
10+
assertFalse(game.hasStarted())
11+
}
12+
}

0 commit comments

Comments
 (0)