1

我正在为 Jersey WS 编写测试用例并停留在 JNDI 初始化中。获取“javax.naming.NameNotFoundException: Name jdbc is not bound in this Context”异常。

    public class LoginServicesTest extends JerseyTest {


    @Override
    protected Application configure() {
        return new ResourceConfig(LoginServices.class);
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        try {
            // 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");


         // Construct DataSource
            OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
            ds.setURL("jdbc:oracle:thin:@dbs-host.comp.com:1525:USRDEV");
            ds.setUser("DBName");
            ds.setPassword("password");
            ic.bind("java:comp/env", ds);

        } catch (NamingException ex) {ex.printStackTrace();}
    }

    @Test
    public void acceptAgreementTest() {
        Response response = target("/user/acceptAgreement").request().get();
        System.out.println(response);
    }
}

数据源创建代码:

public DataSource getDataSource() {
        DataSource dataSource = null;
        String jndiName = null;
        try {

            jndiName = "jdbc/usrdb";
            Context env = (Context) initialContext.lookup("java:comp/env");
            dataSource = (DataSource) env.lookup(jndiName);
        }catch(Exception e) {
            LOGGER.error("ServiceLocator", e);
            e.printStackTrace();
        }
        return dataSource;
    }

请建议,我如何通过 JNDI 对具有数据库连接的 Jersey WS 进行测试覆盖。感谢任何解决问题的指针。卡得很厉害。

4

0 回答 0