Skip to content

Commit

Permalink
Creating tests for service classes
Browse files Browse the repository at this point in the history
  • Loading branch information
anadis504 committed Dec 1, 2020
1 parent e037014 commit 83e668b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import anadis.snakegame.dao.FileScoreDao;
import anadis.snakegame.dao.ScoreDao;
import java.util.List;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

Expand All @@ -30,11 +31,12 @@ public void addScore(String name, int score) {

public VBox getScores() {
VBox list = new VBox();
List<Score> scores = dao.topTwenty();
int rank = 1;
if (dao.topTwenty().size() == 0) {
if (scores.size() == 0) {
list.getChildren().add(new Label("No hightscores yet"));
} else {
for (Score score : dao.topTwenty()) {
for (Score score : scores) {
StringBuilder sb = new StringBuilder();
if (rank < 10) {
sb.append(" ").append(rank++).append(" : ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,23 @@
*/
public class GameServiceTest {

public GameServiceTest() {
}

@BeforeClass
public static void setUpClass() {
}
private GameService gameService;

@AfterClass
public static void tearDownClass() {
public GameServiceTest() {
}

@Before
public void setUp() {
this.gameService = new GameService();
}

@After
public void tearDown() {
}

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
// @Test
// public void hello() {}
@Test
public void testSomethingHere() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import anadis.snakegame.dao.FileScoreDao;
import anadis.snakegame.dao.ScoreDao;
import anadis.snakegame.domain.Score;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.layout.VBox;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -36,8 +40,17 @@ public void tearDown() {
}

@Test
public void testing() {


public void testingCallAddingMethod() {
when(dao.newHighscore(anyInt())).thenReturn(true);
scoreService.addScore("bob", 12);
verify(dao, times(1)).newHighscore(12);
}

@Test
public void serviceDoesNotAddWhenScoreTooLow() {
when(dao.newHighscore(anyInt())).thenReturn(false);
scoreService.addScore("bob", 11);
verify(dao, times(0)).add(anyObject());
}

}

0 comments on commit 83e668b

Please sign in to comment.