Skip to content

Commit c8c8a3e

Browse files
committed
updated tests
1 parent 0c74510 commit c8c8a3e

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

server/src/test/java/pl/piotrowski/remotetexteditor/controller/DocumentsControllerIntegrationTest.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import java.util.function.Supplier;
2525

2626
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
import static org.mockito.BDDMockito.given;
28-
import static org.mockito.BDDMockito.willDoNothing;
27+
import static org.mockito.BDDMockito.*;
28+
import static org.mockito.Mockito.verify;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
3030
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3131
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -52,13 +52,16 @@ void getDocumentTest() throws Exception {
5252

5353
Document document = testDocumentFactory.get();
5454

55-
given(documentsService.getDocument(document.getName())).willReturn(document);
55+
String name = document.getName();
56+
given(documentsService.getDocument(name)).willReturn(document);
5657

57-
mockMvc.perform(get("/docs/{name}", document.getName())).andExpect(status().isOk())
58-
.andExpect(jsonPath("$.name").value(document.getName()))
58+
mockMvc.perform(get("/docs/{name}", name)).andExpect(status().isOk())
59+
.andExpect(jsonPath("$.name").value(name))
5960
.andExpect(jsonPath("$.content").value(document.getContent()))
6061
.andExpect(jsonPath("$.id").value(document.getId())).andReturn();
6162

63+
then(documentsService).should().getDocument(name);
64+
6265
}
6366

6467
@Test
@@ -75,26 +78,29 @@ void getAllDocumentsTest() throws Exception {
7578
});
7679

7780
assertEquals(documents, documentsFromResponse);
81+
then(documentsService).should().getAllDocuments();
7882
}
7983

8084
@Test
8185
void deleteDocumentTest() throws Exception {
8286

8387
Document document = testDocumentFactory.get();
8488

85-
willDoNothing().given(documentsService).removeDocument(document.getName());
86-
given(documentsService.getDocument(document.getName())).willReturn(document);
89+
String name = document.getName();
90+
willDoNothing().given(documentsService).removeDocument(name);
91+
given(documentsService.getDocument(name)).willReturn(document);
8792

88-
MvcResult mvcResult = mockMvc.perform(delete("/docs/{name}/delete", document.getName()))
93+
MvcResult mvcResult = mockMvc.perform(delete("/docs/{name}/delete", name))
8994
.andExpect(status().isOk()).andReturn();
9095

9196
HashMap<String, Document> response = objectMapper
9297
.readValue(mvcResult.getResponse().getContentAsString(),
9398
new TypeReference<HashMap<String, Document>>() {
9499
});
95100

96-
assertEquals(response.get("deleted").getName(), document.getName());
97-
101+
assertEquals(response.get("deleted").getName(), name);
102+
then(documentsService).should().removeDocument(name);
103+
then(documentsService).should().getDocument(name);
98104

99105
}
100106

@@ -112,18 +118,23 @@ void createDocumentTest() throws Exception {
112118
Document addedDocument = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), Document.class);
113119

114120
assertEquals(document, addedDocument);
121+
122+
then(documentsService).should().addDocument(document);
115123
}
116124

117125
@Test
118126
void renameTest() throws Exception {
119127
Document document = testDocumentFactory.get();
120128
String newName = "Foo";
121129

122-
given(documentsService.changeDocumentsName(document.getName(), newName)).willReturn(document);
130+
String name = document.getName();
131+
given(documentsService.changeDocumentsName(name, newName)).willReturn(document);
123132

124-
mockMvc.perform(patch("/docs/{name}", document.getName()).param("newName", newName))
133+
mockMvc.perform(patch("/docs/{name}", name).param("newName", newName))
125134
.andExpect(status().isOk()).andReturn();
126135

136+
then(documentsService).should().changeDocumentsName(name, newName);
137+
127138
}
128139

129140

server/src/test/java/pl/piotrowski/remotetexteditor/dataaccess/DataAccessIntegrationTest.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
package pl.piotrowski.remotetexteditor.dataaccess;
22

3-
import jdk.nashorn.internal.ir.annotations.Ignore;
4-
import org.hibernate.exception.ConstraintViolationException;
5-
import org.junit.jupiter.api.AfterEach;
63
import org.junit.jupiter.api.BeforeEach;
7-
import org.junit.jupiter.api.Disabled;
84
import org.junit.jupiter.api.Test;
95
import org.junit.jupiter.api.extension.ExtendWith;
106
import org.springframework.beans.factory.annotation.Autowired;
117
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
128
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
13-
import org.springframework.context.annotation.Profile;
14-
import org.springframework.dao.DataIntegrityViolationException;
159
import org.springframework.test.context.ActiveProfiles;
1610
import org.springframework.test.context.ContextConfiguration;
1711
import org.springframework.test.context.junit.jupiter.SpringExtension;
1812
import pl.piotrowski.remotetexteditor.Application;
19-
import pl.piotrowski.remotetexteditor.application.DocumentsService;
2013
import pl.piotrowski.remotetexteditor.configuration.TestContext;
2114
import pl.piotrowski.remotetexteditor.model.Document;
22-
import pl.piotrowski.remotetexteditor.service.exceptions.DocumentNotFoundException;
2315

2416
import javax.persistence.PersistenceException;
25-
import javax.print.Doc;
2617
import java.util.HashSet;
2718
import java.util.Optional;
2819
import java.util.function.Supplier;
@@ -83,9 +74,9 @@ void deleteByNameTest() {
8374

8475
@Test
8576
void saveDuplicateThrowsException() {
86-
Document newDoc = new Document("Hello there!","General Kenobi!");
77+
Document newDoc = new Document("Hello there!", "General Kenobi!");
8778
documentsRepository.save(newDoc);
8879
documentsRepository.save(new Document("Hello there!", "Hi!"));
89-
assertThrows(PersistenceException.class, ()-> entityManager.flush());
80+
assertThrows(PersistenceException.class, () -> entityManager.flush());
9081
}
9182
}

server/src/test/java/pl/piotrowski/remotetexteditor/service/DocumentsServiceTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
import pl.piotrowski.remotetexteditor.dataaccess.DocumentsRepository;
1515
import pl.piotrowski.remotetexteditor.model.Document;
1616
import pl.piotrowski.remotetexteditor.model.Update;
17-
import pl.piotrowski.remotetexteditor.service.exceptions.DocumentNotFoundException;
1817

1918
import java.util.HashSet;
2019
import java.util.Optional;
2120
import java.util.function.Supplier;
2221

2322
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertThrows;
2523
import static org.mockito.BDDMockito.given;
2624
import static org.mockito.BDDMockito.then;
2725
import static org.mockito.BDDMockito.willDoNothing;
28-
import static org.mockito.Mockito.verify;
2926

3027
@ExtendWith(SpringExtension.class)
3128
@ContextConfiguration(classes = {TestContext.class, Application.class})

0 commit comments

Comments
 (0)