关于Junit4测试没反应
现在做SSH整合的时候,在hibernate和spring整合过程中,用junit4测试,右键点击后run as->junit test,然后控制台无输出,求大家帮忙解决啊
下面是测试类的代码:
package com.ssh.service.impl;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ssh.bean.User;import com.ssh.service.UserService;public class UserServiceImplTest { public static UserService service; @BeforeClass public void setUpBeforeClass(){ ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml"); service = (UserServiceImpl)context.getBean("userServiceImpl"); } @Test public void testSave(){ User u = new User(); u.setName("zhangsan"); u.setPassword("123"); service.addUser(u); }}
package com.ssh.service.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.ssh.bean.User;import com.ssh.service.UserService;@Service@Transactionalpublic class UserServiceImpl implements UserService { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Transactional(propagation=Propagation.SUPPORTS) public void addUser(User user) { getSessionFactory().getCurrentSession().persist(user); } public void delete(String name) { getSessionFactory().getCurrentSession() .createQuery("delete User u where u.name=:name") .setString("name", name) .executeUpdate(); } public User find(String name) { User user =(User)getSessionFactory().getCurrentSession().load(User.class,name); return user; } public List<User> list() { return getSessionFactory().getCurrentSession().createQuery("from User").list(); } public void update(User user) { getSessionFactory().getCurrentSession() .createQuery("update User u set u.name=:newName,u.password=:newPassword") .setString("newName", user.getName()) .setString("newPassword", user.getPassword()) .executeUpdate(); }}
<context:annotation-config/>
<context:component-scan base-package="com.ssh.service"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssh1"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedPackages">
<list>
<value>com.ssh.bean</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFacotry" ref="sessionFactory"/>
</bean>
</beans>
junit4的包也加了,就是没反应啊,求教
[解决办法]
spring有配套的 spring-test 包 spring 2.5开始就有了
相对于junit4 可以 extend org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
@ContextConfiguration(locations = {"classpath:/applicationContext-dao.xml" })
public class BaseDaoTestCase extends
AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private SessionFactory sessionFactory;
protected final Logger log = LoggerFactory.getLogger(getClass());
}
[解决办法]