Download presentation
Presentation is loading. Please wait.
Published byJoel Smith Modified over 9 years ago
1
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel
2
2 Day 10 JUnit 4 Extensions
3
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 using @Prerequisite tag ◦ @Prerequisite(requires=“prerequisite_name”) Specified methods must be public, and return a boolean or Boolean value 3
4
public class TestFillDatabase { @Prerequisite(requires = "databaseIsAvailable") @Test public void fillData() { //... } public boolean databaseIsAvailable() { boolean isAvailable =...; return isAvailable ; } 4
5
Gets JUnit to inject test data into instances of your test class, via the constructor The test data is provided by a method with the @Parameters annotation TestCase must contain a constructor which takes the correct number, types of parameters. 5
6
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
7
@RunWith(Parameterized.class) public class SampleTest { private int number; private double result; @Parameters 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; } @Test public void shouldCalculateCorrectFee() { PremiumTweetsService premiumTweetsService = new PremiumTweetsService(); double calculatedFees = premiumTweetsService.calculateFeesDu e(numberOfTweets); assertThat(calculatedFees, is(expectedFee)); } 7
8
When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class. 8
9
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 with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class. 9
10
If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or Test method throws an exception. @After methods declared in superclasses will be run after those of the current class. 10
11
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 with @AfterClass causes that method to be run after all the tests in the class have been run. All @AfterClass methods are guaranteed to run even if a BeforeClass method throws an exception. @AfterClass methods declared in superclasses will be run after those of the current class. 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.