#394 update Spring Boot demo的测试方法
This commit is contained in:
+4
-3
@@ -2,12 +2,13 @@
|
||||
|
||||
It shows the mainstream technologies and pragmatic practice in JavaEE world.
|
||||
|
||||
It has 2 major examples:
|
||||
It has 4 major examples:
|
||||
|
||||
1. Quickstart -- a minimal CRUD Todo-List web application.
|
||||
2. Showcase -- demonstrate many other interesting technologies.
|
||||
3. BootService -- a Spring Boot based WebService application.
|
||||
4. BootWeb -- a Spring Boot based Web application(TODO).
|
||||
|
||||
-------------------------------
|
||||
Offical Site: http://springside.github.io
|
||||
Document: https://github.com/springside/springside4/wiki
|
||||
CI Status: [](https://travis-ci.org/springside/springside4)
|
||||
Document: https://github.com/springside/springside4/wiki
|
||||
@@ -0,0 +1,66 @@
|
||||
Things not in demo
|
||||
|
||||
1. use applicactionContextxml
|
||||
|
||||
@ImportResource("applicationContext.xml")
|
||||
public class BootServiceApplication {
|
||||
}
|
||||
|
||||
|
||||
2. use Jetty instead of Tomcat
|
||||
|
||||
3. disable web in junit test use spring context
|
||||
|
||||
setWebEnvironment(false)
|
||||
|
||||
4. set system exit code
|
||||
|
||||
org.springframework.boot.ExitCodeGenerator
|
||||
|
||||
5. read configuration
|
||||
@Value(${name})
|
||||
String name
|
||||
|
||||
or
|
||||
|
||||
@Autowired
|
||||
Environment env;
|
||||
env.get
|
||||
|
||||
6. set the configuration file path
|
||||
|
||||
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
|
||||
|
||||
or just change the file name
|
||||
|
||||
java -jar myproject.jar --spring.config.name=myproject
|
||||
|
||||
7. active profile
|
||||
java -jar myproject.jar --spring.profiles.active=dev,hsqldb
|
||||
|
||||
SpringApplication.setAdditionalProfiles(…)
|
||||
|
||||
8. define logging level in appliation.properties
|
||||
|
||||
logging.level.org.springframework.web: DEBUG
|
||||
logging.level.org.hibernate: ERROR
|
||||
|
||||
9. add spring-mvc defenation
|
||||
wtrite WebMvcConfigurerAdapter without @EnableWebMvc
|
||||
|
||||
10. add servlet/filter
|
||||
|
||||
in application.properties
|
||||
|
||||
ServletRegistrationBean or FilterRegistrationBean
|
||||
|
||||
11. configure web
|
||||
|
||||
add context path
|
||||
server.contextPath
|
||||
|
||||
set tomcat threads
|
||||
|
||||
12. JMX
|
||||
|
||||
@ManagedResource, @ManagedAttribute, @ManagedOperation
|
||||
@@ -12,6 +12,11 @@
|
||||
<properties>
|
||||
<springside.version>4.3.0-SNAPSHOT</springside.version>
|
||||
<spring-boot.version>1.1.8.RELEASE</spring-boot.version>
|
||||
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.6</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -93,20 +98,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
|
||||
+2
-1
@@ -7,7 +7,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
// Spring Java Config的标识
|
||||
@Configuration
|
||||
@ComponentScan("org.springside.examples.bootservice")
|
||||
// Spring 默认扫描ClassPath中的Component,以本类所在地package为起点。
|
||||
@ComponentScan
|
||||
// Spring Boot的AutoConfig
|
||||
@EnableAutoConfiguration
|
||||
public class BootServiceApplication {
|
||||
|
||||
+22
-27
@@ -20,46 +20,41 @@ import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
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.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springside.examples.bootservice.BootServiceApplication;
|
||||
import org.springside.examples.bootservice.domain.Task;
|
||||
import org.springside.examples.bootservice.domain.User;
|
||||
import org.springside.modules.test.data.RandomData;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = BootServiceApplication.class)
|
||||
@WebAppConfiguration
|
||||
@IntegrationTest("server.port=0")
|
||||
@DirtiesContext
|
||||
public class TaskRestServiceTest {
|
||||
private static ConfigurableApplicationContext context;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
private String resourceUrl = "http://localhost:8080/task";
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
@BeforeClass
|
||||
public static void start() throws Exception {
|
||||
Future<ConfigurableApplicationContext> future = Executors.newSingleThreadExecutor().submit(
|
||||
new Callable<ConfigurableApplicationContext>() {
|
||||
private RestTemplate restTemplate;
|
||||
private String resourceUrl;
|
||||
|
||||
public ConfigurableApplicationContext call() throws Exception {
|
||||
return SpringApplication.run(BootServiceApplication.class);
|
||||
}
|
||||
});
|
||||
context = future.get(60, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stop() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
@Before
|
||||
public void setup() {
|
||||
restTemplate = new TestRestTemplate();
|
||||
resourceUrl = "http://localhost:" + port + "/task";
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user