这是来自新泽西州的标准行为和允许基于servlet的功能,例如HttpServletRequest的还不可用。根据我的研究, 我能达到以下条件
- Intiailize测试容器只有一次整个测试类
- 注册的监听器,过滤器的servlet等,以测试 容器
- 确保@Context HttpServletRequest不为空
通过启动/停止手动灰熊容器并在基于自定义球衣的WebappContext上部署灰熊容器实例。具体步骤按照 情况下,任何人都碰到过这样的问题
- 创建的HttpServer
- 在@Before创建自定义WebappContext这反映了你的 的web.xml和使用部署HttpServer的实例在WebappContext
- 为了确保基于Servlet的功能,如 的Http ServletRequest中可在集成测试使用ServletRegistration加载一个Servlet容器内部应用程序如下图所示
第1步
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class ResourceEndpointIntegrationTest{
@Context
private HttpServletRequest httpReq;
private static Logger logger = Logger.getLogger(ResourceEndpointIntegrationTest.class);
public static final String BASE_URI = "http://localhost:8989/";
private static HttpServer server = null;
@BeforeClass
public static void initTest() {
RestAssured.baseURI = "http://localhost:8989/";
}
...
}
使用SpringJUnit4ClassRunner.class和@ContextConfiguration加载applicationContext.xml进行测试。同时声明@Context HttpServletRequest并创建 HttpServer的一个实例稍后使用。我在此处使用@BeforeClass以用于Rest-Assured的特定目的(您不必使用它),它是可选的。
步骤#2
@Before
public void setup() throws Exception {
if (server == null) {
System.out.println("Initializing an instance of Grizzly Container ...");
final ResourceConfig rc = new ResourceConfig(ResourceEndpointIntegrationTest.class, ..., ..., ...); //update
WebappContext ctx = new WebappContext("IntegrationTestContext");
//register your listeners from web.xml in here
ctx.addListener("com.xxx.yyy.XEndpointServletContextListener");
//register your applicationContext.xml here
ctx.addContextInitParameter("contextConfigLocation", "classpath:applicationContext.xml");
//ServletRegistration is needed to load the ResourceConfig rc inside ServletContainer or you will have no
//Servlet-based features available
ServletRegistration registration = ctx.addServlet("ServletContainer",
new ServletContainer(rc));
//Initialize the Grizzly server passing it base URL
server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));
//Deploy the server using our custom context
ctx.deploy(server);
}
}
的设置在@Before运行对每个测试,但,如果条件将迫使设置方法表现得像@BeforeClass允许我们初始化 服务器一次为整个测试类减去静态性质@BeforeClass。
我之所以初始化服务器一次测试类中所有的测试是因为,如果我们不那么我们将有
- 启动服务器时以下工作流程
- 春天的开始扫描豆类
- 春季自动布线
- 春季布置sessionFactory等
- 测试运行
- 春天破坏的背景下
- 服务器关闭
- 重复步骤#1至#7的每个测试
上面是非常耗时,技术上并不可行,因此初始化容器一次(这就是为什么我不扩展它的原因是我想控制测试容器的启动/关闭)。
#步骤3
@AfterClass
public static void tearDown() throws Exception {
System.out.println("Integration tests completed. Tear down the server ...");
if (server != null && server.isStarted()) {
server.shutdownNow();
System.out.println("Grizzly instance shutdown completed");
}
}
在步骤3中,我们使用@AfterClass关机用于集成测试的目的灰熊实例。
进行了抽样检测看起来遵循
@Test
public void testCreateSomethingReturnSuccessfully() {
JSONObject something = new JSONObject();
cust.put("name", "integrationTest");
cust.put("age", 33);
given().
contentType(ContentType.JSON).
body(something.toString()).post("/someEndpoint").
then().
statusCode(200).
assertThat().
body("id", greaterThan(0)).
body("name", equalTo("integrationTest")).
body("age", equalTo(33));
}
(摇篮)一些培训相关的depdendencies
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.23.2'
compile group: 'org.glassfish.jersey.test-framework.providers', name:'jersey-test-framework-provider-grizzly2', version:'2.23.2'
compile group: 'org.springframework', name:'spring-test', version:'4.3.2.RELEASE'
compile group: 'io.rest-assured', name:'rest-assured', version:'3.0.1'
// Spring
compile group: 'org.springframework', name: 'spring-core', version: '4.3.2.RELEASE'
compile group: 'org.springframework', name: 'spring-beans', version: '4.3.2.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '4.3.2.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.2.RELEASE'
compile group: 'org.springframework', name: 'spring-orm', version: '4.3.2.RELEASE'
// Jersey-Spring bridge
compile (group: 'org.glassfish.jersey.ext', name: 'jersey-spring3', version: '2.23.2'){
exclude group: 'org.springframework', module: 'spring-core'
exclude group: 'org.springframework', module: 'spring-web'
exclude group: 'org.springframework', module: 'spring-beans'
exclude group: 'org.springframework', module: 'spring-jdbc'
exclude group: 'org.springframework', module: 'spring-orm'
}
一些进口
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;