JRUNR: The idea
Java Record Unit Test and Replay/Reuse
The problem
There is a need to unit test existing code that is not TDD aware, hence doesnt have a unit test framework.Also, mocking is not an option as the system*under*test (SUT) is mostly a method or piece of code deep in the call stack, and mocking the backend will require mock-calling-mock code.
The solution
Record the state of objects required to make the call to the SUT in one run of the app, store and re*use in unit code
Implementation Thoughts
The implementation will need solutions for the following sub problems:
- how to record without changing source too much or at all
- how to record enough information of the original context for use in the test
- how to view and modify the stored information to suit the same test context or a different one, or to create bulk test data from the single recorded data
- how to use the stored information in a test case
Each is detailed below:
- how to record without changing source too much or at all
- some form of aop
- annotations: might not work as we need the object values passed from the running context to the annotation processor (AP)
- any around advice aspect would work, but to make it generic, we'd need a declarative way of defining the variable(s) that the aspect would be tracing.
- how to record enough information of the original context for use in the test
- ie, how to record which method, line of code etc for tracing, reporting purposes
- could probably use annotations here, but can this be passed to the ap?
- how to view and modify the stored information to suit the same test context or a different one, or to create bulk test data from the single recorded data
- sub problems:
- how to store for easy readability,configurability
- store as serialized objects, write app to read it back and provide a config ui
- store as xml so std xml editors.etc can be used
- store as xml that is also spring config
- store as json,yml or similar such readable format
- how to view and modify
- use existing test generator frameworks/UIs
- which? are there any? research this
- create custom webapp
- how to use the stored information in a test case
- this is pretty straightforward - need to have a way to refer uniquely to the test data that we need in a junit test case. After that its standard junit testing.
Links:
http://ddtunit.sourceforge.net/ makes unit tests data driven. can be used to enhance the last part - ie scaling up recorder data to regression/data coverage levels http://xunitpatterns.com/ reference site that can be used to explain the context and terminology. specifically "SUT" and "Recorded Test" are terms from this site.
Status:
Recording with aop tested out - working Context can also be got as <class to be tested>.<method called>. This can be used to store the data identifying it uniquely. current attempt is to store as json so that objects can be edited with a freely available json editor finally all of this can be packaged into a spring web app.