Jakarta ee OpenEJB-为JUnit配置数据源

Jakarta ee OpenEJB-为JUnit配置数据源,jakarta-ee,persistence,openejb,Jakarta Ee,Persistence,Openejb,我对OpenEJB 3.1.3的数据源的正确配置有问题。我尝试配置到postgres db的连接,但当我调试测试时,我会得到默认的hsql连接参数 这是我的测试课: @RunWith(ApplicationComposer.class) public class OpenEJBTest { @EJB Files fb; @Test public void testSth() { List<UploadSession> uploadNo

我对OpenEJB 3.1.3的数据源的正确配置有问题。我尝试配置到postgres db的连接,但当我调试测试时,我会得到默认的hsql连接参数

这是我的测试课:

@RunWith(ApplicationComposer.class)
public class OpenEJBTest {
    @EJB
    Files fb;

    @Test
    public void testSth() {
        List<UploadSession> uploadNotFinishedSessions = fb.getUploadNotFinishedSessions();
    }

    @Module
    public EjbJar beans() {
        EjbJar ejbJar = new EjbJar("beans");
        ejbJar.addEnterpriseBean(new StatelessBean(FilesBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(FilesUtilBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(ServerFilesBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(UserUtilBean.class));
        return ejbJar;
    }

    @Module
    public PersistenceUnit persistence() {
        PersistenceUnit unit = new PersistenceUnit("postgresPU", HibernatePersistence.class.getName());
        String simpleXml = SimpleXmlUtil.toSimpleXml(unit);
        System.out.println(simpleXml);
        unit.setJtaDataSource("PostgresDS");
        unit.setNonJtaDataSource("PostgresDSUnmanaged");
        return unit;
    }
}
  • 通过openejb.xml(位于类路径上)配置数据源

  • 我可能做错了什么?

    ApplicationComposer没有使用JNDI来引导容器,因此永远看不到
    JNDI.properties
    文件

    相反,您可以在方法上使用
    org.apache.openejb.junit.Configuration
    注释,让它返回您想要用于配置测试的属性

    @Configuration
    public Properties properties() {
       //...
    }
    
    我将
    jndi.properties
    重命名为其他名称,这样就不会混淆,然后您可以使用类似这样的代码在类路径中找到它

       final URL url = this.getClass().getResource("/config.properties");
       final Properties props = new Properties();
       final InputStream in = url.openStream();
       try {
           props.load(in);
       } finally {
           close(in);
       }
    
    WARN - SQL Error: -22, SQLState: S0002
    ERROR - Table not found in statement [select top ? user0_.id as col_0_0_ from tusers user0_ where user0_.login=?]
    
    @Configuration
    public Properties properties() {
       //...
    }
    
       final URL url = this.getClass().getResource("/config.properties");
       final Properties props = new Properties();
       final InputStream in = url.openStream();
       try {
           props.load(in);
       } finally {
           close(in);
       }