wood burning stoves*
The moose likes Spring and the fly likes spring3 with junit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring3 with junit" Watch "spring3 with junit" New topic
Author

spring3 with junit

anne mishra gopi
Greenhorn

Joined: Feb 22, 2013
Posts: 2
Hi All,
I am using junit 4 writing test cases for Implimentation class in spring3.
below is my test case class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:**/spring-servlet-context.xml")

public class AppointmentControllerTest extends AbstractJUnit38SpringContextTests{
private AppointmentController controller;
protected MockHttpServletRequest request;
protected MockHttpServletResponse response;
protected AnnotationMethodHandlerAdapter handlerAdapter;
protected ModelAndView modelAndView;

@Autowired
public ApplicationContext applicationContext;


private Detail detail1;

private DetailCriteria criteria;
private APPTDETAILS apptDetail;
@Before
public void setUp(){
controller = new Detail ();
criteria = new DetailCriteria ();
apptDetail = new APPTDETAILS ();

//geting bean through ApplicationContext

Detail = (Detail )getApplicationContext().getBean("'appointmentDetail' ");

request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = new AnnotationMethodHandlerAdapter();
}

@Test
public void testGetCallDetails() throws Exception{
apptDetail.setReferenceno(11111);
apptDetail.setAuthcode("test");
criteria.setApptDetail(apptDetail);
criteria.setServiceType("FS");
criteria=appointmentDetail1.getDetail(criteria);
String myexpectedviewname ="StatusOptions";
assertEquals("Incorrect view name returned", myexpectedviewname, modelAndView.getViewName());

}

/*@Override
public void setApplicationContext(ApplicationContext applicationContext1)
throws BeansException {
applicationContext =applicationContext1;

}*/


}

getting error trace in junit...

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'appointmentDetail' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
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 com.ge.appl.chs.fs.controller.tset.AppointmentControllerTest.setUp(AppointmentControllerTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)

Please provide some solution ASAP
Thanks in Advance.

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Please UseCodeTags.

Is that the only error you are getting? It is simply saying that spring-servlet-context.xml does not declare the bean appointmentDetail


[How To Ask Questions][Read before you PM me]
anne mishra gopi
Greenhorn

Joined: Feb 22, 2013
Posts: 2
I m using spring3 annotation, have define Implimentation class of AppointmentDetail as component bean by @Component("appointmentDetail ").
to access this impl class in junit test case I m using

Detail = (Detail )getApplicationContext().getBean("'appointmentDetail' ");

but in the error it is saying "No bean named 'appointmentDetail' is defined"

Note: I m able to access Implimentation class by using defined bean in application but not able to use in Junit Class.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yes because you have two sets of quotes in the name you are passing. The name of the bean is not 'appointmentDetail' it is appointmentDetail so the call to getBean would be

context.getBean("appointmentDetail")

not

context.getBean("'appointmentDetail'")

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: spring3 with junit
 
Similar Threads
Spring JUnit error
how to mock return value of method
How do I ignore "The entity "raquo" was referenced, but not declared." exceptions?
"Failed to load ApplicationContext" exception when trying to run JUnit Spring test
Facing problem in running JUnit test cases on code with Spring Annotations