0

我在测试我的应用程序时遇到了一些麻烦,但它在正常执行中运行良好。我认为它来自未找到的 JNDI 资源,但我不明白为什么以及如何修复它。

当我开始我的 Junit 测试时,我收到了这个错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource com.sample.DAOImpl.myDatasource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myDatasource)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDatasource' defined in URL [file:src/test/resources/spring/test-dao-config.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at ...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource com.sample.DAOImpl.myDatasource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myDatasource)}
    at ...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myDatasource)}
    at ..

这是我的配置:

上下文.xml

<Resource name="jdbc/myDatasource" auth="Container" type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@database:99999:instance"
    username="user"
    password="password"
    validationQuery="select 1 from dual"
    testOnBorrow ="true"
    maxActive="5"
    maxIdle="1"
    maxWait="-1" />

test-dao-config.xml

<bean id="myDatasource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/myDatasource" />
</bean>

道实现

@Repository
public class DacsDAOImpl implements DacsDAO
{
    private final static Logger LOGGER = LoggerFactory.getLogger(DAOImpl.class);

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

    ....
}

还有我的测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/spring/test-dao-config.xml" })
public class MyDAOImplTest
{
    private MyDAO dao;

    @BeforeClass
    public static void initJndi() throws IllegalStateException, NamingException
    {
        //some test, but doesn't work

//      SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
//      builder.bind("java:comp/env/jdbc/myDatasource", "myDatasource");
//      builder.activate();
    }

    @Before
    public void setUp() throws IllegalStateException, NamingException
    {
        dao = new MyDAOImpl();
    }

    @Test
    public void testTotalUser()
    {
        int result = dao.getTotalUser();
        Assert.assertEquals(0, result);
    }
}

谢谢

4

1 回答 1

0

您正在测试用例中运行,因此您的所有Context.xml内容均不可用,因为仅在 tomcat 上可用。为什么你需要在你的测试用例中进行 jndi 查找呢?如果您想测试您的 dao,请使用内存数据库(如 hsql、h2 或 derby)并改用它。Spring 有一些不错的标签可以让您轻松使用。

<jdbc:embedded-database id="myDataSource" type="H2">
    // Add some init scripts here.
</jdbc:embedded-database>

如果您真的需要进行 JNDI 查找,那么您的测试用例就差不多完成了。但是,您必须注册 aDataSource而不是String. 因此,您仍然需要构建一些(内存中)数据源并将其绑定到模拟 jndi 位置

@BeforeClass
public static void initJndi() throws IllegalStateException, NamingException
{
    //some test, but doesn't work
    // Construct in-memory database
  SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
  builder.bind("java:comp/env/jdbc/myDatasource", myDatasource);  //Actual datasource not a String!
  builder.activate();
}

最后你的测试也有缺陷,你正在加载你的上下文,但没有对它做任何事情。MyDAOImpl您正在您的方法中构建 a @Before。为什么还要费心加载上下文,因为你什么都不做。

于 2014-03-17T19:17:29.710 回答