Currently, I'm working on testing a Class (let's say Message for example) that has CRUD operations. Following the methodology used behind, I want to test all the operations in exact Create, Read, Update and Delete order.
Since the order of execution of test cases in JUnit is dependent on JVM, I need some proper way to ensure that the unit test cases are executed in the given order.
So far, I've come up with following strategies:
Using
@FixMethodOrder(MethodSorters.NAME_ASCENDING)in JUnit 4 to execute methods in ascending order of their names. However, this requires renaming test cases and didn't seem the best way to me as if in future more test case need to be added existing test cases may need to be refactored.Using different
Messageobject for each test case. However, I will need to create/initialize it before and delete it after each test case. This for me seems to go against the notion of unit-testing, as to testupdateI'll have to call create and delete, which no longer makes this a unit test case. Ifcreatefails, this would result inread,updateanddeletetests in failing as well, even when there's nothing wrong with them.Using a single test to call other test methods. This to me seems the more appropriate solution than other two. If
createfails, it should stop other three tests from running and such.
So is there any better way to achieve this or any alternative way to test such a model?
Currently using JUnit4 and thinking of moving to JUnit5 if no solution satisfies my case.