Reworked to use the Spring Guides' Spring Boot web application example

This commit is contained in:
Erik C. Thauvin 2023-10-22 22:06:49 -07:00
parent 33984c4c0d
commit 3385b72fb5
14 changed files with 143 additions and 72 deletions

View file

@ -1,34 +0,0 @@
package com.example.demo;
import rife.bld.BaseProject;
import java.util.List;
import static rife.bld.dependencies.Repository.*;
import static rife.bld.dependencies.Scope.compile;
import static rife.bld.dependencies.Scope.test;
public class DemoApplicationBuild extends BaseProject {
public DemoApplicationBuild() {
pkg = "com.example.demo";
name = "DemoApplication";
mainClass = "com.example.demo.DemoApplication";
version = version(0,1,0);
repositories = List.of(MAVEN_CENTRAL);
scope(compile)
.include(dependency("org.springframework.boot", "spring-boot-starter",
version(3,1,5)));
scope(test)
.include(dependency("org.springframework.boot", "spring-boot-starter-test",
version(3,1,5)))
.include(dependency("org.junit.jupiter", "junit-jupiter",
version(5,10,0)));
testOperation().mainClass("com.example.demo.DemoApplicationTest");
}
public static void main(String[] args) {
new DemoApplicationBuild().start(args);
}
}

View file

@ -0,0 +1,37 @@
package com.example.springboot;
import rife.bld.BaseProject;
import java.util.List;
import static rife.bld.dependencies.Repository.MAVEN_CENTRAL;
import static rife.bld.dependencies.Scope.compile;
import static rife.bld.dependencies.Scope.test;
public class ApplicationBuild extends BaseProject {
public ApplicationBuild() {
pkg = "com.example.demo";
name = "DemoApplication";
mainClass = "com.example.springboot.Application";
version = version(0, 1, 0);
repositories = List.of(MAVEN_CENTRAL);
scope(compile)
.include(dependency("org.springframework.boot", "spring-boot-starter",
version(3, 1, 5)))
.include(dependency("org.springframework.boot", "spring-boot-starter-actuator",
version(3, 1, 5)))
.include(dependency("org.springframework.boot", "spring-boot-starter-web",
version(3, 1, 5)));
scope(test)
.include(dependency("org.springframework.boot", "spring-boot-starter-test",
version(3, 1, 5)));
testOperation().mainClass("com.example.springboot.HelloControllerIT");
}
public static void main(String[] args) {
new ApplicationBuild().start(args);
}
}

View file

@ -1,11 +0,0 @@
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

View file

@ -0,0 +1,33 @@
package com.example.springboot;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}

View file

@ -0,0 +1,14 @@
package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}

View file

@ -1 +0,0 @@

View file

@ -1,13 +0,0 @@
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTest {
@Test
void contextLoads() {
}
}

View file

@ -0,0 +1,27 @@
package com.example.springboot;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {
@Autowired
private TestRestTemplate template;
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity("/", String.class);
assertThat(response.getBody()).isEqualTo("Greetings from Spring Boot!");
}
public static void main(String[] args) throws Exception {
new HelloControllerIT().getHello();
}
}

View file

@ -0,0 +1,29 @@
package com.example.springboot;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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;
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
}
}