Mat*_*ell 4 jax-rs java-ee javadb glassfish-embedded
我是比较新的JAX-RS,JPA,GlassFish的,Java DB的,和NetBeans,我想编写单元测试我的代码.[版本号在底部.]但是,我坚持从哪里开始.我已经做了很多搜索,但我还没有清楚地了解如何设置我的代码的嵌入式测试.我正在使用NetBeans,但我的问题很笼统.我希望我能更清楚地形成我的问题,但这是我能做的最好的事情.到目前为止,我已经找到了以下可能的部分(更像是此时的提示).
o我想在没有Maven的情况下进行设置,但这意味着我必须手动安装嵌入式jar.问:我在哪里可以找到它们?
o创建指定GlassFish和Java DB嵌入版本的config xml文件版本(glassfish-resources.xml和persistence.xml).问:但是,您如何告诉NetBeans使用它们进行测试而不是依赖于已安装版本的生产?
我认为persistence.xml看起来像这样(从使用嵌入式德比的hibernate):
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:test"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
Run Code Online (Sandbox Code Playgroud)
o创建自定义Glassfish域配置文件(Embedded GlassFish忽略Maven测试资源).问:这应该是什么样的?我从使用NetBeans安装创建的默认domain1中获取了domain.xml,但它有很多内容.
o一旦我的项目可以访问嵌入的文件,并且它被配置为使用它们,我的JUnit线束应该是什么样的?http://jersey.java.net/nonav/documentation/latest/client-api.html#d4e759说:
protected void setUp() throws Exception {
...
glassfish = new GlassFish(BASE_URI.getPort());
ScatteredWar war = new ScatteredWar(...);
glassfish.deploy(war);
...
Run Code Online (Sandbox Code Playgroud)
但是,我也看到提到的EJBContainer,例如,(从http://docs.oracle.com/javaee/6/tutorial/doc/gkcqz.html):
@BeforeClass
public static void initContainer() throws Exception {
ec = EJBContainer.createEJBContainer(); ctx = ec.getContext();
}
Run Code Online (Sandbox Code Playgroud)
o我正在使用JPA,所以我需要访问PersistenceContext/EntityManager.目前我通过以下方式查询:
new InitialContext().lookup("java:module/<jax-rs resource name>");
Run Code Online (Sandbox Code Playgroud)
但我也看到了:
emf = Persistence.createEntityManagerFactory("chapter02PU");
Run Code Online (Sandbox Code Playgroud)
问:什么是正确的方法来掌握这个?
我将衷心感谢您的帮助.
好的,我使GlassFish基础设施正常工作,并且能够成功地为简单的servlet和简单的JAX-RS服务创建测试.我花了一些时间来解决这个问题,所以我会在这里分享以防其他人可以使用它.我还没有深入研究JPA测试,但一步一步.我是新来的StackOverflow,所以我不知道在回答中共享了大量的代码被接受的协议,但在这里不用:如何引导GlassFish的嵌入式实例提供一个简单的Servlet和JAX-RS的资源,然后测试他们.包省略.嵌入式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 {
}
Run Code Online (Sandbox Code Playgroud)
2.定义资源: 包休息;
@Path("generic")
public class GenericResource {
public static final String MESSAGE = "hi there";
public GenericResource() {
}
@GET @Produces(MediaType.TEXT_PLAIN)
public String sayHi() {
return MESSAGE;
}
}
Run Code Online (Sandbox Code Playgroud)
3.定义servlet:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
要在NetBeans中创建此项目:
希望有所帮助!