简体   繁体   中英

Test Setup routines design pattern

I have a class that extends the JUnit TestCase class. The test has a number of 'setup' routines that need to be executed. One of these routines is to insert some data into a database for example, another is to send JMS messages onto EMS queues.

Currently, all of this functionality is inside the overridden setUp method in the test class. However, there must be a better way of doing this. I'd possibly want to use dependency injection also to make the test class more generic.

Thanks.

I don't think dependency injection is (necessarily) the answer unless you plan on writing tests for your test cases. If I were you I'd do the simplest thing possible and that is to extract this setup code into classes. For example, a JmsSetup class and a DatabaseSetup class. Have your setup method create and use these classes.

Take it from there, if that starts to get messy you can do more but don't cross that bridge until you get to it.

Nothing stops you from factoring these setup code segments out into separate classes and use any other OO principles you like. You can then have a simple setup routine that delegates it all to a number of classes that do the actual work and can therefore be reused in other test cases..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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