forked from BuntyRaghani/spring-boot-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bunty Raghani
committed
May 2, 2021
1 parent
5052615
commit c75065d
Showing
8 changed files
with
184 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,51 @@ | ||
# spring-boot-hello-world | ||
A simple Spring Boot app to send hello world message to a user | ||
# Spring Boot Hello World | ||
|
||
**A simple Spring Boot app to send hello world message to a user** | ||
|
||
## How to Run Application | ||
|
||
**Start the application using any of the commands mentioned below** | ||
|
||
> **Note:** First two commands need to run inside the root folder of this project i.e inside the **spring-boot-hello-world** folder | ||
|
||
- **Using maven** <br/>``` mvn spring-boot:run``` | ||
|
||
|
||
- **From jar file** | ||
Create a jar file using '**mvn clean install**' command and then execute | ||
<br/>```java -jar target/hello-world-1.0.1-SNAPSHOT.jar``` | ||
|
||
|
||
- **Directly from IDE** | ||
<br/>```Right click on HelloWorldApplication.java and click on 'Run' option``` | ||
<br/><br/> | ||
|
||
> **Note:** By default spring boot application starts on port number 8080. If port 8080 is occupied in your system then you can change the port number by uncommenting and updating the **server.port** property inside the **application.properties** file that is available inside the **src > main > resources** folder. | ||
<br/> | ||
|
||
**Send an HTTP GET request to '/hello' endpoint using any of the two methods** | ||
|
||
- **Browser or REST client** | ||
<br/>```http://localhost:8080/hello``` | ||
|
||
|
||
- **cURL** | ||
<br/>```curl --request GET 'http://localhost:8080/hello'``` | ||
|
||
|
||
## How to Run Unit Test Cases | ||
|
||
**Run the test cases using any of the commands mentioned below** | ||
|
||
> **Note:** These commands need to run inside the root folder of this project i.e inside the **spring-boot-hello-world** folder | ||
- **To run all the test cases** | ||
<br/>```mvn test``` | ||
|
||
|
||
- **To run a particular test class** | ||
<br/>```mvn -Dtest=HelloWorldControllerTest test``` | ||
<br/>or | ||
<br/>```mvn -Dtest=HelloWorldApplicationTests test``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.4.5</version> | ||
</parent> | ||
|
||
<groupId>com.example</groupId> | ||
<artifactId>hello-world</artifactId> | ||
<version>1.0.1-SNAPSHOT</version> | ||
|
||
<name>hello-world</name> | ||
<description>A simple Spring Boot app to send hello world message to a user</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/helloworld/HelloWorldApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.helloworld; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class HelloWorldApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(HelloWorldApplication.class, args); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/helloworld/controller/HelloWorldController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.helloworld.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class HelloWorldController { | ||
|
||
@GetMapping("/hello") | ||
public String sendGreetings() { | ||
return "Hello, World!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# configuring port number on which application should run | ||
#server.port=8081 |
23 changes: 23 additions & 0 deletions
23
src/test/java/com/example/helloworld/HelloWorldApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.helloworld; | ||
|
||
import com.example.helloworld.controller.HelloWorldController; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
|
||
@SpringBootTest | ||
class HelloWorldApplicationTests { | ||
|
||
@Autowired | ||
private HelloWorldController helloWorldController; | ||
|
||
@Test | ||
void contextLoads() { | ||
// to ensure that controller is getting created inside the application context | ||
assertNotNull(helloWorldController); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/com/example/helloworld/controller/HelloWorldControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.helloworld.controller; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public class HelloWorldControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
public void shouldReturnExpectedMessage() throws Exception { | ||
|
||
mockMvc.perform(get("/hello")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("Hello, World!")); | ||
} | ||
} |