0

I wanted to write test cased for testing my service using springjunit runner.
My service will call another service and transform its out put and send the response.
I thouhgt that server need to be up and running for calling the other service while running junit.
But I was told that spring junit doesnt need server to be running.
Spring container will do the magic it seems.
Am not pretty sure how this happens.
Can any one enlighten me with how spring container can act as server?

If its so silly quesiton,sorry for that.Thanks in advance

Renganathan V
  • 413
  • 1
  • 9
  • 27
  • Please look at the `@ContextConfiguration` annotation and its usage. You can specify your XML config `applicationContext` to be loaded in memory. – Hrishikesh Feb 20 '14 at 10:48
  • Please read [10. Testing](http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/testing.html). – Markus Malkusch Feb 20 '14 at 10:50

1 Answers1

0

do you need server to run java.class , answer is no, untill unless if you have any ejb component to be called from your service, or you service need some external web service to respond (here you may need to either mock this service to give mock data or run the service on server) i have service which calls data access layer , sometimes service calls another service.

all you need to have spring context configuration in your test class

@ContextConfiguration({ "classpath:spring-context.xml", "classpath:otherservice-context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@Component
public class TestJuint{

  @Autowire
  private otherService otherServiceImpl;

  @Autowire
  private service serviceImpl;

  @Test
  public void testDummy{
   serviceImpl.addDummy(dummyObj);
  }
}

suppose if you need to have another service from some other package then you might want to include its context file in context configuration, so that its bean reference will be in spring context while autowiring

pappu_kutty
  • 2,378
  • 8
  • 49
  • 93