0

我想编写测试用例来使用 springjunit runner 测试我的服务。
我的服务将调用另一个服务并转换其输出并发送响应。
我认为服务器需要启动并运行才能在运行 junit 时调用其他服务。
但有人告诉我 spring junit 不需要服务器运行。
Spring 容器会发挥它看起来的魔力。
我不太确定这是怎么发生的。
谁能告诉我spring容器如何充当服务器?

如果它是如此愚蠢的问题,对不起。提前谢谢

4

1 回答 1

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

于 2014-02-20T11:17:26.463 回答