Junit和Spring依赖项

时间:2016-08-10 13:49:45

标签: spring junit

junit是否支持在MVC架构中向较低级别注入bean?

ie:当我想从classTest访问我的服务时,当service classe尝试访问bean dao时,它会给我nullpointerException 我不知道问题出在哪里,有人可以提供帮助 谢谢

我的配置:

测试类:

 @RunWith(SpringJUnit4ClassRunner.class)

 @ContextConfiguration(locations = "classpath:/test-config.xml")

 public class Case1Test extends Mockito {

 @Autowired
 UserServiceImpl userService;


 @Test
 public final void testInit() {
 String username = "abc";
 String prenom = userService.findByUserName(username);

assertEquals(prenom, "Hajar");
}
}

我的服务:

public class UserServiceImpl implements UserService  {
private static final Logger LOG
=LoggerFactory.getLogger(UserServiceImpl.class);
 public UserServiceImpl() {

 LOG.info("Initializing {}", UserServiceImpl.class.getSimpleName());
 }

 private UserDao userDao;

public UserDao getUserDao() {
return userDao;
  }

@Autowired (required=false)
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

@Override
 public String findByUserName(String username) {

String prenom = userDao.findByUserName(username).getPrenom();


return prenom;
}


  }

我的DAO:

public class UserDaoImpl implements UserDao {

private static final Logger LOG =LoggerFactory.getLogger(UserDaoImpl.class);

public UserDaoImpl() {

 LOG.info("Initializing {}", UserDaoImpl.class.getSimpleName());
 }

 private SessionFactory sessionFactory;

  @Transactional
  @SuppressWarnings("unchecked")
   public User findByUserName(String username) {

  List<User> users = new ArrayList<User>();
  users = this.getCurrentSession().createQuery("from User where
  username=?")
        .setParameter(0, username).list();

  if (users.size() > 0) {
    return users.get(0);
  } else {
    return null;
  }

   }


public SessionFactory getSessionFactory() {

        return sessionFactory;
}
@Autowired (required=false)
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Session getCurrentSession()  {
if ( this.sessionFactory == null ) {
System.out.println("session NULLL");
}

 Session session;
 try {
    session =  this.sessionFactory.getCurrentSession() ;
 } catch(HibernateException e ){

    session = this.sessionFactory.openSession();
  }

        return session  ;
  }


     }

test-config.xml是:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context"
         xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc
   http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">


 <bean class="fr.service.UserServiceImpl"></bean>


    </beans>

和我的context.xml:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
 <property name="driverClassName" value="${dataBase.driver}"/>
 <property name="url" value="${dataBase.url}"/>
 <property name="username" value="${dataBase.user}"/>
<property name="password" value="${dataBase.password}"/>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations"
 value="classpath*:/orm/*.hbm.xml"/>

<property name="hibernateProperties">

    <props>
        <prop
 key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.show_sql">true</prop>

    </props>

 </property>

 </bean>

当我的Service类(UserServiceImpl)尝试访问此行中的dao bean时,我有NullPointerException:String prenom = userDao.findByUserName(username).getPrenom();来自UserServiceImpl类

1 个答案:

答案 0 :(得分:0)

那么你的配置在哪里实际包含了context.xml? 我没有看到它,因此您的测试用例的上下文仅包含test-config.xml中定义的那些只是用户服务的bean ...

如果您希望将您的dao连接到您的服务,它应该在类路径扫描中,或包含在您的test-config.xml