Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

spring3 with junit

 
anne mishra gopi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
anne mishra gopi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic