我使用Hello! project使用spring-boot运行一个小spring-loaded。它似乎工作,但我不知道如何实际编写测试,测试类在更改后重新加载。
我可以打开intellij-idea中的代码,用gradle启动它,在java方法中进行更改,编译类并成功(?)而不重新启动jetty参见
spring-boot-loaded [bootRun]: 1 class reloaded
如果我重新加载浏览器,则会显示更改。我可以在IntelliJ中手动热交换的代码是
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings FOO from Spring Boot!"; //I can hotswap this output if I recompile
}
}
如果我编译代码,我可以运行它并在IntelliJ IDEA中手动热交换java代码。我从CLI运行它的方式是gradle bootRun
,它是文档中reference project的副本。我不知道如何在Ubuntu中从CLI运行测试。
IIUC,测试不测试重新加载,只测试它正在运行。我发现test in spring loaded repo用于方法重新加载,但它看起来很旧很多文件。我可以从命令行简单地使用它,或者它只是IDE,例如IntelliJ IDEA支持hotswap?
我可以编写测试重新加载的测试吗?
我的测试代码是
HelloControllerIT
package hello;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest({"server.port=0"})
public class HelloControllerIT {
@Value("${local.server.port}")
private int port;
private URL base;
private RestTemplate template;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
template = new TestRestTemplate();
}
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
}
}
HelloControllerTest
package hello;
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.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class HelloControllerTest {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@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!")));
}
}
的build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE"
classpath 'org.springframework:springloaded:1.2.6.RELEASE'
classpath 'io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'rebel'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.zeroturnaround', name: 'gradle-jrebel-plugin', version: '1.1.3'
}
}
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit")
}
// change default IntelliJ output directory for compiling classes
idea {
module {
inheritOutputDirs = false
outputDir = file("$buildDir/classes/main/")
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}