Skip to content
Open
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
40 changes: 39 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<packaging>pom</packaging>
<packaging>jar</packaging>
<version>0.1.0</version>

<parent>
Expand All @@ -32,6 +32,29 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.12.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand All @@ -45,6 +68,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/hello/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hello;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
public class ApplicationTest {

@MockBean
private RestTemplate restTemplate;

@TestConfiguration
static class TestConfig {
@Bean
public CommandLineRunner run(RestTemplate restTemplate) {
return args -> {};
}
}

@Test
public void contextLoads() {
}
}
57 changes: 57 additions & 0 deletions src/test/java/hello/controller/GreetingControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hello.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class GreetingControllerTest {

private MockMvc mockMvc;

private final ObjectMapper objectMapper = new ObjectMapper();

@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(new GreetingController()).build();
}

@Test
public void greeting_defaultName_returnsHelloWorld() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content", is("Hello, World!")))
.andExpect(jsonPath("$.id").isNumber());
}

@Test
public void greeting_withName_returnsHelloName() throws Exception {
mockMvc.perform(get("/?name=Devin"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content", is("Hello, Devin!")))
.andExpect(jsonPath("$.id").isNumber());
}

@Test
public void greeting_calledTwice_idIncrements() throws Exception {
MvcResult first = mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andReturn();

MvcResult second = mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andReturn();

long firstId = objectMapper.readTree(first.getResponse().getContentAsString()).get("id").asLong();
long secondId = objectMapper.readTree(second.getResponse().getContentAsString()).get("id").asLong();

assertTrue("ID should increment", secondId > firstId);
}
}
70 changes: 70 additions & 0 deletions src/test/java/hello/controller/HelloControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package hello.controller;

import hello.service.TopicService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class HelloControllerTest {

private MockMvc mockMvc;

@Mock
private TopicService topicService;

@InjectMocks
private HelloController helloController;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(helloController).build();
}

@Test
public void datetime_returns200WithExpectedSubstrings() throws Exception {
mockMvc.perform(get("/datetime"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Datetime now is")))
.andExpect(content().string(containsString("leap year")));
}

@Test
public void showStringOperation_returns200WithAssembledResponse() throws Exception {
when(topicService.returnAllTopicIDWithStringSlicing()).thenReturn("spring:java:javascript");
when(topicService.makeDistinctAndSortCharacters("spring:java:javascript")).thenReturn(":acgijnoprstvw");
when(topicService.splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin("spring:java:javascript"))
.thenReturn("java:javascript");
when(topicService.findIdHavingCharacter()).thenReturn("[spring, javascript]");

mockMvc.perform(get("/topic/string/operation"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("spring:java:javascript")))
.andExpect(content().string(containsString(":acgijnoprstvw")))
.andExpect(content().string(containsString("java:javascript")))
.andExpect(content().string(containsString("[spring, javascript]")));
}

@Test
public void showFileOperation_returns200WithAssembledResponse() throws Exception {
when(topicService.findAllFilesInPathAndSort()).thenReturn("build.gradle; pom.xml");
when(topicService.findParticularFileInPathAndSort()).thenReturn("gradle; gradlew");
when(topicService.findParticularFileInPathAndSortWithWalkFunction()).thenReturn("gradle; gradlew");
when(topicService.readFileWithStreamFunction()).thenReturn(" Hello, World");

mockMvc.perform(get("/topic/file/operation"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("build.gradle; pom.xml")))
.andExpect(content().string(containsString("gradle; gradlew")))
.andExpect(content().string(containsString(" Hello, World")));
}
}
136 changes: 136 additions & 0 deletions src/test/java/hello/controller/TopicControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package hello.controller;

import hello.model.Topic;
import hello.service.TopicService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class TopicControllerTest {

private MockMvc mockMvc;

@Mock
private TopicService topicService;

@InjectMocks
private TopicController topicController;

private final ObjectMapper objectMapper = new ObjectMapper();

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(topicController).build();
}

@Test
public void getAllTopics_returns200WithJsonArray() throws Exception {
List<Topic> topics = Arrays.asList(
new Topic("spring", "Spring Framework", "Spring Description"),
new Topic("java", "Core Java", "Java Description")
);
when(topicService.getAllTopics()).thenReturn(topics);

mockMvc.perform(get("/topic"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].id", is("spring")))
.andExpect(jsonPath("$[1].id", is("java")));
}

@Test
public void getTopicWithID_returns200WithJsonObject() throws Exception {
Topic topic = new Topic("spring", "Spring Framework", "Spring Description");
when(topicService.getTopicWithId("spring")).thenReturn(topic);

mockMvc.perform(get("/topic/spring"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("spring")))
.andExpect(jsonPath("$.subjectName", is("Spring Framework")))
.andExpect(jsonPath("$.subjectDescription", is("Spring Description")));
}

@Test
public void addTopic_callsServiceAddTopic() throws Exception {
Topic topic = new Topic("python", "Python", "Python Desc");

mockMvc.perform(post("/topic")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(topic)))
.andExpect(status().isOk());

verify(topicService, times(1)).addTopic(any(Topic.class));
}

@Test
public void updateTopic_callsServiceUpdateTopic() throws Exception {
Topic topic = new Topic("spring", "Spring Boot", "Updated Desc");

mockMvc.perform(put("/topic/spring")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(topic)))
.andExpect(status().isOk());

verify(topicService, times(1)).updateTopic(eq("spring"), any(Topic.class));
}

@Test
public void deleteTopic_callsServiceDeleteTopic() throws Exception {
mockMvc.perform(delete("/topic/spring"))
.andExpect(status().isOk());

verify(topicService, times(1)).deleteTopic("spring");
}

@Test
public void filterMinimumLengthForId_returns200WithFilteredTopics() throws Exception {
List<Topic> filtered = Arrays.asList(
new Topic("spring", "Spring Framework", "Spring Description"),
new Topic("javascript", "JavaScript", "JS Description")
);
when(topicService.filterMinimumLengthForId(4)).thenReturn(filtered);

mockMvc.perform(get("/topic/minimum/length/4"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].id", is("spring")))
.andExpect(jsonPath("$[1].id", is("javascript")));
}

@Test
public void sortTopicsWithID_returns200WithSortedTopics() throws Exception {
List<Topic> sorted = Arrays.asList(
new Topic("java", "Core Java", "Java Description"),
new Topic("javascript", "JavaScript", "JS Description"),
new Topic("spring", "Spring Framework", "Spring Description")
);
when(topicService.sortTopicsWithID()).thenReturn(sorted);

mockMvc.perform(get("/topic/sort"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].id", is("java")))
.andExpect(jsonPath("$[1].id", is("javascript")))
.andExpect(jsonPath("$[2].id", is("spring")));
}
}
28 changes: 28 additions & 0 deletions src/test/java/hello/declaration/CustomPredicateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hello.declaration;

import org.junit.Test;

import static org.junit.Assert.*;

public class CustomPredicateTest {

@Test
public void test_withLambda_returnsTrue() {
CustomPredicate<String> isLong = s -> s.length() > 3;
assertTrue(isLong.test("hello"));
}

@Test
public void test_withLambda_returnsFalse() {
CustomPredicate<String> isLong = s -> s.length() > 3;
assertFalse(isLong.test("hi"));
}

@Test
public void test_withIntegerPredicate() {
CustomPredicate<Integer> isPositive = n -> n > 0;
assertTrue(isPositive.test(5));
assertFalse(isPositive.test(-1));
assertFalse(isPositive.test(0));
}
}
Loading