Parcourir la source

game service tested

nafis nibir il y a 8 ans
Parent
révision
9fcc595be0

+ 2
- 0
server/src/test/java/com/example/demo/RepositoryTest/UserRepositoryTest.java Voir le fichier

7
 import org.springframework.beans.factory.annotation.Autowired;
7
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
8
 import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
9
 import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
9
 import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
10
+import org.springframework.test.annotation.DirtiesContext;
10
 import org.springframework.test.context.junit4.SpringRunner;
11
 import org.springframework.test.context.junit4.SpringRunner;
11
 
12
 
12
 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
13
 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
13
 
14
 
14
 @RunWith(SpringRunner.class)
15
 @RunWith(SpringRunner.class)
15
 @DataJpaTest
16
 @DataJpaTest
17
+@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
16
 public class UserRepositoryTest {
18
 public class UserRepositoryTest {
17
 
19
 
18
     @Autowired
20
     @Autowired

+ 48
- 0
server/src/test/java/com/example/demo/ServiceTest/GameServiceTest.java Voir le fichier

1
 package com.example.demo.ServiceTest;
1
 package com.example.demo.ServiceTest;
2
 
2
 
3
+import com.example.demo.Entity.Game;
4
+import com.example.demo.Repository.GameRepository;
5
+import com.example.demo.Service.GameService;
6
+import org.junit.Assert;
7
+import org.junit.Before;
8
+import org.junit.Test;
9
+import org.mockito.InjectMocks;
10
+import org.mockito.Mock;
11
+import org.mockito.Mockito;
12
+import org.mockito.MockitoAnnotations;
13
+
14
+import java.util.ArrayList;
15
+import java.util.Collection;
16
+import java.util.List;
17
+
3
 public class GameServiceTest {
18
 public class GameServiceTest {
19
+
20
+    @Mock
21
+    private GameRepository gameRepository;
22
+
23
+    @InjectMocks
24
+    private GameService gameService;
25
+
26
+    @Before
27
+    public void setUp(){
28
+        MockitoAnnotations.initMocks(this);
29
+    }
30
+
31
+    @Test
32
+    public void createGameTest(){
33
+        Game metroid = new Game("metroid","nintendo", "nes");
34
+        Mockito.when(gameRepository.save(metroid)).thenReturn(metroid);
35
+
36
+        Game actual = gameService.createGame(metroid);
37
+
38
+        Assert.assertEquals(actual, metroid);
39
+    }
40
+
41
+    @Test
42
+    public void findAllGamesTest(){
43
+        List<Game> games = new ArrayList<>();
44
+        games.add(new Game("metroid","nintendo", "nes"));
45
+        games.add(new Game("contra","nintendo", "nes"));
46
+
47
+        Mockito.when(gameRepository.findAll()).thenReturn(games);
48
+
49
+        Collection<Game> actual = gameService.findAllGames();
50
+        Assert.assertEquals(actual, games);
51
+    }
4
 }
52
 }