junit - Cannot instantiate class: org.apache.naming.java.javaURLContextFactory -
i'm working on junit test file loads sql file , loads oracle:
import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.sql.connection; import java.sql.sqlexception; import java.sql.statement; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception; import javax.sql.datasource; import oracle.jdbc.pool.oracleconnectionpooldatasource; import org.junit.beforeclass; import org.junit.test; public class oraclecreatescheme1 { public oraclecreatescheme1() { } @beforeclass public static void setupclass() throws exception { // rcarver - setup jndi context , datasource seek { // create initial context system.setproperty(context.initial_context_factory, "org.apache.naming.java.javaurlcontextfactory"); system.setproperty(context.url_pkg_prefixes, "org.apache.naming"); initialcontext ic = new initialcontext(); ic.createsubcontext("java:"); ic.createsubcontext("java:/comp"); ic.createsubcontext("java:/comp/env"); ic.createsubcontext("java:/comp/env/jdbc"); // build datasource oracleconnectionpooldatasource ds = new oracleconnectionpooldatasource(); ds.seturl("jdbc:oracle:thin:@192.168.1.104:1521:oracle"); ds.setuser("admin"); ds.setpassword("qwerty"); ic.bind("java:/comp/env/jdbc/oracle", ds); } grab (namingexception ex) { //logger.getlogger(mydaotest.class.getname()).log(level.severe, null, ex); } } @test public void createoraclescheme() throws sqlexception, namingexception { context initcontext = new initialcontext(); context webcontext = (context) initcontext.lookup("java:/comp/env"); datasource ds = (datasource) webcontext.lookup("jdbc/oracle"); // read file ------------------------------------------------------------------ string s = new string(); stringbuffer sb = new stringbuffer(); seek { filereader fr = new filereader(new file("oraclescheme.sql")); bufferedreader br = new bufferedreader(fr); while ((s = br.readline()) != null) { sb.append(s); } br.close(); // here our splitter ! utilize ";" delimiter each request // sure have formed statements string[] inst = sb.tostring().split(";"); connection c = ds.getconnection(); statement st = c.createstatement(); (int = 0; < inst.length; i++) { // ensure there no spaces before or after request string // in order not execute empty statements if (!inst[i].trim().equals("")) { st.executeupdate(inst[i]); system.out.println(">>" + inst[i]); } } } grab (exception e) { system.out.println("*** error : " + e.tostring()); system.out.println("*** "); system.out.println("*** error : "); e.printstacktrace(); system.out.println("################################################"); system.out.println(sb.tostring()); } } }
when test file problem:
cannot instantiate class: org.apache.naming.java.javaurlcontextfactory
can tell me how can solve problem? , find problem java code?
i fixed adding libraries apache tomcat run-time test libraries.
in netbeans:
project properties -> libraries -> run tests add together jar/folder
the 2 libraries needed catalina.jar
, tomcat-juli.jar
. mileage may vary.
i found them under installation directory tomcat. e.g:
apache-tomcat-7.0.34/bin/tomcat-juli.jar apache-tomcat-7.0.34/lib/catalina.jar
note 1 of jars in bin
directory, other in lib
directory
this isn't best way prepare problem. improve have different way inject datasource
.
java junit
No comments:
Post a Comment