Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot find datasource in JUnit tests
    primarykey
    data
    text
    <p>I have this simple CDI bean which displays content into JSF page:</p> <pre><code>@Named("ZonesController") @ViewScoped public class Zones implements Serializable { @Resource(name = "jdbc/Oracle") private DataSource ds; ........... public int countDBRowNum() throws Exception { String SqlStatement = null; if (ds == null) { throw new SQLException(); } Connection conn = ds.getConnection(); if (conn == null) { throw new SQLException(); } PreparedStatement ps = null; ResultSet resultSet = null; int count = 0; try { conn.setAutoCommit(false); boolean committed = false; try { SqlStatement = "SELECT COUNT(1) FROM component x, componentstats y WHERE x.componentstatsid = y.componentstatsid AND y.componenttypeid = 1100"; ps = conn.prepareStatement(SqlStatement); resultSet = ps.executeQuery(); if (resultSet.next()) { count = resultSet.getInt(1); } conn.commit(); committed = true; } finally { if (!committed) { conn.rollback(); } } } finally { ps.close(); conn.close(); } // Returns total rows in table. return count; } ............. } </code></pre> <p>I created this JUnit test case which calls the Java method:</p> <pre><code>public class ZonesTest { @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"); ic.createSubcontext("java:/comp/env/jdbc"); // Construct 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); } catch (NamingException ex) { //Logger.getLogger(MyDAOTest.class.getName()).log(Level.SEVERE, null, ex); } } @Test public void testCountDBRowNum() throws Exception { Zones instance = new Zones(); int rows = instance.countDBRowNum(); System.out.println(rows); } } </code></pre> <p>I get error at these lines:</p> <pre><code>if (ds == null) { throw new SQLException(); } </code></pre> <p>How I can solve this problem? I want to use the datasource from the JUnit test during the testing. Can I somehow use the JUnit datasource?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload