简体   繁体   English

单元测试Web服务方法

[英]Unit testing web service methods

I am currently writing an application which is kind of a proxy between the end-user and a web service. 我目前正在编写一个应用程序,它是最终用户和Web服务之间的代理。

The user requests a service, and the application calls the web service method with appropriate parameters set, and returns the result. 用户请求服务,应用程序使用适当的参数集调用Web服务方法,并返回结果。

To make sure nothing went wrong, I have decided to create unit tests for the project (Yep! I know it's a bit late to do it after writing the code. I'll write the unit-tests first next time). 为了确保没有出错,我决定为项目创建单元测试(是的!我知道编写代码之后这样做有点晚了。我下次将首先编写单元测试)。

in these unit tests, I have to make real requests to the web service to be sure no permission exception, service-related exception, etc is raised. 在这些单元测试中,我必须向Web服务发出实际请求,以确保没有引发权限异常,服务相关异常等。 Therefore extracting an interface and mocking the service for testing is not going to help. 因此,提取接口并模拟服务以进行测试并没有帮助。 Is it acceptable to write unit tests, that actually call the real web service? 编写实际调用真实Web服务的单元测试是否可以接受?

Is it possible to have a delay between two tests if my web service just allows for example one request per minute? 如果我的Web服务只允许每分钟一个请求,是否可能在两个测试之间有延迟?

What you are speaking about is integration test . 你在说什么是集成测试

It's not a problem to write such tests for your web service. 为您的Web服务编写此类测试不是问题。 They are written in the same style as unit test but testing live system and or it's parts. 它们的编写方式与单元测试相同,但是测试实时系统和/或它的部件。 You can even start your web service in your test context. 您甚至可以在测试环境中启动Web服务。

And it's up to you and your test context to limit situations with call delays or simulate any other situation. 这取决于您和您的测试环境,以限制具有呼叫延迟的情况或模拟任何其他情况。

My understanding is that unit tests are supposed to yield immediate result. 我的理解是单元测试应该立即产生结果。 What you are after is integration tests, not unit tests. 你所追求的是集成测试,而不是单元测试。

It is acceptable of course, although they would me more "integration tests" than unit tests, since you are testing your whole system integrated. 当然,这是可以接受的,虽然他们会比单元测试更多的“集成测试”,因为您正在测试整个系统的集成。

What you can do to delay the tests, is to simply put a Thread.sleep(1000) in the beginning of the test. 你可以做些什么来延迟测试,就是在测试开始时简单地放一个Thread.sleep(1000)

What about testing the code behind the web service layer? 那么测试Web服务层背后的代码呢? What I have done in the past is using web services as merely wrappers to a business logic library where all the real code lives. 我过去所做的就是使用Web服务作为业务逻辑库的包装器,其中所有真实代码都存在。 Then you can do integration tests on this library without worrying about your web service request limits. 然后,您可以对此库执行集成测试,而无需担心Web服务请求限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM