내가 JSF 페이지에 내용을 표시이 간단한 CDI 빈을 테스트 : 내가 얻을찾을 수 없습니다 데이터 소스는
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);
}
}
: 나는 자바 메소드를 호출이 JUnit 테스트 케이스를 생성
@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;
}
.............
}
다음 줄의 오류 :
if (ds == null)
{
throw new SQLException();
}
이 문제를 어떻게 해결할 수 있습니까? 테스팅 중에 JUnit 테스트의 데이터 소스를 사용하고 싶습니다. 어떻게 든 JUnit 데이터 소스를 사용할 수 있습니까?
내 코드에 스프링을 사용하지 마십시오. 'SpringJUnit4ClassRunner'를 다른 것으로 대체 할 수 있습니까? –
죄송합니다. Spring MVC를 사용하고 있다고 생각했지만, 제가 언급 한 첫 번째 솔루션을 사용할 수 있습니다. 나는 나의 대답을 편집하여 명확하게했다. – mtrovo
예, 잘 작동합니다. 고맙습니다! –