我已经在这个问题上工作了几天,这让我发疯了。
我有一段使用 Jboss 4.2.3 的遗留代码。我最近将项目转换为 Maven 进行依赖管理,但是为项目编写的单元测试需要数据源。当我禁用测试时,程序工作得很好,但是当我启用测试时,它会抛出以下内容:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
我在 pom 文件中列出了 jboss 嵌入式依赖项。
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded-all</artifactId>
<version>beta3.SP12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded</artifactId>
<version>beta3.SP12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>hibernate-all</artifactId>
<version>beta3.SP12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>thirdparty-all</artifactId>
<version>beta3.SP12</version>
<scope>test</scope>
</dependency>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>src/test/bootstrap</additionalClasspathElement>
</additionalClasspathElements>
<forkMode>once</forkMode>
<argLine>-Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
<skip>false</skip>
</configuration>
</plugin>
在 src/test/bootstrap/deploy 中,我放置了 dev-ds.xml 文件,该文件正在被容器拾取。
Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=jdbc/theDS' to JNDI name 'java:jdbc/theDS'
我的 dev-ds 文件看起来像这样(有些混淆,请原谅任何拼写错误)
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/theDS</jndi-name>
<connection-url>jdbc:oracle:thin:@xxx.xxx.xxx:1521:DEVDB</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>user</user-name>
<password>password</password>
</local-tx-datasource>
</datasources>
实际测试本身使用 Mockito 来尝试获取数据源。
@BeforeClass
public static void setUpClass() throws Exception
{
if( ! containerRunning )
{
Bootstrap bootstrap = Bootstrap.getInstance();
bootstrap.bootstrap();
containerRunning = true;
}
}
@Before
public void setUp()
{
MyDAO dao = spy( new MyDAO() );
try
{
InitialContext ctx = new InitialContext();
dao.datasource = (DataSource) ctx.lookup( "java:jdbc/theDS" );
Connection conn = dao.datasource.getConnection(); //bang, we're dead
}
例外:
Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Failed to register driver for: oracle.jdbc.driver.OracleDriver; - nested throwable: (java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver)); - nested throwable: (org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Failed to register driver for: oracle.jdbc.driver.OracleDriver; - nested throwable: (java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver)
正如我所提到的,代码在运行时有效,所以我知道 ojdbc6.jar 位于 ${jboss.dir}/server/default/lib 中。但可以肯定的是,我尝试将其添加到构建路径中,并且尝试了 Eclipse 中的“订购和导出”,但均无济于事。我的想法不多了。如果我错过了什么,请告诉我。