I run a small Hello! project using spring-boot with spring-loaded. It seems to work but I'm not sure how to actually write a test that tests that the classes are reloading after a change.
I can open the code in intellij-idea, start it with gradle, make a change in a java method, compile the class and successfully(?) without restarting
jetty see
spring-boot-loaded [bootRun]: 1 class reloaded
and my change appears if I reload the browser. The code I can hotswap manually in IntelliJ is
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings FOO from Spring Boot!"; //I can hotswap this output if I recompile
}
}
I can run it and manually hotswap java code in IntelliJ IDEA, if I compile the code. The way I run it from CLI is gradle bootRun
and it is a copy of the reference project from the docs. I don't know how to run the test from the CLI in Ubuntu.
IIUC, the tests don't test reloading, only that it is running. I found test in spring loaded repo for method reloading but it seems old and many files. Can I make it simple and from the command line or is it only IDEs e.g. IntelliJ IDEA that supports hotswap?
Can I write a test that tests that reloading is working?
My test codes are
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'
}