Java SpringMVC测试,JNDI

Java SpringMVC测试,JNDI,java,spring,spring-mvc,jndi,spring-test,Java,Spring,Spring Mvc,Jndi,Spring Test,我想为我的测试将现有数据源绑定到JNDI名称 @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = { "file:WebContent/WEB-INF/application-context.xml", "file:WebContent/WEB-INF/test-datasource.xml" }) public class Simulator

我想为我的测试将现有数据源绑定到JNDI名称

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "file:WebContent/WEB-INF/application-context.xml",
        "file:WebContent/WEB-INF/test-datasource.xml" })
public class SimulatorTest {

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @PostConstruct
    public void createContext() throws NamingException {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        builder.bind("java:comp/env/jdbc/DefaultDB", dataSource);
        builder.activate();
    }
...
这似乎可行,但只要我的代码(即EclipseLink)尝试查找此JNDI名称,它就会失败:

Exception Description: Cannot acquire data source [java:comp/env/jdbc/DefaultDB].
Internal Exception: javax.naming.OperationNotSupportedException: SimpleNamingContext does not support [javax.naming.Name]
我看过源代码,但它确实没有:


如何创建一个上下文,使EclipseLink不会无法查找数据源

解决了我的问题。在测试期间尽量避免使用JNDI,至少这也是很多人的建议

在我的代码中,我手动创建了一个
EntityManagerFactory
,用于查找persistence.xml中定义的JNDI字符串。由于缺少JNDI,这在测试期间失败

现在,我让Spring为我创建EntityManagerFactory,并让Spring选择数据源

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="MyPU" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
    <property name="jpaPropertyMap">
        <props>
            <prop key="eclipselink.logging.level">INFO</prop>
            <prop key="eclipselink.weaving">false</prop>
        </props>
    </property>
</bean>

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DefaultDB" expected-type="javax.sql.DataSource" />

信息
假的

persistence.xml
中不定义数据源的JNDI名称,而是在spring配置中,这样做的好处是spring可以在测试期间覆盖
dataSource
bean,因此现在JNDI查找,没有失败的测试。

解决了我的问题。在测试期间尽量避免使用JNDI,至少这也是很多人的建议

在我的代码中,我手动创建了一个
EntityManagerFactory
,用于查找persistence.xml中定义的JNDI字符串。由于缺少JNDI,这在测试期间失败

现在,我让Spring为我创建EntityManagerFactory,并让Spring选择数据源

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="MyPU" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
    <property name="jpaPropertyMap">
        <props>
            <prop key="eclipselink.logging.level">INFO</prop>
            <prop key="eclipselink.weaving">false</prop>
        </props>
    </property>
</bean>

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DefaultDB" expected-type="javax.sql.DataSource" />

信息
假的
不在
persistence.xml
中定义数据源的JNDI名称,而是在spring配置中定义的优点是spring可以在测试期间覆盖
dataSource
bean,因此现在JNDI查找,没有失败的测试