Jakarta ee can';t在J2EE应用程序上执行Junit测试
我已经尝试在我的J2EE应用程序上执行junit测试一周了,但我无法执行。下面是我的代码和控制台错误,显示了我执行测试的位置: 测试代码:Jakarta ee can';t在J2EE应用程序上执行Junit测试,jakarta-ee,junit,null,ejb,Jakarta Ee,Junit,Null,Ejb,我已经尝试在我的J2EE应用程序上执行junit测试一周了,但我无法执行。下面是我的代码和控制台错误,显示了我执行测试的位置: 测试代码: private List<Eleve> listEleves; private Eleve eleve; private GestionEntitiesBean gestionEntitiesBean; @BeforeClass public static void oneTimeSetUp() { // one-time init
private List<Eleve> listEleves;
private Eleve eleve;
private GestionEntitiesBean gestionEntitiesBean;
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
listEleves = new ArrayList<Eleve>();
gestionEntitiesBean=Utils.getGestionEntitiesBean();
System.out.println("@Before - setUp");
eleve=new Eleve();
}
@After
public void tearDown() {
listEleves.clear();
System.out.println("@After - tearDown");
}
@Test
public final void testNewEleve() {
fail("Not yet implemented"); // TODO
}
@Test
public final void testMajEleve() {
fail("Not yet implemented"); // TODO
}
@Test
public final void testDelEleve() {
fail("Not yet implemented"); // TODO
}
@Test
public final void testGetAllEleve() {
// fail("Not yet implemented"); // TODO
listEleves =gestionEntitiesBean.getAllEleve();
assertEquals(1, listEleves.size());
}
@Test
public final void testGetEleveFromId() {
fail("Not yet implemented"); // TODO
}
最后是错误控制台:
@BeforeClass - oneTimeSetUp
@Before - setUp
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.asl.groupb.utils.Utils.getGestionEntitiesBean(Utils.java:24)
at org.asl.groupb.repositoriesTest.EleveRepositoryTest.setUp(EleveRepositoryTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
@After - tearDown
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.asl.groupb.utils.Utils.getGestionEntitiesBean(Utils.java:24)
at org.asl.groupb.repositoriesTest.EleveRepositoryTest.setUp(EleveRepositoryTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
@Before - setUp
@After - tearDown
@Before - setUp
@After - tearDown
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.asl.groupb.utils.Utils.getGestionEntitiesBean(Utils.java:24)
at org.asl.groupb.repositoriesTest.EleveRepositoryTest.setUp(EleveRepositoryTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.asl.groupb.utils.Utils.getGestionEntitiesBean(Utils.java:24)
at org.asl.groupb.repositoriesTest.EleveRepositoryTest.setUp(EleveRepositoryTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
@Before - setUp
@After - tearDown
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.asl.groupb.utils.Utils.getGestionEntitiesBean(Utils.java:24)
at org.asl.groupb.repositoriesTest.EleveRepositoryTest.setUp(EleveRepositoryTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
@Before - setUp
@After - tearDown
@AfterClass - oneTimeTearDown
问题是JUnit从JavaSE层驱动测试,因此执行测试的线程不会在JavaEE容器中执行。正如您所发现的,许多需要Java EE容器的Java EE操作将不可用,例如
InitialContext.lookup()
以下是我在Java EE环境中执行JUnit测试时使用的一种相当标准的方法:
使用带有基本测试存根的JUnit SE套件来驱动对应用程序的GET请求:
@BeforeClass
public static void oneTimeSetUp() {
// start my app server and make sure apps are installed
}
@AfterClass
public static void oneTimeTearDown() {
// shut down app server
}
@Test
public void testApplicationAvailable() {
runTestInServlet("testApplicationAvailable");
}
@Test
public void testThatFails() {
runTestInServlet("testThatFails");
}
private void runTestInServlet(String testName) {
// Construct the URL, note that the name of the test is passed in
// as the value of the "test" parameter. This will get read from
// the servlet's doGet method
URL url = new URL("http://localhost:9080/myAppName/testServletName?test=" + test);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
try{
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// use the BufferedReader to validate response from test servlet
// i.e. search for some "successful test" token
} finally {
con.disconnect();
}
}
@WebServlet(name = "TestServlet", urlPatterns = { "/testServletName" })
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String test = request.getParameter("test");
PrintWriter out = response.getWriter();
try {
System.out.println("-----> " + test + " starting");
// Use reflection to branch out and invoke the method found in the GET method's "test" parameter
getClass().getMethod(test, PrintWriter.class).invoke(this, response.getWriter());
System.out.println("<----- " + test + " successful");
} catch (Throwable x) {
System.out.println("<----- " + test + " failed:");
} finally {
out.flush();
out.close();
}
}
public void testApplicationAvailable(PrintWriter pw) throws Exception {
System.out.prinln("Application and test servlet available");
}
public void testThatFails(PrintWriter pw) throws Exception {
throw new Exception("This test will fail");
}
}
在测试servlet中使用相应的方法在JavaEE环境中执行真正的测试:
@BeforeClass
public static void oneTimeSetUp() {
// start my app server and make sure apps are installed
}
@AfterClass
public static void oneTimeTearDown() {
// shut down app server
}
@Test
public void testApplicationAvailable() {
runTestInServlet("testApplicationAvailable");
}
@Test
public void testThatFails() {
runTestInServlet("testThatFails");
}
private void runTestInServlet(String testName) {
// Construct the URL, note that the name of the test is passed in
// as the value of the "test" parameter. This will get read from
// the servlet's doGet method
URL url = new URL("http://localhost:9080/myAppName/testServletName?test=" + test);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
try{
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// use the BufferedReader to validate response from test servlet
// i.e. search for some "successful test" token
} finally {
con.disconnect();
}
}
@WebServlet(name = "TestServlet", urlPatterns = { "/testServletName" })
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String test = request.getParameter("test");
PrintWriter out = response.getWriter();
try {
System.out.println("-----> " + test + " starting");
// Use reflection to branch out and invoke the method found in the GET method's "test" parameter
getClass().getMethod(test, PrintWriter.class).invoke(this, response.getWriter());
System.out.println("<----- " + test + " successful");
} catch (Throwable x) {
System.out.println("<----- " + test + " failed:");
} finally {
out.flush();
out.close();
}
}
public void testApplicationAvailable(PrintWriter pw) throws Exception {
System.out.prinln("Application and test servlet available");
}
public void testThatFails(PrintWriter pw) throws Exception {
throw new Exception("This test will fail");
}
}
@WebServlet(name=“TestServlet”,urlPatterns={”/testServletName})
公共类TestServlet扩展了HttpServlet{
@凌驾
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
字符串测试=request.getParameter(“测试”);
PrintWriter out=response.getWriter();
试一试{
System.out.println(“-->”+测试+启动”);
//使用反射分支并调用GET方法的“test”参数中的方法
getClass().getMethod(test,PrintWriter.class).invoke(this,response.getWriter());
System.out.println(“问题是JUnit从Java SE层驱动测试,因此执行测试的线程将不会在Java EE容器中执行。正如您所发现的,许多需要Java EE容器的Java EE操作将不可用,例如InitialContext.lookup()
以下是我在Java EE环境中执行JUnit测试时使用的一种相当标准的方法:
使用带有基本测试存根的JUnit SE套件来驱动对应用程序的GET请求:
@BeforeClass
public static void oneTimeSetUp() {
// start my app server and make sure apps are installed
}
@AfterClass
public static void oneTimeTearDown() {
// shut down app server
}
@Test
public void testApplicationAvailable() {
runTestInServlet("testApplicationAvailable");
}
@Test
public void testThatFails() {
runTestInServlet("testThatFails");
}
private void runTestInServlet(String testName) {
// Construct the URL, note that the name of the test is passed in
// as the value of the "test" parameter. This will get read from
// the servlet's doGet method
URL url = new URL("http://localhost:9080/myAppName/testServletName?test=" + test);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
try{
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// use the BufferedReader to validate response from test servlet
// i.e. search for some "successful test" token
} finally {
con.disconnect();
}
}
@WebServlet(name = "TestServlet", urlPatterns = { "/testServletName" })
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String test = request.getParameter("test");
PrintWriter out = response.getWriter();
try {
System.out.println("-----> " + test + " starting");
// Use reflection to branch out and invoke the method found in the GET method's "test" parameter
getClass().getMethod(test, PrintWriter.class).invoke(this, response.getWriter());
System.out.println("<----- " + test + " successful");
} catch (Throwable x) {
System.out.println("<----- " + test + " failed:");
} finally {
out.flush();
out.close();
}
}
public void testApplicationAvailable(PrintWriter pw) throws Exception {
System.out.prinln("Application and test servlet available");
}
public void testThatFails(PrintWriter pw) throws Exception {
throw new Exception("This test will fail");
}
}
在测试servlet中使用相应的方法在JavaEE环境中执行真正的测试:
@BeforeClass
public static void oneTimeSetUp() {
// start my app server and make sure apps are installed
}
@AfterClass
public static void oneTimeTearDown() {
// shut down app server
}
@Test
public void testApplicationAvailable() {
runTestInServlet("testApplicationAvailable");
}
@Test
public void testThatFails() {
runTestInServlet("testThatFails");
}
private void runTestInServlet(String testName) {
// Construct the URL, note that the name of the test is passed in
// as the value of the "test" parameter. This will get read from
// the servlet's doGet method
URL url = new URL("http://localhost:9080/myAppName/testServletName?test=" + test);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
try{
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// use the BufferedReader to validate response from test servlet
// i.e. search for some "successful test" token
} finally {
con.disconnect();
}
}
@WebServlet(name = "TestServlet", urlPatterns = { "/testServletName" })
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String test = request.getParameter("test");
PrintWriter out = response.getWriter();
try {
System.out.println("-----> " + test + " starting");
// Use reflection to branch out and invoke the method found in the GET method's "test" parameter
getClass().getMethod(test, PrintWriter.class).invoke(this, response.getWriter());
System.out.println("<----- " + test + " successful");
} catch (Throwable x) {
System.out.println("<----- " + test + " failed:");
} finally {
out.flush();
out.close();
}
}
public void testApplicationAvailable(PrintWriter pw) throws Exception {
System.out.prinln("Application and test servlet available");
}
public void testThatFails(PrintWriter pw) throws Exception {
throw new Exception("This test will fail");
}
}
@WebServlet(name=“TestServlet”,urlPatterns={”/testServletName})
公共类TestServlet扩展了HttpServlet{
@凌驾
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
字符串测试=request.getParameter(“测试”);
PrintWriter out=response.getWriter();
试一试{
System.out.println(“-->”+测试+启动”);
//使用反射分支并调用GET方法的“test”参数中的方法
getClass().getMethod(test,PrintWriter.class).invoke(this,response.getWriter());
System.out.println(“我建议您使用它进行单元测试,它要简单得多
请使用下面的stackoverflow帖子了解在没有容器的情况下使用EntityManager的基本用法和解决方案
我建议您使用它进行单元测试,它要简单得多
请使用下面的stackoverflow帖子了解在没有容器的情况下使用EntityManager的基本用法和解决方案
可能重复@Vladimitikhomirov我不认为这是一个dup。根本问题是在EE容器中运行测试,问题比NoInitialContextException
更广泛。可能重复@Vladimitikhomirov我不认为这是一个dup。根本问题是在EE容器中运行测试,问题是em比NoInitialContextException
更广泛。