Skip to content

Commit

Permalink
springside#394 update Spring Boot demo的测试方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin committed Oct 12, 2014
1 parent 1073c07 commit 429cdce
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 45 deletions.
7 changes: 4 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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: [![Build Status](https://travis-ci.org/springside/springside4.svg?branch=master)](https://travis-ci.org/springside/springside4)
Document: https://github.com/springside/springside4/wiki
66 changes: 66 additions & 0 deletions examples/bootservice/README.txt
Original file line number Diff line number Diff line change
@@ -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
19 changes: 5 additions & 14 deletions examples/bootservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

// Spring Java Config的标识
@Configuration
@ComponentScan("org.springside.examples.bootservice")
// Spring 默认扫描ClassPath中的Component,以本类所在地package为起点。
@ComponentScan
// Spring Boot的AutoConfig
@EnableAutoConfiguration
public class BootServiceApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,41 @@

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
Expand Down

0 comments on commit 429cdce

Please sign in to comment.