Anystub wraps function calls and attempts to find a matching call that has already been made and recorded. Two things can then happen:
Recorded calls and corresponding results are stored in files called stub-files.
Out of box anyStub includes wrappers for org.apache.http.client.HttpClient
from Apache
HttpClient 4.5.8
to stub http connections and some interfaces from javax.sql.*
to stub DB-connections.
anyStub provides simple API and requires minimum overhead code to get the benefits.
Out of box Anystub supports Apache HttpClient and javax.sql.DataSource to record and replay requests.
To use it you need to inject wrappers properly. For stubbing http connection use org.anystub.http.StubHttpClient
. The main
class for stubbing database calls is org.anystub.jdbc.StubDataSource
. In tests you will create real connections and wrap them with
provided implementations by passing them as a parameter in constructor.
Examples below demonstrate how to stub connections in spring-boot tests.
Once you stub the connections and run tests all calls begin to appear in a default-stub file src/test/resources/anystub/stub.yml
Sometimes, you need to split the calls by different stub-files. To do that you can utilize an annotation @AnystubId
. You can apply it at class or method level in test files.
You can put it with your test class/test method to direct saving all calls under the annotation in a distinct file.
By default, the file has a name of the class or the method with .yml extention.
You will find the files in src/test/resources/anystub/. The annotation
has a parameter "filename" to
give the file a specific name. The extension will be added automatically.
AnyStub library is developed for spring-boot applications to stub external communication. In the examples you run the same full spring container. The only difference is a bean wrapped by class from anyStun library. If you run spring container in tests in a application with a configuration in .xml files you can also consider injecting the classes.
You can stub any call in your plain-java application. Here is an
example of how to create a wrapper for a method get()
of plain java class.
In examples above, the stub files were created as by default with RequestMode.rmNew
attribute.
That means all calls will be assessed against the stub files.
In case of the call is found in the file the result will be recovered from file and the hit of the real
system will be procrastinated (or maybe omitted if not required).
Duplicates of the calls do not appear in the stub-file.
If AnyStub failed to find a call it will hit the target system, store the call id and
results in the stub and return the result to caller.
AnyStub provides few more modes to proceed calls:
RequestMode.rmNone
Sending requests to a real system is forbidden. If request is not found in a stub-file then exception is
thrown.
RequestMode.rmTrack
If the stub file just created all requests are recorded in the stub file (even if duplicated exists).
If the stub-file exists
sending requests to a real system is forbidden. Calls are expected exactly in the same order as it was
on a record phase.
If next call doesn't match records in the stub-file the exception thrown.
RequestMode.rmAll
The Stub file clear on the first call. All requests are sent to a real system and results written to the
stub-file. Duplicates are possible.
RequestMode.rmPassThrough
all calls go directly to a real system.