• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit: How to run both @BeforeEach from 2 files that are in same package.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am working with JUnit and completely stuck on a problem.
I have two test files that inherit a "JUnitTestBase" file. Both of these test files have an @Override and @BeforeEach method as shown below. I want both of these @BeforeEach to pass arguments to the base class's method when the tests run. But turns out that ONLY ONE @BeforeEach is called from one package even though there are two files with the @BeforeEach. I have tried creating an Abstract class that extends the JUnitTestBase and extending that class from those test files but that didn't work either. Are there any techniques that can be used to pass the arguments from child class to the base class from the each of the methods that have @BeforeEach on both files?



Since both of them are in the same package. The @BeforeEach is run for only one of the test files, but as you can see, I need to run both of the @BeforeEach or use some other technique in order to pass the login credentials to the parent class JUnitTestBase. And it must be BEFORE any of the tests in the file is run.
Any help would be highly appreciated.
 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , you mean the initDriver() method of the ChildClass2 is not called at all?
 
Saloon Keeper
Posts: 15547
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

@BeforeEach methods run before each @Test method is called. Where are your @Test methods?
 
Himai Minh
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a @Test method in each class. But I am not sure if that is the correct way to define it like this:
@Test
driver.get(url).

It needs to be:
@Test
public String testGet(String url){

//perform test using assertTrue and etc.
}
 
Saloon Keeper
Posts: 27816
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The @BeforeEach methods should NOT run tests. They are intended to set up the environment that must be in place before running each individual test run once per test. Likewise @AfterEach tears down stuff after each test runs.

As Hmai has said, a proper Junit test is annotated with @Test, it returns void and its method name should begin with "test". The Junit framework will then automatically run the before-all, before-each, the test itself(!) and "after" methods. A lot of times you'll open a test database connection before all and close it after all rather than before each test. On the other hand, before-each is good if you want to reset the contents of table(s) before running each test. Although there are also database test support frameworks that handle that at a higher level, if you prefer.

If you want to "pass a parameter" from a "before" method to a unit test method, set a class instance* property in the test class in the "before" method and consume it in the test.

---
* Someone: is "before all" an instance method or a class method? I'm too lazy to check.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic