2016-05-24 121 views
2

當我運行我的spring集成junit類時,我正在獲取上面的異常。運行測試用例時加載ApplicationContext失敗

這裏是我的

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:applicationContext.xml") 
public class BpmControllerTest { 

    @Autowired 
    private BpmProcessorDaoImplTest bpmProcessorDao; 

    @Test 
    public void testRun() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception { 
    List<User>user=bpmProcessorDao.testRead(); 
    Assert.assertEquals(0,user.size()); 

    } 
    } 

我有我的WEB-INF內ApplicationContext,並且,我使用所有的春季4.x的罐子。

這裏是我的堆棧跟蹤..

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist 
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) 
    ... 37 more 

任何機構可以請告訴我如何寫這條線

@ContextConfiguration(locations = "classpath:applicationContext.xml") 

一些地方在谷歌,我發現這樣的

@ContextConfiguration(locations = "classpath:**/applicationContext.xml") 

這兩個 有什麼不同,當我用明星寫這行時,我是得到不同的異常

這裏是我的堆棧跟蹤。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tcs.test.dao.BpmProcessorDaoImplTest] 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)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 
    ... 28 more 

有一點,我的應用程序只是動態web項目沒有maven,沒有螞蟻。 可以任何機構請告訴我如何成功運行我的測試用例..

這裏是我的applicationContext。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/task 
     http://www.springframework.org/schema/task/spring-task-3.2.xsd 
     http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util-3.2.xsd 
     http://www.springframework.org/schema/jdbc 
     http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 
    "> 
     <tx:annotation-driven /> 
      <tx:jta-transaction-manager/> 
     <context:component-scan base-package="com.tcs.test" /> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
    <property name="url" value="jdbc:oracle:thin:@172.19.8.159:1521/OIM.itba.gov.in" /> 
    <property name="username" value="AppDB"></property> 
    <property name="password" value="AppDB"></property> 
    <property name="initialSize" value="2" /> 
    <property name="maxActive" value="5" /> 
    </bean>  

    <bean id="messageSource" 
     class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename"> 
      <value>messages</value> 
     </property> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <bean id="runScheduler" class="com.tcs.controller.BpmControllerTest" /> 
     <task:scheduled-tasks> 
    <task:scheduled ref="runScheduler" method="testRun" cron="0 0/1 * * * ?" /> 
    </task:scheduled-tasks> 
</beans> 

所有我的測試java文件在側面測試源文件夾。 和所有的中端文件包的哪個前綴是com.tcs.test

這裏是我的daoImpl類

package com.tcs.test.dao; 

import java.io.ByteArrayInputStream; 
import java.io.ObjectInputStream; 
import java.sql.Blob; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Locale; 

import org.apache.log4j.Logger; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.support.ResourceBundleMessageSource; 
import org.springframework.dao.DataAccessException; 
import org.springframework.jdbc.core.JdbcTemplate; 
import org.springframework.jdbc.core.ResultSetExtractor; 
import org.springframework.stereotype.Repository; 

import com.tcs.controller.BPMConstants; 
import com.tcs.controller.User; 

@Repository 
public class BpmProcessorDaoImplTest implements BpmProcessorDaoTest{ 

    private static final Logger logger=Logger.getLogger(BpmProcessorDaoImplTest. class); 


    @Autowired 
    JdbcTemplate jdbcTemplate; 

    @Autowired 
    private ResourceBundleMessageSource messageSource; 

    @Test 
    public void testWrite() { 
    } 

    @Test 
    public void testUpdateStatus() { 
    } 

    @Test 
    public List<User> testRead() { 

     String query=null; 
     List<User>users=null; 

     try{ 
    // jdbcTemplate.setDataSource(dataSource); 
    // query=messageSource.getMessage(BPMConstants.QUERY,null,Locale.US); 
      query="select taskoutcome,seqNo,hash_mapdata,userid,status,taskId from com_tt_bpm_batch , " 
        + "wftask where status='ACTIVE' and request_id=instanceid and state='ASSIGNED'"; 

     logger.info("query"); 
     jdbcTemplate.setFetchSize(20); 


     users=jdbcTemplate.query(query, new ResultSetExtractor<List<User>>(){ 
      List<User> userList = new ArrayList<User>(); 

      @SuppressWarnings("unchecked") 
      @Override 
      public List<User> extractData(ResultSet rs) throws SQLException,DataAccessException { 
        while(rs.next()){ 
        logger.info("fetching records from db");  
        User user = new User(); 
        user.setTaskOutcome(rs.getString(BPMConstants.TASK_OUTCOME)); 
        user.setUserId(rs.getString(BPMConstants.USER_ID)); 
        user.setStatus(rs.getString(BPMConstants.STATUS)); 
        user.setTaskId(rs.getString(BPMConstants.TASK_ID)); 
        user.setSeqNo(rs.getLong(BPMConstants.SEQ_NO)); 
        user.setUserComment("nothing"); 
        Blob blob=rs.getBlob(BPMConstants.HASH_MAPDATA); 
        try{ 
        if(blob!=null && !blob.equals("")){ 
        int blobLength = (int) blob.length(); 
        byte[] blobAsBytes = blob.getBytes(1, blobLength); 
        ByteArrayInputStream bos = new ByteArrayInputStream(blobAsBytes); 
        ObjectInputStream out=null; 
         out = new ObjectInputStream(bos); 
        HashMap<String, Object> map=null; 
         map = (HashMap<String, Object>)out.readObject(); 
        user.setMap(map); 
        } 
        userList.add(user); 
        }catch(Exception e){ 
         logger.error(e.getMessage()); 
         logger.error("Exception at UserRowMapper class while reading data from blob "+e.getStackTrace()); 
        } 
        } 
       return userList; 
      } 
     }); 
     }catch(Exception e){ 
      logger.error(e.getMessage()); 
      logger.error("Exception at UserRowMapper class while reading data from db "+e.getStackTrace()); 
     } 
     return users; 
    } 
    } 
+0

您的applicationContext.xml是否有'BpmProcessorDaoImplTest'的bean定義? –

+0

請分享你的applicationContext文件。 – shankarsh15

+0

是的,它在那裏。我正在編輯我的代碼,請檢查。 – suri

回答

1

1)

我有我的WEB-INF內ApplicationContext,並且,我使用所有的春季4.x罐子。

web-inf文件夾在運行測試時沒有(沒有黑客和問題)accessabel。

所以短期和簡單的解決方案是把春天的配置文件:

  • (如果你用maven):src\main\resources
  • (如果你不使用Maven的):你java源文件根文件夾
  • (如果您不使用maven但是eclipse):創建一個額外的文件夾(例如resources),將文件放入該文件夾中,然後將該文件夾一日食源文件夾(右鍵單擊該文件夾中的包資源管理器,然後選擇「構建路徑」 /「用作源文件夾」)

2) 您BpmProcessorDaoImplTest看起來並不像我的一個有效的測試。

要麼是測試用例 - 那麼它的方法有@Test註釋,而類本身的@ContextConfiguration配置指向您的配置文件。或者是存儲庫,那麼它有一個@Repository註釋而不是@Test@ContextConfiguration。註釋。但我從來沒有見過一個混合了這個的課程。

那麼試試這個:

@ContextConfiguration("classpath:applicationContext.xml") 
@Transactional //make your tests run in an transaction that gets rolled back after the test 
public class BpmProcessorDaoImplTest { 

    /** Class under test */ 
    @Autowired 
    private BpmProcessorDao dbmProcessorDao; 

    @Autowired 
    JdbcTemplate jdbcTemplate; 

    @Autowired 
    private ResourceBundleMessageSource messageSource; 

    //just to make the example test usefull 
    @PersistenceContext 
    private EntityManager em 

    @Test 
    public void testWrite() { 

     DbmProcessor entity = ...; 
     ... 
     this.dbmProcessorDao.save(); 
     ... 
     em.flush(); //make sure that every is saved before clear 
     em.clear(); //clear to make read(id) read the entity from the database but not from l1-cache. 
     int id = entity.getId();   
     ... 

     DbmProcessor reloadedEntity = this.dbmProcessorDao.read(id); 

     //getName is just an example 
     assertEquals(entity.getName(), dbmProcessorDao.getName()); 
    } 
} 
+0

非常感謝您的回覆,現在我得到了任何異常。但是在我的dao類中,jdbc模板和消息資源類都是null,這意味着spring不會爲這些類創建對象。 – suri

+0

Ist很可能需要複製其他配置文件。 – Ralph

+0

我只有一個配置文件 – suri

0

這將occure時的applicationContext.xml不能在類路徑中找到

可能的解決方案:

  1. 添加目錄包含的applicationContext.xml到類路徑。
  2. 給出的applicationContext.xml的相對路徑
  3. 給出絕對路徑的applicationContext.xml

如果第三解決方案爲您將意味着的applicationContext.xml是不是在類路徑中。

+0

海感謝您的回覆,在這裏,正如我前面提到的,我保留了我的applicationContext,xml在web-inf中,那麼class path – suri

相關問題