Many people get confused when they read about the
@Factory annotations – what to use when? and what is better?
Let's take a look at both of their functionalities:
- DataProvider: A test method that uses DataProvider will be executed a multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.
- Factory: A factory will execute all the test methods present inside a test class using a separate instance of the respective class.
Let's create an example that shows the clear difference between these two.