I understand that there are @Before
and @BeforeClass
, which are used to define fixtures for the @Test
's. But what should I use if I need different fixtures for each @Test
?
@Test
? @Test
?I am asking for the best practices here, since both solutions aren't clean in my opinion. With the first solution, I would test the initialization code. And with the second solution I would break the "one test class for each class" pattern.
As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete.
The purpose of a test fixture is to ensure that there is a well-known and fixed environment in which tests are run so that results are repeatable. Test suites: Bundles a few unit test cases and runs them together.
Examples of fixtures: Preparation of input data and setup/creation of fake or mock objects. Loading a database with a specific, known set of data. Copying a specific known set of files creating a test fixture will create a set of objects initialized to certain states.
Tips:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With