0

I'm using jUnit to test my business rules and I would like to execute an especific class first. In that class, I want to delete my database and create it again. I already implement that in a class, but it isn't executed first. From what I could understand the execution of those classes follow an alphabetical order (maybe I can be wrong). I want to create a class that delete and create my database again to not repeat code in other classes. Can I change that execution order?

  • @AedvaldTseh In this case, I want to change the classes execution order. To methods, I use the `@FixMethodOrder` annotation and nominated them alphabetically. – Marcelo Araujo Oct 13 '17 at 20:45

1 Answers1

2

Use @Before to define a method which is executed before a test method is run. Use this to prepare your database for your test methods.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Also @BeforeClass if you split things into suites. –  Oct 13 '17 at 21:06
  • @Progman I use it in some classes, but there are two classes that I need to delete my database and create it again. I didn't want to repeat code in those classes, that's why I want to create just a class that do all of these things and use my other test classes to execute other tests. – Marcelo Araujo Oct 13 '17 at 21:14
  • @MarceloAraujo You don't need to repeat code, you can extract it in a new method and call this method from your test methods. One test method (with the execution of `@Before*` and `@After*`) should run independently as written in https://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4 – Progman Oct 13 '17 at 21:27
  • @Progman My ideia is just create one class to delete and create my database. I have two classes that needs to do it, but I don't want to keep it like that. I just want to keep ONE class to do that operations. Did you understand? – Marcelo Araujo Oct 14 '17 at 01:37
  • @MarceloAraujo No problem in that, you just call the method like any other method like `YourOtherClass.deleteDatabase()` with a static method call. – Progman Oct 14 '17 at 08:57
  • @Progman it's a good ideia. I will try it and I tell my progress here. Thanks a lot. – Marcelo Araujo Oct 14 '17 at 21:15