My application has a custom server component.
We have some JUnit test cases for the business logic with JMockit in place to mock the DB layer.
For integration testing, we end up having a lot of similar conditions to check. The only difference is that I need to serialize stuff and send to a socket instead of just making a call.
Is there any (simple) way to generalize the tests so that I can 开发者_运维问答specify those conditions once for both tests?
> Is there any (simple) way to generalize the tests so that I can
> specify those conditions once for both tests?
You can put the tests into a baseclass with a virtual FactoryMethod that constructs helper-objects.
Your unittest und integrationtest inherits from that class. The implementation of the FactoryMethod creates either some mock or some real object.
You could add the rules to a file which you can reference from the integration test and the unit tests.
You should write it in some kind of domain specific language which can be read by non-technical stakeholders but can also be parsed, e.g.:
discount: 10% for: orders over $10
discount: 20% for: orders over $40
discount: 30% for: orders over $100
Your unit and integration tests are just another piece of code which would need to be applied the statndard concepts you have for your main stream code. So, things like writing reusable code, using design patterns, coding to interfaces and finally continuous refactoring would hold true for tests as well.
Following SO threads have more advice:
- Re-using code in unit tests
- Unit-Test, Integration test or problem in design?
Hope that helps.
精彩评论