0

我正在尝试 JUnit 访问 iBatis 的 DAO。问题是 iBatis 无法读取 SqlMapClient,尽管我在applicationContext.xml. 所以我试图为它设置完整路径,但我仍然得到错误。

应用程序上下文.xml

<!-- Sql Map Client -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="file:C:/workspace/MyProject/WebContent/WEB-INF/sqlMapConfig.xml" />
    <property name="dataSource" ref="dataSource"/>
</bean>

JUnit 代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations={
  "file:C:/workspace/MyProject/WebContent/WEB-INF/sqlMapConfig.xml",
  "file:C:/workspace/MyProject/WebContent/WEB-INF/applicationContext.xml",
  "file:C:/workspace/MyProject/WebContent/WEB-INF/dataSource.xml",  
}
)

// This doesn't work
//locations={
//"classpath:/WEB-INF/sqlMapConfig.xml",
//"classpath:/WEB-INF/applicationContext.xml",
//"classpath:/WEB-INF/dataSource.xml",
//}

public class ProductDaoTest {
    /**
     * Run the List<Product> getProductList(Product) method test.
     *
     * @throws Exception
     *
     * @generatedBy CodePro at 7/1/13 10:55 AM
     */
    @Test
    public void testGetProductList()
        throws Exception {
        ProductDao fixture = new ProductDao();
        Product product = new Product();

        List<Product> result = fixture.getProductList(product);
        assertNotNull(result);
    }
}

我收到这个错误

java.lang.IllegalArgumentException: No SqlMapClient specified
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:166)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:249)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:296)
    at net.rcba.dao.BrandDao.getBrandRankingSumViewList(BrandDao.java:19)
    at net.rcba.dao.BrandDaoTest.testGetBrandRankingSumViewList_1(BrandDaoTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

1 回答 1

1

我猜错误可能在

ProductDao fixture = new ProductDao();

尝试将其更改为

WebApplicationContext wac = WebApplicationContextUtils.
                   getRequiredWebApplicationContext( config.getServletContext());

ProductDao fixture = (ProductDao) wac.getBean("ProductDao" );
于 2013-07-02T08:03:10.600 回答