Skip to content

Commit 77bfa8d

Browse files
committed
TDD microservice
0 parents  commit 77bfa8d

File tree

9 files changed

+262
-0
lines changed

9 files changed

+262
-0
lines changed

.gitignore

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Ignore target resources
2+
**/target
3+
4+
# Ignore logs/
5+
**/logs
6+
7+
### Eclipse ###
8+
*.zip
9+
.metadata
10+
bin/
11+
tmp/
12+
*.tmp
13+
local.properties
14+
.settings/
15+
.idea/
16+
.eclipse
17+
18+
!.mvn/wrapper/maven-wrapper.jar
19+
20+
### STS ###
21+
.apt_generated
22+
.classpath
23+
.factorypath
24+
.project
25+
.settings
26+
.springBeans
27+
.sts4-cache
28+
29+
### IntelliJ IDEA ###
30+
.idea
31+
*.iws
32+
*.iml
33+
*.ipr
34+
35+
### NetBeans ###
36+
/nbproject/private/
37+
/build/
38+
/nbbuild/
39+
/dist/
40+
/nbdist/
41+
/.nb-gradle/

pom.xml

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.workflow.github</groupId>
8+
<artifactId>workflow-github-actions</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
<java.version>8</java.version>
15+
16+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
17+
<junit5.version>5.1.1</junit5.version>
18+
<junit.jupiter.version>5.1.0</junit.jupiter.version>
19+
<junit.vintage.version>5.1.0</junit.vintage.version>
20+
<junit.platform.version>1.1.0</junit.platform.version>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
<version>2.2.6.RELEASE</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-web</artifactId>
34+
<version>2.2.6.RELEASE</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<version>2.2.6.RELEASE</version>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
</dependencies>
45+
46+
<profiles>
47+
<profile>
48+
<id>eclipse</id>
49+
<dependencies>
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter-engine</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.junit.platform</groupId>
56+
<artifactId>junit-platform-launcher</artifactId>
57+
<version>1.6.0</version>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
<dependencyManagement>
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.junit.jupiter</groupId>
65+
<artifactId>junit-jupiter-engine</artifactId>
66+
<version>${junit5.version}</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.junit.platform</groupId>
71+
<artifactId>junit-platform-launcher</artifactId>
72+
<version>1.1.1</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
</dependencyManagement>
77+
</profile>
78+
</profiles>
79+
80+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.workflow.github;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class WorkflowApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(WorkflowApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.workflow.github;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpEntity;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequestMapping("demo")
14+
public class WorkflowController {
15+
16+
@Autowired
17+
private WorkflowService workflowService;
18+
19+
@GetMapping(value = "actions", produces = MediaType.APPLICATION_JSON_VALUE)
20+
public HttpEntity<String> actions() {
21+
22+
// Simulate call service
23+
String message = workflowService.message();
24+
25+
return new ResponseEntity<String>(message, HttpStatus.OK);
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.workflow.github;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class WorkflowService {
7+
public String message() {
8+
return "Hello";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server.port=9090
2+
spring.profiles.active=DEV
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.workflow.github;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class EnvTest {
7+
8+
@Test
9+
public void should_return_true_when_initialize_env() {
10+
Assertions.assertThat(true).isTrue();
11+
}
12+
13+
@Test
14+
public void should_concatenate_two_strings() {
15+
String actual = "hello ".concat("world");
16+
String expected = "hello world";
17+
18+
Assertions.assertThat(actual).isEqualTo(expected);
19+
}
20+
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.workflow.github;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
8+
import org.springframework.boot.test.web.client.TestRestTemplate;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
17+
public class IntegrationEndpointTest {
18+
19+
@Autowired
20+
private TestRestTemplate restTemplate;
21+
22+
@Test
23+
public void should_return_hello_world_when_calling_get_endpoint() {
24+
// ARRANGE
25+
ResponseEntity<String> response = restTemplate.getForEntity("/demo/actions", String.class);
26+
27+
// ASSERT
28+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
29+
assertThat(response.getBody()).isEqualTo("Hello");
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.workflow.github;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.mockito.BDDMockito;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
8+
import org.springframework.boot.test.mock.mockito.MockBean;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
12+
13+
import static org.mockito.BDDMockito.*;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@WebMvcTest(WorkflowController.class)
19+
public class WorkflowControllerTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
@MockBean
25+
private WorkflowService workflowService;
26+
27+
@Test
28+
public void should_return_hello_world_when_calling_get_endpoint() throws Exception {
29+
30+
given(workflowService.message()).willReturn("AZUL");
31+
32+
mockMvc.perform(MockMvcRequestBuilders.get("/demo/actions"))
33+
.andExpect(status().isOk())
34+
.andExpect(jsonPath("$").value("AZUL"));
35+
}
36+
}

0 commit comments

Comments
 (0)