Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel
2 Day 10 JUnit 4 Extensions
At times test cases requires some prerequisites ◦ Online in internet ◦ Database is available ◦ Database is filled with required test data Best practice is to check this during running tests. Done tag Specified methods must be public, and return a boolean or Boolean value 3
public class TestFillDatabase = public void fillData() { //... } public boolean databaseIsAvailable() { boolean isAvailable =...; return isAvailable ; } 4
Gets JUnit to inject test data into instances of your test class, via the constructor The test data is provided by a method with annotation TestCase must contain a constructor which takes the correct number, types of parameters. 5
This method needs to return a collection of arrays, but beyond that you can implement it however you want. We create an embedded array in the Java code. However, we can also get data from other sources. 6
@RunWith(Parameterized.class) public class SampleTest { private int number; private double public static Collection data() { return Arrays.asList(new Object[][] { { 0, 0.00 }, { 50, 5.00 }, { 99, 9.90 } }); } public SampleTest(int number, double result) { super(); this.number = number; this.result = result; public void shouldCalculateCorrectFee() { PremiumTweetsService premiumTweetsService = new PremiumTweetsService(); double calculatedFees = premiumTweetsService.calculateFeesDu e(numberOfTweets); assertThat(calculatedFees, is(expectedFee)); } 7
When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method causes that method to be run before the Test method. methods of superclasses will be run before those of the current class. 8
Sometimes several tests need to share computationally expensive setup (like logging into a database). This can compromise the independence of tests, but it may be necessary optimization. Annotating a public static void no-arg method causes it to be run once before any of the test methods in the class. methods of superclasses will be run before those the current class. 9
If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method causes that method to be run after the Test method. methods are guaranteed to run even if a Before or Test method throws an exception. methods declared in superclasses will be run after those of the current class. 10
If you allocate expensive external resources in a BeforeClass method you need to release them after all the tests in the class have run. Annotating a public static void method causes that method to be run after all the tests in the class have been run. methods are guaranteed to run even if a BeforeClass method throws an exception. methods declared in superclasses will be run after those of the current class. 11