Here is a test class code :
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);
}
}
userServiceImpl code is as follows :
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
@Transactional
public 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();
}
}
spring configuration file :
xmlns: context = "http://www.springframework.org/schema/context"
xmlns: tx = "http://www.springframework.org/schema/tx"
xsi: schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
junit4 package also added that no reaction ah , neighborhoods
------ Solution ----------------------- ---------------------
spring is supported by spring-test package starts there will be a spring 2.5
relative junit4 can 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 ());
}
------ Solution ------------------------------------ --------
unit test class to inherit AbstractTransactionalJUnit4SpringContextTests Spring integration Junit test
------ For reference only ----------- ----------------------------
Meirenhuida Oh . .
------ For reference only -------------------------------------- -
do not know what you said no console output is what happens !
Regardless of whether the console output , IDE environment JUnit panel there will be a corresponding output ;
If JUnit correct ( green ) , then no output console log output control may be a problem ;
on your current test class where there is no place println
------ For reference only ------------------- --------------------
etc. , learning, ha ha
------ For reference only -------- -------------------------------
protected final Logger log = LoggerFactory . getLogger (getClass ()); Will the next phrase log
Where will show ah ? I do not understand this sentence , ah, huh
------ For reference only ------------------------------ ---------
I used Log4j, the console will display properly built table statement , but I'm running the test method , Junit even the red stripe are not, that is not a state run Show
------ For reference only ------------------------------------- -
and a running test , junit above is displayed terminated, I used MyEclipse8.5
------ For reference only ---------------- -----------------------
selected method , then right !
------ For reference only -------------------------------------- -
it not being given ?
------ For reference only -------------------------------------- -
can cut a figure it ? Your phrase log log object is obtained . . . .
------ For reference only -------------------------------------- -
your class being tested simply do not reflect where the use JUnit , JUnit test class must inherit from TestCase, which extends TestCase, so right click select run as-> junit test before they can
- ---- For reference only ---------------------------------------
[ img = http://hi.csdn.net/space-3967939-do-album-picid-932244.html] [/ img]
[img = http://hi.csdn.net/space-3967939-do-album-picid-932244-goto-down.html] [/ img]
at the show terminated, even the errors are not reported ah
------ For reference only ----------------------- ----------------
I said, seemingly not !
------ For reference only -------------------------------------- -
Well, yes ah , junit4 not inherited , Oh , looks like just above @ Test on the line, can I point does not respond , worry ah.
------ For reference only -------------------------------------- -
This is a screenshot Oh , we help look ah
------ For reference only ------------------------ ---------------
Me also encountered , will be 64 when the BUG system does , the landlord operating environment how
------ For reference only ---------------------------------------
junit4 not inherit it ....
------ For reference only -------------------------------- -------
I was 32 , ah , Oh, I do not know how to die, before been possible
------ For reference only ---- -----------------------------------
assertEquals Why did not like the way ?
------ For reference only -------------------------------------- -
Baidu search for a long time, did you get to get to go or did not solve this problem , ah, I also encountered the same problem and the landlord
------ For reference only --------- ------------------------------
I also encountered the same problem In xp operating system is no problem. But replaced win7 can not. Do not know why
------ For reference only ---------------------------------- -----
today I have encountered this problem. I use spring-test and junit test checked, suddenly saw a say was using a spring-test , then you can do @ test but the method name must start with test . I hope for your help !
------ For reference only -------------------------------------- -
Is there an answer it ? I used juno eclipse version also have this problem
------ For reference only ---------------------------- -----------
who knows how to solve ah. I also encountered
------ For reference only ----- ----------------------------------
main method used to call the test method , and I have such a questions
没有评论:
发表评论