diff --git a/.idea/libraries/Maven__junit_junit_4_13_2.xml b/.idea/libraries/Maven__junit_junit_4_13_2.xml
new file mode 100644
index 000000000..606c352d5
--- /dev/null
+++ b/.idea/libraries/Maven__junit_junit_4_13_2.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_2_2.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_2_2.xml
new file mode 100644
index 000000000..15f1e4c17
--- /dev/null
+++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_2_2.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_springframework_spring_web_5_3_92.xml b/.idea/libraries/Maven__org_springframework_spring_web_5_3_9.xml
similarity index 100%
rename from .idea/libraries/Maven__org_springframework_spring_web_5_3_92.xml
rename to .idea/libraries/Maven__org_springframework_spring_web_5_3_9.xml
diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml
new file mode 100644
index 000000000..797acea53
--- /dev/null
+++ b/.idea/runConfigurations.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/demo.iml b/demo.iml
index 2f45d51da..f17d54dcb 100644
--- a/demo.iml
+++ b/demo.iml
@@ -61,7 +61,6 @@
-
@@ -99,5 +98,7 @@
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index b4f4fc9d4..328ea740b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -52,6 +52,38 @@
4.13.2
test
+
+ org.springframework
+ spring-web
+ 5.3.9
+ test
+
+
+ org.springframework
+ spring-web
+
+
+ org.springframework
+ spring-web
+ 5.3.2
+ test
+
+
+ org.springframework
+ spring-web
+
+
+ org.springframework
+ spring-web
+
+
+
+ com.google.code.gson
+ gson
+ 2.8.6
+
+
+
diff --git a/src/main/java/com/example/demo/controller/BlogPostController.java b/src/main/java/com/example/demo/controller/BlogPostController.java
index 00c752bba..4c831b2f7 100644
--- a/src/main/java/com/example/demo/controller/BlogPostController.java
+++ b/src/main/java/com/example/demo/controller/BlogPostController.java
@@ -31,6 +31,7 @@ public ResponseEntity create(@RequestBody BlogPost blogPost){
return new ResponseEntity<>(service.create(blogPost), HttpStatus.CREATED);
}
+
@PutMapping(value = "/update/{id}")
public ResponseEntity update(@PathVariable Long id, @RequestBody BlogPost blogPost){
return new ResponseEntity<>(service.update(id, blogPost), HttpStatus.OK);
diff --git a/src/test/java/com/example/demo/controller/BlogPostControllerTest.java b/src/test/java/com/example/demo/controller/BlogPostControllerTest.java
index b8773a17c..5923725fd 100644
--- a/src/test/java/com/example/demo/controller/BlogPostControllerTest.java
+++ b/src/test/java/com/example/demo/controller/BlogPostControllerTest.java
@@ -2,21 +2,27 @@
import com.example.demo.models.BlogPost;
import com.example.demo.service.BlogPostService;
+import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
-
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.ArrayList;
import java.util.List;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class BlogPostControllerTest {
+ @Autowired
+ MockMvc mockMvc;
@InjectMocks
BlogPostController blogPostController;
@@ -24,82 +30,77 @@ class BlogPostControllerTest {
@Mock
BlogPostService service;
+ private Gson gson;
+
+ BlogPostControllerTest() {
+ }
+
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(blogPostController).build();
+ gson = new Gson();
+
}
@Test
- void read() {
- //given
+ void read() throws Exception {
+
BlogPost blogPost = new BlogPost();
- ResponseEntity expected = new ResponseEntity<>(blogPost,HttpStatus.OK);
- //when
Mockito.doReturn(blogPost).when(service).read(Mockito.anyLong());
- ResponseEntity actual = blogPostController.read(Mockito.anyLong());
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(get("/blogPostController/read/{id}",5L)).andExpect(status().isOk());
}
@Test
- void readAll() {
- //given
+ void readAll() throws Exception {
+
List blogPostList = new ArrayList<>();
- ResponseEntity> expected = new ResponseEntity<>(blogPostList,HttpStatus.OK);
- //when
Mockito.doReturn(blogPostList).when(service).readAll();
- ResponseEntity> actual = blogPostController.readAll();
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(get("/blogPostController/read",5L)).andExpect(status().isOk());
}
@Test
- void create() {
- //given
+ void create() throws Exception {
+
BlogPost blogPost = new BlogPost();
- ResponseEntity expected = new ResponseEntity<>(blogPost,HttpStatus.CREATED);
- //when
Mockito.doReturn(blogPost).when(service).create(blogPost);
- ResponseEntity actual = blogPostController.create(blogPost);
-
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(post("/blogPostController/create")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(gson.toJson(blogPost)))
+ .andExpect(status().isCreated());
}
@Test
- void updateTest() {
- //given
+ void updateTest() throws Exception {
+
BlogPost blogPost = new BlogPost();
- ResponseEntity expected = new ResponseEntity<>(blogPost,HttpStatus.OK);
- //when
Mockito.when(service.update(5L, blogPost)).thenReturn(blogPost);
- ResponseEntity actual = blogPostController.update(5L,blogPost);
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(put("/blogPostController/update/{id}",5L)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(gson.toJson(blogPost)))
+ .andExpect(status().isOk());
}
@Test
- void delete() {
- //given
- BlogPost blogPost = new BlogPost();
- ResponseEntity expected = new ResponseEntity<>(blogPost,HttpStatus.OK);
+ void deleteTest() throws Exception {
- //when
+ BlogPost blogPost = new BlogPost();
+//
Mockito.doReturn(blogPost).when(service).delete(Mockito.anyLong());
- ResponseEntity actual = blogPostController.delete(Mockito.anyLong());
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(delete("/blogPostController/delete/{id}", 5L))
+ .andExpect(status().isOk());
+
}
}
\ No newline at end of file
diff --git a/src/test/java/com/example/demo/controller/TagControllerTest.java b/src/test/java/com/example/demo/controller/TagControllerTest.java
index 00210ec0c..4e4f46d0b 100644
--- a/src/test/java/com/example/demo/controller/TagControllerTest.java
+++ b/src/test/java/com/example/demo/controller/TagControllerTest.java
@@ -3,100 +3,107 @@
import com.example.demo.models.BlogPost;
import com.example.demo.models.Tag;
import com.example.demo.service.TagService;
+import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class TagControllerTest {
+ @Autowired
+ MockMvc mockMvc;
@InjectMocks
TagController tagController;
@Mock
TagService service;
+
+ private Gson gson;
+
+ TagControllerTest(){
+
+ }
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(tagController).build();
+ gson = new Gson();
}
@Test
- void read() {
- //given
+ void read() throws Exception {
+
Tag tag = new Tag();
- ResponseEntity expected = new ResponseEntity<>(tag, HttpStatus.OK);
- //when
Mockito.doReturn(tag).when(service).read(Mockito.anyLong());
- ResponseEntity actual = tagController.read(Mockito.anyLong());
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(get("/tagController/read/{id}",5L)).andExpect(status().isOk());
}
@Test
- void readAll() {
- //given
+ void readAll() throws Exception {
+
List tagList = new ArrayList<>();
- ResponseEntity> expected = new ResponseEntity<>(tagList,HttpStatus.OK);
- //when
Mockito.doReturn(tagList).when(service).readAll();
- ResponseEntity> actual = tagController.readAll();
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(get("/tagController/read",5L)).andExpect(status().isOk());
}
@Test
- void create() {
- //given
+ void create() throws Exception {
+
Tag tag = new Tag();
- ResponseEntity expected = new ResponseEntity<>(tag,HttpStatus.CREATED);
- //when
Mockito.doReturn(tag).when(service).create(tag);
- ResponseEntity actual = tagController.create(tag);
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(post("/tagController/create")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(gson.toJson(tag)))
+ .andExpect(status().isCreated());
}
@Test
- void update() {
- //given
+ void update() throws Exception {
+
Tag tag = new Tag();
- ResponseEntity expected = new ResponseEntity<>(tag,HttpStatus.OK);
- //when
Mockito.when(service.update(5L, tag)).thenReturn(tag);
- ResponseEntity actual = tagController.update(5L,tag);
+ mockMvc.perform(put("/tagController/update/{id}",5L)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(gson.toJson(tag)))
+ .andExpect(status().isOk());
+
- //Then
- assertEquals(expected,actual);
}
@Test
- void delete() {
- //given
+ void deleteTest() throws Exception {
+
Tag tag = new Tag();
- ResponseEntity expected = new ResponseEntity<>(tag,HttpStatus.OK);
- //when
Mockito.doReturn(tag).when(service).delete(Mockito.anyLong());
- ResponseEntity actual = tagController.delete(Mockito.anyLong());
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(delete("/tagController/delete/{id}", 5L))
+ .andExpect(status().isOk());
}
}
\ No newline at end of file
diff --git a/src/test/java/com/example/demo/controller/UserControllerTest.java b/src/test/java/com/example/demo/controller/UserControllerTest.java
index 78f4acdaf..38900e330 100644
--- a/src/test/java/com/example/demo/controller/UserControllerTest.java
+++ b/src/test/java/com/example/demo/controller/UserControllerTest.java
@@ -3,101 +3,106 @@
import com.example.demo.models.BlogPost;
import com.example.demo.models.User;
import com.example.demo.service.UserService;
+import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class UserControllerTest {
+ @Autowired
+ MockMvc mockMvc;
+
@InjectMocks
UserController userController;
@Mock
UserService service;
+ private Gson gson;
+
+ UserControllerTest(){
+
+ }
+
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
+ gson = new Gson();
}
@Test
- void read() {
- //given
+ void read() throws Exception {
+
User user = new User();
- ResponseEntity expected = new ResponseEntity<>(user, HttpStatus.OK);
- //when
Mockito.doReturn(user).when(service).read(Mockito.anyLong());
- ResponseEntity actual = userController.read(Mockito.anyLong());
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(get("/userController/read/{id}",5L)).andExpect(status().isOk());
}
@Test
- void readAll() {
- //given
+ void readAll() throws Exception {
+
List userList = new ArrayList<>();
- ResponseEntity> expected = new ResponseEntity<>(userList,HttpStatus.OK);
- //when
Mockito.doReturn(userList).when(service).readAll();
- ResponseEntity> actual = userController.readAll();
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(get("/userController/read",5L)).andExpect(status().isOk());
}
@Test
- void create() {
- //given
+ void create() throws Exception {
+
User user = new User();
- ResponseEntity expected = new ResponseEntity<>(user,HttpStatus.CREATED);
- //when
Mockito.doReturn(user).when(service).create(user);
- ResponseEntity actual = userController.create(user);
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(post("/userController/create")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(gson.toJson(user)))
+ .andExpect(status().isCreated());
}
@Test
- void update() {
- //given
+ void update() throws Exception {
+
User user = new User();
- ResponseEntity expected = new ResponseEntity<>(user,HttpStatus.OK);
- //when
Mockito.when(service.update(5L, user)).thenReturn(user);
- ResponseEntity actual = userController.update(5L,user);
-
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(put("/userController/update/{id}",5L)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(gson.toJson(user)))
+ .andExpect(status().isOk());
}
@Test
- void delete() {
- //given
+ void deleteTest() throws Exception {
+
User user = new User();
- ResponseEntity expected = new ResponseEntity<>(user,HttpStatus.OK);
- //when
Mockito.doReturn(user).when(service).delete(Mockito.anyLong());
- ResponseEntity actual = userController.delete(Mockito.anyLong());
- //Then
- assertEquals(expected,actual);
+ mockMvc.perform(delete("/userController/delete/{id}", 5L))
+ .andExpect(status().isOk());
}
}
\ No newline at end of file
diff --git a/target/classes/com/example/demo/controller/BlogPostController.class b/target/classes/com/example/demo/controller/BlogPostController.class
index b5aded88e..aa27f89d7 100644
Binary files a/target/classes/com/example/demo/controller/BlogPostController.class and b/target/classes/com/example/demo/controller/BlogPostController.class differ
diff --git a/target/test-classes/com/example/demo/controller/BlogPostControllerTest.class b/target/test-classes/com/example/demo/controller/BlogPostControllerTest.class
index 417a4927e..41839a1ba 100644
Binary files a/target/test-classes/com/example/demo/controller/BlogPostControllerTest.class and b/target/test-classes/com/example/demo/controller/BlogPostControllerTest.class differ
diff --git a/target/test-classes/com/example/demo/controller/TagControllerTest.class b/target/test-classes/com/example/demo/controller/TagControllerTest.class
index 07bd4c9ee..ce59272b2 100644
Binary files a/target/test-classes/com/example/demo/controller/TagControllerTest.class and b/target/test-classes/com/example/demo/controller/TagControllerTest.class differ
diff --git a/target/test-classes/com/example/demo/controller/UserControllerTest.class b/target/test-classes/com/example/demo/controller/UserControllerTest.class
index 939462214..add5216a3 100644
Binary files a/target/test-classes/com/example/demo/controller/UserControllerTest.class and b/target/test-classes/com/example/demo/controller/UserControllerTest.class differ