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
28 changes: 27 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,10 +32,17 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
<mockito.version>2.28.2</mockito.version>
<byte-buddy.version>1.9.16</byte-buddy.version>
</properties>


Expand All @@ -45,6 +52,25 @@
<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.8</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
72 changes: 72 additions & 0 deletions src/test/java/hello/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package hello;

import hello.model.Quote;
import hello.model.Value;
import org.junit.Test;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

public class ApplicationTest {

@Test
public void testRestTemplateBean() {
Application app = new Application();
RestTemplateBuilder builder = new RestTemplateBuilder();
RestTemplate rt = app.restTemplate(builder);
assertNotNull(rt);
}

@Test
public void testCommandLineRunnerBean() throws Exception {
Application app = new Application();
RestTemplate mockRestTemplate = mock(RestTemplate.class);
Quote quote = new Quote();
quote.setType("success");
Value value = new Value();
value.setId(1L);
value.setQuote("Test");
quote.setValue(value);
when(mockRestTemplate.getForObject(anyString(), eq(Quote.class))).thenReturn(quote);

CommandLineRunner runner = app.run(mockRestTemplate);
assertNotNull(runner);
runner.run();
verify(mockRestTemplate).getForObject(anyString(), eq(Quote.class));
}

@Test
public void testRunJdbcOperations() throws Exception {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

Application app = new Application();
java.lang.reflect.Field field = Application.class.getDeclaredField("jdbcTemplate");
field.setAccessible(true);
field.set(app, jdbcTemplate);

app.run(new String[]{});

java.util.List<hello.model.Customer> customers = jdbcTemplate.query(
"SELECT id, first_name, last_name FROM customers WHERE first_name = ?",
new Object[]{"Josh"},
(rs, rowNum) -> new hello.model.Customer(
rs.getLong("id"),
rs.getString("first_name"),
rs.getString("last_name")
)
);
assertEquals(2, customers.size());
}
}
42 changes: 42 additions & 0 deletions src/test/java/hello/controller/GreetingControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hello.controller;

import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

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;

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

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

@Test
public void testGreetingWithName() throws Exception {
mockMvc.perform(get("/").param("name", "Bob"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, Bob!"));
}

@Test
public void testGreetingIncrementsId() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk());
mockMvc.perform(get("/"))
.andExpect(status().isOk());
}
}
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.mockito.ArgumentMatchers.anyString;
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 testDatetime() throws Exception {
mockMvc.perform(get("/datetime"))
.andExpect(status().isOk())
.andExpect(content().string(org.hamcrest.Matchers.containsString("Greetings from Spring Boot!")));
}

@Test
public void testDatetimeContainsLeapYear() throws Exception {
mockMvc.perform(get("/datetime"))
.andExpect(status().isOk())
.andExpect(content().string(org.hamcrest.Matchers.containsString("Is this a leap year")));
}

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

mockMvc.perform(get("/topic/string/operation"))
.andExpect(status().isOk())
.andExpect(content().string(org.hamcrest.Matchers.containsString("spring:java:javascript")));
}

@Test
public void testShowFileOperation() throws Exception {
when(topicService.findAllFilesInPathAndSort()).thenReturn("file1; file2");
when(topicService.findParticularFileInPathAndSort()).thenReturn("gradle_file");
when(topicService.findParticularFileInPathAndSortWithWalkFunction()).thenReturn("gradle_walk");
when(topicService.readFileWithStreamFunction()).thenReturn("print_content");

mockMvc.perform(get("/topic/file/operation"))
.andExpect(status().isOk())
.andExpect(content().string(org.hamcrest.Matchers.containsString("file1; file2")));
}
}
123 changes: 123 additions & 0 deletions src/test/java/hello/controller/TopicControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package hello.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import hello.model.Topic;
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.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.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class TopicControllerTest {

private MockMvc mockMvc;
private ObjectMapper objectMapper = new ObjectMapper();

@Mock
private TopicService topicService;

@InjectMocks
private TopicController topicController;

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

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

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

@Test
public void testGetTopicWithID() throws Exception {
Topic topic = new Topic("java", "Core Java", "Java Description");
when(topicService.getTopicWithId("java")).thenReturn(topic);

mockMvc.perform(get("/topic/java"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value("java"))
.andExpect(jsonPath("$.subjectName").value("Core Java"));
}

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

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

verify(topicService).addTopic(any(Topic.class));
}

@Test
public void testUpdateTopic() throws Exception {
Topic topic = new Topic("java", "Updated Java", "Updated Description");

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

verify(topicService).updateTopic(eq("java"), any(Topic.class));
}

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

verify(topicService).deleteTopic("java");
}

@Test
public void testFilterMinimumLengthForId() 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("$[0].id").value("spring"));
}

@Test
public void testSortTopicsWithID() 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("$[0].id").value("java"));
}
}
21 changes: 21 additions & 0 deletions src/test/java/hello/declaration/CustomPredicateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hello.declaration;

import org.junit.Test;
import static org.junit.Assert.*;

public class CustomPredicateTest {

@Test
public void testPredicateWithString() {
CustomPredicate<String> isLong = s -> s.length() > 5;
assertTrue(isLong.test("longstring"));
assertFalse(isLong.test("hi"));
}

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