Initial commit
This commit is contained in:
commit
b18ee8c625
7 changed files with 214 additions and 0 deletions
14
src/main/java/com/kodekloud/hello_demo/HelloController.java
Normal file
14
src/main/java/com/kodekloud/hello_demo/HelloController.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package com.kodekloud.hello_demo;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping("/hello")
|
||||
String hello() {
|
||||
return "Hello, KodeKloud community!";
|
||||
}
|
||||
|
||||
}
|
||||
13
src/main/java/com/kodekloud/hello_demo/MainApplication.java
Normal file
13
src/main/java/com/kodekloud/hello_demo/MainApplication.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package com.kodekloud.hello_demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MainApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MainApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
2
src/main/resources/application.properties
Normal file
2
src/main/resources/application.properties
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
spring.application.name=hello-demo
|
||||
server.port=6767
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package com.kodekloud.hello_demo;
|
||||
|
||||
|
||||
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.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import com.kodekloud.hello_demo.*;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class HelloControllerTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Autowired
|
||||
private HelloController helloController; // Inject HelloController instance
|
||||
|
||||
@Test
|
||||
public void welcome_ok() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(equalTo("Hello, KodeKloud community!")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void welcome_returnsNotNull() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(notNullValue()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void welcome_returnsNotEmpty() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(not(isEmptyString())));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void welcome_returnsCorrectLength() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(hasLength(27)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void welcome_startsWithExpectedGreeting() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(startsWith("Hola")));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void welcome_endsWithExpectedGreeting() throws Exception {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(endsWith("community!")));
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue