i got a such type of error in junit+struts2 +spring


org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1068)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at net.action.TestNew.setUp(TestNew.java:45)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
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)

Post your code for test or have closer look on line 45 of your TestNew.java it is complaining sessionFactory not being defined

package net.action;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.WebApplicationContext;

import com.opensymphony.xwork2.Action;


public class TestNew extends TestBase {

	protected FileSystemXmlApplicationContext context = null;
	protected SessionFactory sessionFactory=null ;
	protected ApplicationContext applicationContext;
	private AccountAction currentAction;

	public String getContextLocations() {
		return "/applicationContext.xml";
	}

	protected void init() {
		servletContext.setAttribute(
				WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
				applicationContext);
	}

	/**
	 * setup method is called before test
	 */
	protected void setUp() throws Exception {
		super.setUp();
		
		//String[] contextLocations = new String[1];
		//contextLocations[0] = "test/applicationContext.xml";
		//contextLocations[1] = "src/test/applicationContext.xml";
		context = new FileSystemXmlApplicationContext("test/applicationContext.xml");
		System.out.println("see ");
		
		sessionFactory =  (SessionFactory) context.getBean("sessionFactory");
		System.out.println("pl check it");
		Session session = SessionFactoryUtils.getSession(sessionFactory,false);
		TransactionSynchronizationManager.bindResource(sessionFactory,
			new SessionHolder(session));
	}

	@Test
	public void tests() throws Exception {
		
		request.setAttribute("account.username", "hello");
		request.setAttribute("account.password", "test");
		
		// code to set test action environment
		createAction("/create", "Test", "list");

		// code to execute test action
		String result = proxy.execute();
		
		// code to test action result
		assertEquals("valid Page...", Action.SUCCESS, result);
	
		System.out.println("List successfully dispalyed ....");
		
	}
	

	/**
	 * @param currentAction
	 *            the currentAction to set
	 */
	public void setCurrentAction(AccountAction currentAction) {
		this.currentAction = currentAction;
	}

	/**
	 * @return the currentAction
	 */
	public AccountAction getCurrentAction() {
		return currentAction;
	}
	
	public SessionFactory getSessionFactory(){
		
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory=sessionFactory;
		
	}
	
}

thanx friend ...
where we haveto define sessionFactory

Are sure that is correct way to initialize SessionFactory for Spring?

context = new FileSystemXmlApplicationContext("test/applicationContext.xml");
		System.out.println("see ");
		
		sessionFactory =  (SessionFactory) context.getBean("sessionFactory");

I have no experiences with Spring so I don't know if that is possible.
In basic conditions what people have is some sort of HibernateUtil class to get hold of SessionFactory

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

and then in test you do

private MyServiceToTest serviceToTest;

    @Before
    public void setUpItemTypeRepository() {
        HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
        serviceToTest = new HibernateServiceToTest(HibernateUtil.getSessionFactory());
    }

    @After
    public void rollback() {
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
    }

i got a such type of error in junit+struts2 +spring

If you are "testing" your web application as opposed to individual Java components (which is what JUnit does), look into frameworks specifically tailored for those needs. IMO, a proper abstraction here would be to use Cactus or HttpUnit for testing your "web" requests. Use JUnit if you want to test your *individual* Java components, not the entire HTTP request/response cycle.

After a bit of searching, I found a tutorial which uses Cactus for testing a Struts web application which you might find useful.

commented: Useful link, thanx +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.