瀏覽代碼

Revert "More testing of testing methods"

This reverts commit a02510d20a59ddecd6abb4c63acd2f7f2fe99820.
Trinh Tong 7 年之前
父節點
當前提交
70a143389c

+ 9
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java 查看文件

52
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
52
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
53
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
53
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
54
         // Save the entity
54
         // Save the entity
55
-        verifyPoll(poll.getId());
55
+        verifyPoll(poll);
56
         Poll p = pollRepository.save(poll);
56
         Poll p = pollRepository.save(poll);
57
         return new ResponseEntity<>(p, HttpStatus.OK);
57
         return new ResponseEntity<>(p, HttpStatus.OK);
58
     }
58
     }
66
 
66
 
67
 
67
 
68
     @RequestMapping(method = RequestMethod.GET)
68
     @RequestMapping(method = RequestMethod.GET)
69
+    public void verifyPoll(Poll poll) throws ResourceNotFoundException {
70
+        if (pollRepository.findOne(poll.getId()) == null) {
71
+            throw new ResourceNotFoundException();
72
+        }
73
+    }
74
+
75
+    @RequestMapping(method = RequestMethod.GET)
69
     public void verifyPoll(Long pollId) throws ResourceNotFoundException {
76
     public void verifyPoll(Long pollId) throws ResourceNotFoundException {
70
         if (pollRepository.findOne(pollId) == null) {
77
         if (pollRepository.findOne(pollId) == null) {
71
             throw new ResourceNotFoundException();
78
             throw new ResourceNotFoundException();
72
         }
79
         }
73
     }
80
     }
81
+
74
 }
82
 }

+ 0
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java 查看文件

11
 
11
 
12
 @RestController
12
 @RestController
13
 public class VoteController {
13
 public class VoteController {
14
-
15
     private VoteRepository voteRepository;
14
     private VoteRepository voteRepository;
16
 
15
 
17
     @Autowired
16
     @Autowired

+ 0
- 6
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java 查看文件

28
     public void setOption(Option option) {
28
     public void setOption(Option option) {
29
         this.option = option;
29
         this.option = option;
30
     }
30
     }
31
-
32
-    @Override
33
-    public String toString() {
34
-        return "{\"id\":" + id
35
-                + ",\"option\":" + option + "}";
36
-    }
37
 }
31
 }

+ 2
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java 查看文件

9
             "FROM Option o, Vote v " +
9
             "FROM Option o, Vote v " +
10
             "WHERE o.POLL_ID = ?1 " +
10
             "WHERE o.POLL_ID = ?1 " +
11
             "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
11
             "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
12
-    Iterable<Vote> findVotesByPoll(Long pollId);
12
+
13
+    public Iterable<Vote> findVotesByPoll(Long pollId);
13
 
14
 
14
 }
15
 }

+ 0
- 27
src/test/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultControllerTest.java 查看文件

1
-package io.zipcoder.tc_spring_poll_application.controller;
2
-
3
-import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
4
-import org.junit.Before;
5
-import org.junit.Test;
6
-import org.springframework.boot.test.mock.mockito.MockBean;
7
-
8
-import static org.junit.Assert.*;
9
-
10
-
11
-public class ComputeResultControllerTest {
12
-    private ComputeResultController computeResultController;
13
-
14
-    @MockBean
15
-    private VoteRepository voteRepository;
16
-
17
-    @Before
18
-    public void setUp(){
19
-        computeResultController = new ComputeResultController(voteRepository);
20
-    }
21
-
22
-    @Test
23
-    public void testComputeResults() {
24
-        voteRepository.findVotesByPoll(1L);
25
-        computeResultController.computeResult(1L);
26
-    }
27
-}

+ 37
- 63
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java 查看文件

1
 package io.zipcoder.tc_spring_poll_application.controller;
1
 package io.zipcoder.tc_spring_poll_application.controller;
2
 
2
 
3
 import com.fasterxml.jackson.databind.ObjectMapper;
3
 import com.fasterxml.jackson.databind.ObjectMapper;
4
-import io.zipcoder.tc_spring_poll_application.domain.Option;
5
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
6
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
7
 import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
6
 import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
8
-import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
9
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
7
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
10
 import org.junit.Assert;
8
 import org.junit.Assert;
11
 import org.junit.Before;
9
 import org.junit.Before;
13
 import org.junit.runner.RunWith;
11
 import org.junit.runner.RunWith;
14
 import org.mockito.InjectMocks;
12
 import org.mockito.InjectMocks;
15
 import org.mockito.Mock;
13
 import org.mockito.Mock;
16
-import org.mockito.MockitoAnnotations;
17
 import org.springframework.beans.factory.annotation.Autowired;
14
 import org.springframework.beans.factory.annotation.Autowired;
18
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
19
-import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
20
 import org.springframework.boot.test.context.SpringBootTest;
16
 import org.springframework.boot.test.context.SpringBootTest;
21
 import org.springframework.boot.test.json.JacksonTester;
17
 import org.springframework.boot.test.json.JacksonTester;
22
-import org.springframework.boot.test.mock.mockito.MockBean;
23
 import org.springframework.http.HttpStatus;
18
 import org.springframework.http.HttpStatus;
24
 import org.springframework.http.MediaType;
19
 import org.springframework.http.MediaType;
25
 import org.springframework.mock.web.MockHttpServletResponse;
20
 import org.springframework.mock.web.MockHttpServletResponse;
26
 import org.springframework.test.context.junit4.SpringRunner;
21
 import org.springframework.test.context.junit4.SpringRunner;
27
 import org.springframework.test.web.servlet.MockMvc;
22
 import org.springframework.test.web.servlet.MockMvc;
28
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
23
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
29
-import org.springframework.web.bind.annotation.RequestAttribute;
30
-import org.springframework.web.context.request.RequestAttributes;
31
-import org.springframework.web.context.request.RequestContextHolder;
32
 
24
 
33
 import java.util.Arrays;
25
 import java.util.Arrays;
34
 import java.util.Collections;
26
 import java.util.Collections;
35
-import java.util.List;
36
 
27
 
37
-import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
38
 import static org.mockito.BDDMockito.given;
28
 import static org.mockito.BDDMockito.given;
39
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
29
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
40
 
30
 
41
-//@SpringBootTest
31
+@SpringBootTest
42
 @RunWith(SpringRunner.class)
32
 @RunWith(SpringRunner.class)
43
-@WebMvcTest
33
+@AutoConfigureMockMvc
44
 public class VoteControllerTest {
34
 public class VoteControllerTest {
35
+
45
     @Autowired
36
     @Autowired
46
     private MockMvc mvc;
37
     private MockMvc mvc;
47
-
48
     private Vote testVote;
38
     private Vote testVote;
49
     private Iterable<Vote> votes;
39
     private Iterable<Vote> votes;
50
-    private final Long TEST_ID = 1L;
51
-    private final Long BAD_ID = 400L;
40
+    private Long testId;
52
 
41
 
53
-    @MockBean
54
-    private VoteRepository voteRepository;
55
 
42
 
56
-    @MockBean
57
-    private PollRepository pollRepository;
43
+    @Mock
44
+    private VoteRepository voteRepository;
58
 
45
 
59
     @InjectMocks
46
     @InjectMocks
60
     private VoteController voteController;
47
     private VoteController voteController;
61
 
48
 
62
-    private Poll testPoll;
63
-
64
     @Before
49
     @Before
65
     public void setUp() {
50
     public void setUp() {
66
 
51
 
72
                 .build();
57
                 .build();
73
 
58
 
74
         testVote = new Vote();
59
         testVote = new Vote();
75
-        testVote.setId(TEST_ID);
60
+        testId = 1L;
61
+        testVote.setId(1L);
76
 
62
 
77
         Vote[] votearr = {testVote};
63
         Vote[] votearr = {testVote};
78
         votes = Arrays.asList(votearr);
64
         votes = Arrays.asList(votearr);
79
 
65
 
80
-        testPoll = new Poll();
81
-        testPoll.setId(TEST_ID);
82
     }
66
     }
83
 
67
 
84
     @Test
68
     @Test
85
     public void testGetVotePollIdSuccess() throws Exception {
69
     public void testGetVotePollIdSuccess() throws Exception {
86
 
70
 
87
-        given(voteRepository.findVotesByPoll(TEST_ID))
88
-                .willReturn(votes);
89
-
90
-        MockHttpServletResponse response = mvc.perform(get("/polls/1/votes")
91
-                .content(asJsonString(votes))
92
-                .contentType(MediaType.APPLICATION_JSON)
93
-                .accept(MediaType.APPLICATION_JSON))
94
-                .andReturn().getResponse();
95
-
96
-        Assert.assertEquals(HttpStatus.OK.value(), response.getStatus());
97
-        Assert.assertEquals(response.getContentAsString(), votes.toString());
71
+//        given(voteRepository.findVotesByPoll(testId))
72
+//                .willReturn(votes);
73
+//
74
+//        MockHttpServletResponse response = mvc.perform(get("polls/1/votes")
75
+//                .accept(MediaType.APPLICATION_JSON))
76
+//                .andReturn().getResponse();
77
+//
78
+//        Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
79
+//        Assert.assertEquals(response.getContentAsString(), testVote.toString());
98
     }
80
     }
99
 
81
 
100
-//    @Test(expected = ResourceNotFoundException.class)
101
     @Test
82
     @Test
102
     public void testGetVoteIdFail() throws Exception {
83
     public void testGetVoteIdFail() throws Exception {
103
-        given(voteRepository.findVotesByPoll(BAD_ID))
104
-                .willThrow(new ResourceNotFoundException());
105
 
84
 
106
-        MockHttpServletResponse response = mvc.perform(get("/polls/500/votes")
107
-                .accept(MediaType.APPLICATION_JSON))
108
-                .andReturn().getResponse();
109
-
110
-        System.out.println(response.getStatus());
111
-
112
-        Assert.assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatus());
113
-    }
85
+        given(voteRepository.findAll())
86
+                .willReturn(null);
114
 
87
 
115
-    @Test
116
-    public void testGetAllVotes() throws Exception {
117
-        given(voteController.getAllVotes())
118
-                .willReturn(votes);
119
-
120
-        MockHttpServletResponse response = mvc.perform(get("/polls/votes")
88
+        MockHttpServletResponse response = mvc.perform(get("polls/200/votes")
121
                 .accept(MediaType.APPLICATION_JSON))
89
                 .accept(MediaType.APPLICATION_JSON))
122
                 .andReturn().getResponse();
90
                 .andReturn().getResponse();
123
 
91
 
124
-        Assert.assertEquals(response.getStatus(), HttpStatus.OK.value());
92
+        Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
125
         Assert.assertEquals(response.getContentAsString(), votes.toString());
93
         Assert.assertEquals(response.getContentAsString(), votes.toString());
126
     }
94
     }
127
 
95
 
128
-    @Test
129
-    public void testGetAllVotesFail() throws Exception {
130
-        given(voteController.getAllVotes())
131
-                .willThrow(new ResourceNotFoundException());
96
+//    @Test
97
+//    public void testGetAllVotes() throws Exception {
98
+//
99
+//        given(voteRepository.findOne(200L))
100
+//                .willThrow(new ResourceNotFoundException());
101
+//
102
+//        MockHttpServletResponse response = mvc.perform(
103
+//                get("polls/votes")
104
+//                        .accept(MediaType.APPLICATION_JSON))
105
+//                .andReturn().getResponse();
106
+//
107
+//        Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
108
+//        Assert.assertTrue(response.getContentAsString().isEmpty());
109
+//    }
132
 
110
 
133
-        MockHttpServletResponse response = mvc.perform(
134
-                get("polls/votes")
135
-                        .accept(MediaType.APPLICATION_JSON))
136
-                .andReturn().getResponse();
111
+    @Test
112
+    public void testGetAllVotesFail() {
137
 
113
 
138
-        Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
139
-        Assert.assertTrue(response.getContentAsString().isEmpty());
140
     }
114
     }
141
 
115
 
142
     @Test
116
     @Test