Java 测试spring云服务到服务调用
我想知道测试服务对服务呼叫的最佳方法是什么?目前,每个服务驻留在单独的子模块中。e、 gJava 测试spring云服务到服务调用,java,spring,spring-cloud,microservices,Java,Spring,Spring Cloud,Microservices,我想知道测试服务对服务呼叫的最佳方法是什么?目前,每个服务驻留在单独的子模块中。e、 g /project /cat-service //Location of CatController /dog-service //Location of DogController, CatClient Cat服务 CatController如下所示: class CatController { int callCounter = 0; public int getCal
/project
/cat-service //Location of CatController
/dog-service //Location of DogController, CatClient
Cat服务
CatController
如下所示:
class CatController
{
int callCounter = 0;
public int getCallCounter() { return callCounter; }
@RequestMapping(method = GET, value = "/catservice/simple")
public Object callCat() {
callCounter++;
return "purrrrrr";
}
}
class DogController
{
@Autowired
CatClient catClient;
@RequestMapping(method = GET, value = "/dogservice/simple")
public Object callDog()
{
//In here make a call to the catservice over the network.
catClient.callCatService();
return "Woof woof";
}
}
@FeignClient("cat")
public interface CatClient
{
@RequestMapping(method = GET, value = "/catservice/simple")
public Object callCatService();
}
养狗服务
DogController
如下所示:
class CatController
{
int callCounter = 0;
public int getCallCounter() { return callCounter; }
@RequestMapping(method = GET, value = "/catservice/simple")
public Object callCat() {
callCounter++;
return "purrrrrr";
}
}
class DogController
{
@Autowired
CatClient catClient;
@RequestMapping(method = GET, value = "/dogservice/simple")
public Object callDog()
{
//In here make a call to the catservice over the network.
catClient.callCatService();
return "Woof woof";
}
}
@FeignClient("cat")
public interface CatClient
{
@RequestMapping(method = GET, value = "/catservice/simple")
public Object callCatService();
}
CatClient
如下所示:
class CatController
{
int callCounter = 0;
public int getCallCounter() { return callCounter; }
@RequestMapping(method = GET, value = "/catservice/simple")
public Object callCat() {
callCounter++;
return "purrrrrr";
}
}
class DogController
{
@Autowired
CatClient catClient;
@RequestMapping(method = GET, value = "/dogservice/simple")
public Object callDog()
{
//In here make a call to the catservice over the network.
catClient.callCatService();
return "Woof woof";
}
}
@FeignClient("cat")
public interface CatClient
{
@RequestMapping(method = GET, value = "/catservice/simple")
public Object callCatService();
}
为了测试从狗服务到猫服务的呼叫,我将在狗服务中包括猫服务以进行测试:
// dog-service/build.gradle
testCompile project(path: ':cat-service')
然后在测试类中:
@IntegrationTest
public class DogTest
{
@Autowired
CatController catController;
@Autowired
DogController dogController;
@Test
public void testDogController()
{
//Could spy on the catController instead.
int callCountStart = catController.getCallCounter();
dogController.callDog();
assertTrue (catController.getCallCounter() > callCountStart);
// Could also verify the state of the spy
}
}
有更好的方法吗?这有什么严重的缺陷吗?你想测试什么?在我的例子中,预期的行为是,
dogservice
每次调用/dogservice/simple
时都会通知catservice
。