Skip to content
Snippets Groups Projects
Commit ee1c0d54 authored by Hau Ha's avatar Hau Ha
Browse files

add mvc file

parent 0934f0e5
Branches
No related tags found
No related merge requests found
Pipeline #77246 passed
{
"java.configuration.updateBuildConfiguration": "interactive"
}
\ No newline at end of file
File added
...@@ -29,11 +29,22 @@ ...@@ -29,11 +29,22 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
......
package com.example.demo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
public class DemoApplication {
//test
@GetMapping("/")
public String home() {
return "Spring is here!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
class SiteGen {
static String siteTitle = "My favorite movies";
static List<String> movieList = List.of(
"Star Wars",
"Star Trek",
"Battlestar Galactica"
);
static String htmlString =
"<html>" +
"<head><title>" + siteTitle + "</title></head>" +
"<body>" +
"<h1>" + siteTitle + "</h1>" +
"<ul>" + movieList.stream().map(i -> "<li>"+ i + "</li>").collect(Collectors.joining()) + "</ul>" +
"</body>" +
"<html>";
public static void main(String args[]) throws Exception {
Files.createDirectory(Paths.get("public/"));
Files.write(Paths.get("public/index.html"), htmlString.getBytes());
}
}
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class DemoApplicationTests {
@Test
void contextLoads() {
}
@Autowired
private TestRestTemplate restTemplate;
@Test
void homeResponse() {
String body = this.restTemplate.getForObject("/", String.class);
assertEquals("Spring is here!", body);
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SiteGenTest {
@Test
public void testHtmlString() {
String correctHtml = "<html><head><title>My favorite movies</title></head><body><h1>My favorite movies</h1><ul><li>Star Wars</li><li>Star Trek</li><li>Battlestar Galactica</li></ul></body></html>";
Assertions.assertEquals(correctHtml, SiteGen.htmlString);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment