javax.naming.NoInitialContextException:需要指定类

时间:2019-02-14 12:00:23

标签: java eclipse junit jboss

我有一个junit类来测试我的DAO方法。但是,运行后,将出现以下错误:javax.naming.NoInitialContextException:需要在环境或系统属性中或作为applet参数或在应用程序资源文件中指定类名称:java.naming.factory.initial。

从容器中创建了连接,并将其作为参数传递。我该怎么办!

这是班级的延伸:

   ...
        @Test
        public void pesquisarPorIdComConexao() throws SQLException {
            TabCNAEAlvaraAutomaticoDAO DAO = new TabCNAEAlvaraAutomaticoDAO();
            TabCNAEAlvaAutomaticoDto dto = new TabCNAEAlvaAutomaticoDto();
            Connection connection = null;
            try {
                connection = abrir();
                connection.setAutoCommit(false);
                dto = DAO.pesquisarPorId(Long.valueOf("12"), connection);
                assertEquals("Método - pesquisarPorId com Conexão - Com sucesso!",dto != null,dto);

            } catch (Exception ex) {
                ex.printStackTrace();
                fail("Falha no método pesquisarPorId com Conexão ");

            } finally {
                connection.close();
                System.out.println("finalizei");
            }
        }
 // ================== Métodos Auxiliares ======================

    /**
     * Cria uma nova Conexão.
     * @return
     * @throws Exception
     */
    private static Connection abrir() throws Exception {
        Connection conexao = null;
        Class.forName(DRIVER); // Registrar o driver
        conexao = DriverManager.getConnection(URL, USUARIO, SENHA); // Capturar a conexão
        return conexao; // Retorna a conexao aberta
    }

    /**
     * Popula dados para métodos de do DAO.
     * @param dto
     */
    private void carregaDto (TabCNAEAlvaAutomaticoDto dto){
        dto.settCnpjInstRegra("05055009000113");
        dto.settCodCbo("");
        dto.settCodCnae("3314717");
        dto.settFlagCBOCNAE("1");
        dto.settStatus(1);
        dto.settIdSec(Long.valueOf("12"));
    }

0 个答案:

没有答案