好的,我让 GlassFish 基础设施正常工作,并且能够成功地为一个简单的 servlet 和一个简单的 JAX-RS 服务创建测试。花了一些时间来解决这个问题,所以我会在这里分享以防其他人可以使用它。我还没有深入研究 JPA 测试,但一步一步来。我是 StackOverflow 的新手,所以我不知道在答案中共享大量代码的公认协议,但这里有:如何启动一个为简单 servlet 和 JAX-RS 资源提供服务的 GlassFish 嵌入式实例,然后测试他们。包裹省略。嵌入式 javadocs:http ://embedded-glassfish.java.net/nonav/apidocs/
1. 配置 JAX-RS:
package org.netbeans.rest.application.config;
@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends javax.ws.rs.core.Application {
}
2.定义资源:
包rest;
@Path("generic")
public class GenericResource {
public static final String MESSAGE = "hi there";
public GenericResource() {
}
@GET @Produces(MediaType.TEXT_PLAIN)
public String sayHi() {
return MESSAGE;
}
}
3. 定义小服务程序:
package servlet;
@WebServlet(urlPatterns = {"/hello"})
public class HelloWebServlet extends HttpServlet {
public HelloWebServlet() {
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
PrintWriter pw = res.getWriter();
try {
pw.println(GenericResource.MESSAGE);
} catch (Exception ex) {
e.printStackTrace();
}
}
}
4. 定义测试,使用 Jersey REST 客户端,JUnit 4,http://download.java.net/maven/glassfish/org/glassfish/extras/glassfish-embedded-all/3.1.1/glassfish-embedded-all -3.1.1.jar
package rest;
public class NewServletTest {
private static final Logger LOG = Logger.getLogger(NewServletTest.class.getCanonicalName());
private static GlassFish glassfish = null;
private static final String WEB_APP_NAME = "RestTemp";
private static final String BASE_URI = "http://localhost:" + 8080 + "/" + WEB_APP_NAME;
private static final String REST_URI = BASE_URI + "/" + "resources" + "/" + "generic";
public NewServletTest() {
}
@BeforeClass
public static void startServer() {
try {
GlassFishProperties gfProps = new GlassFishProperties();
gfProps.setPort("http-listener", 8080); // NB: not sure where name comes from - a standard property?
glassfish = GlassFishRuntime.bootstrap().newGlassFish(gfProps);
glassfish.start();
Deployer deployer = glassfish.getDeployer();
ScatteredArchive archive = new ScatteredArchive(WEB_APP_NAME, ScatteredArchive.Type.WAR);
File buildDir = new File("build", "classes"); // NB: location depends on IDE setup
archive.addClassPath(buildDir);
deployer.deploy(archive.toURI());
} catch (GlassFishException ex) {
LOG.log(Level.SEVERE, null, ex);
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
@AfterClass
public static void shutDownServer() {
if (glassfish != null) {
try {
glassfish.stop();
glassfish.dispose();
glassfish = null;
} catch (GlassFishException ex) {
LOG.log(Level.SEVERE, "tearDownClass(): exception: ", ex);
}
}
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testPing() throws MalformedURLException, IOException {
URL url = new URL(BASE_URI + "/hello");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
InputStream inputStream = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
assertEquals(GenericResource.MESSAGE, br.readLine());
}
@Test
public void testGet() {
WebResource r = Client.create().resource(REST_URI);
ClientResponse cr = r.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); // GET
String crEntityStr = cr.getEntity(String.class);
ClientResponse.Status crStatus = cr.getClientResponseStatus();
assertEquals(GenericResource.MESSAGE, crEntityStr);
assertEquals(ClientResponse.Status.OK, crStatus);
}
}
要在 NetBeans 中创建此项目:
- 选择新建项目 > Java 应用程序。采用默认值
- 删除 JavaApplicationTemp
- 使用 NetBeans 在 Source Packages IDE 文件夹中创建上述文件(将进入 src/):
- org.netbeans.rest.application.config.ApplicationConfig
- rest.GenericResource
- servlet.HelloWebServlet
- 选择新建 > JUnit 测试。取默认值,选择 JUnit 4.x
- 删除 NewEmptyJUnitTest
- 使用 NetBeans 在 Test Packages IDE 文件夹中创建上述文件(将进入 test/):
- 编辑项目的属性并将 glassfish-embedded-all-3.1.1.jar 添加到 Libraries > Compile 类别
- 选择运行 > 测试项目。理想情况下,您会看到三个通过的测试!
希望有帮助!