Initial commit

This commit is contained in:
tben1981 2025-12-02 01:37:52 +08:00
commit b18ee8c625
7 changed files with 214 additions and 0 deletions

View 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!";
}
}

View 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);
}
}

View file

@ -0,0 +1,2 @@
spring.application.name=hello-demo
server.port=6767

View file

@ -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!")));
}
}