Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_13_2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_2_2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.52" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.52" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.52" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.9" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.9" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.9" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:8.0.26" level="project" />
Expand Down Expand Up @@ -99,5 +98,7 @@
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.9" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:2.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.9" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
</component>
</module>
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ResponseEntity<BlogPost> create(@RequestBody BlogPost blogPost){
return new ResponseEntity<>(service.create(blogPost), HttpStatus.CREATED);
}


@PutMapping(value = "/update/{id}")
public ResponseEntity<BlogPost> update(@PathVariable Long id, @RequestBody BlogPost blogPost){
return new ResponseEntity<>(service.update(id, blogPost), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,105 @@

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;

@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<BlogPost> expected = new ResponseEntity<>(blogPost,HttpStatus.OK);

//when
Mockito.doReturn(blogPost).when(service).read(Mockito.anyLong());
ResponseEntity<BlogPost> 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<BlogPost> blogPostList = new ArrayList<>();
ResponseEntity<List<BlogPost>> expected = new ResponseEntity<>(blogPostList,HttpStatus.OK);

//when
Mockito.doReturn(blogPostList).when(service).readAll();
ResponseEntity<List<BlogPost>> 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<BlogPost> expected = new ResponseEntity<>(blogPost,HttpStatus.CREATED);

//when
Mockito.doReturn(blogPost).when(service).create(blogPost);
ResponseEntity<BlogPost> 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<BlogPost> expected = new ResponseEntity<>(blogPost,HttpStatus.OK);

//when
Mockito.when(service.update(5L, blogPost)).thenReturn(blogPost);

ResponseEntity<BlogPost> 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<BlogPost> expected = new ResponseEntity<>(blogPost,HttpStatus.OK);
void deleteTest() throws Exception {

//when
BlogPost blogPost = new BlogPost();
//
Mockito.doReturn(blogPost).when(service).delete(Mockito.anyLong());
ResponseEntity<BlogPost> actual = blogPostController.delete(Mockito.anyLong());

//Then
assertEquals(expected,actual);
mockMvc.perform(delete("/blogPostController/delete/{id}", 5L))
.andExpect(status().isOk());

}
}
77 changes: 42 additions & 35 deletions src/test/java/com/example/demo/controller/TagControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag> expected = new ResponseEntity<>(tag, HttpStatus.OK);

//when
Mockito.doReturn(tag).when(service).read(Mockito.anyLong());
ResponseEntity<Tag> 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<Tag> tagList = new ArrayList<>();
ResponseEntity<List<Tag>> expected = new ResponseEntity<>(tagList,HttpStatus.OK);

//when
Mockito.doReturn(tagList).when(service).readAll();
ResponseEntity<List<Tag>> 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<Tag> expected = new ResponseEntity<>(tag,HttpStatus.CREATED);

//when
Mockito.doReturn(tag).when(service).create(tag);
ResponseEntity<Tag> 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<Tag> expected = new ResponseEntity<>(tag,HttpStatus.OK);

//when
Mockito.when(service.update(5L, tag)).thenReturn(tag);

ResponseEntity<Tag> 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<Tag> expected = new ResponseEntity<>(tag,HttpStatus.OK);

//when
Mockito.doReturn(tag).when(service).delete(Mockito.anyLong());
ResponseEntity<Tag> actual = tagController.delete(Mockito.anyLong());

//Then
assertEquals(expected,actual);
mockMvc.perform(delete("/tagController/delete/{id}", 5L))
.andExpect(status().isOk());
}
}
Loading