Table of Contents
JUnit consolidates two words - Java and Unit Testing. Given the prominence of Java in software applications, JUnit has acquired serious prevalence. Throughout the long term, JUnit has helped in the improvement of a test-driven methodology. The GUI accessible in JUnit for composing repeatable and reasonable experiments makes it stand apart from other testing frameworks on the web. Through JUnit, engineers can run unit tests on every product part before it goes to testers.
Tests are run rapidly and failed tests are recorded in a different segment for simple troubleshooting. Other than manual testing, JUnit is favored similarly for automation testing. It can likewise be utilized alongside the Selenium WebDriver to automate tests for web applications. It gives an extraordinary method for composing structured, short, and better test cases. JUnit utilizes explanations to permit the production of various test scripts for different purposes utilizing a few techniques.
Understanding Automation Testing with JUnit [Explained with example]
Also Read: Data-Driven Testing with TestNG and Selenium Webdriver
JUnit tests can be run straightforwardly in IntelliJ, however they can likewise be run in other IDEs like Eclipse, NetBeans, or even the command line.
Tests ought to continuously run at assemble time, particularly unit tests. A form with any faltering tests ought to be thought of as fizzled, whether or not the issue is in the creation or the test code-this requires discipline from the group and an eagerness to give most elevated need to failing tests, however it's important to stick to the soul of automation.
JUnit tests can likewise be run and covered by persistent combination frameworks like Jenkins. Projects that utilization apparatuses like Gradle, Maven, or Insect enjoy the additional benefit of having the option to run tests as a component of the form cycle.
Why is JUnit Testing Framework Important
While JUnit is a primitive method for testing Java-based projects, it gives many benefits. Following are a couple of advantages of utilizing JUnit Testing Framework:-
- Open Source: JUnit is an open-source testing system. Subsequently, a more extensive local area can add to the product. That prompts better and quicker advancement from designers across the world
- Early bug-locater: JUnit finds the bug right off the bat in code when contrasted with other test systems. At the point when a bug is found, it is shown in a different segment until it is settled. This assists drive with focus in on troubleshooting.
- Best for Test-Driven Improvement (TDD) Environment: To push the least number of bugs to QA teams, many designing groups pick a test-driven improvement cycle. Engineers initially perform tests, and issues are settled, prior to taking the form for QA testing. JUnit involves attestations in the tests, and statements are most proficient when they fail. In this way, JUnit helps in the TDD work of the software.
Getting Started with Automation Testing using Junit
Automation testing refers to the most common way of testing the software through automated processes. A devoted tool is utilized for duplicating client activity and repeating test cases. Automation testing decreases human intercession and expands exactness and achievement pace of tests.
JUnit is best at making repeatable test cases. On the off chance that there is a need to run a test in JUnit multiple times, one doesn't have to compose it multiple times or not even execute it physically multiple times. This cycle is automated. Automation testing is generally liked over manual testing in view of quicker and dependable outcomes. Since the mistake edge is exceptionally less in automation testing, discussing JUnit without thinking about automation testing won't be sufficient.
This article centers around automation testing in JUnit with Selenium. Yet, prior to continuing, remember a webpage ought to be tried on various programs to distinguish any peculiarity or error ahead of time and to likewise improve the code and site. Since each program is unique and renders the site in an unexpected way, the components that make up the site work such that the manufacturer needs.
For example, the placeholder attribute was not accessible on Internet Explorer 6 prior. Notwithstanding, it was accessible in different browsers. There are many such issues, and an developer needs to deal with them. These issues are cross-browser issues, and testing it is known as cross-browser testing.
Therefore cross browser testing is fundamental in any testing pipeline. Each program manufacturer attempts to make their browser as remarkable and secure as could be expected. Because of this, they don't execute every one of the elements accessible in HTML, or they carry out fractional highlights. A site on one browser might come up short on highlights on the other because of these reasons. An developer needs to deal with this.
How to Perform JUnit Testing with Selenium
This part centers around testing the code with Selenium and JUnit. This sort of testing goes under automation testing since the work process, and the execution of the test is automated. Selenium and JUnit can be joined to make a strong test blend to test sites.
package unitTests; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class SimpleTest { private static FirefoxDriver driver; WebElement element; @BeforeClass public static void openBrowser(){ driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @Test public void valid_UserCredential(){ System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName()); driver.get("https://www.facebook.com"); driver.findElement(By.xpath(".//*[@id='login']/a")).click(); driver.findElement(By.id("log")).sendKeys("simple_3"); driver.findElement(By.id("pwd")).sendKeys("simple@123"); driver.findElement(By.id("login")).click(); try{ element = driver.findElement(By.xpath(".//*[@id='login_logout']/a")); }catch (Exception e){} Assert.assertNotNull(element); System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName()); } @Test public void inValid_UserCredential(){ System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName()); driver.get("https://www.facebook.com"); driver.findElement(By.xpath(".//*[@id='login']/a")).click(); driver.findElement(By.id("log")).sendKeys("simpleuser"); driver.findElement(By.id("pwd")).sendKeys("simple@123"); driver.findElement(By.id("login")).click(); try{ element = driver.findElement(By.xpath(".//*[@id='login_logout']/a")); }catch (Exception e){} Assert.assertNotNull(element); System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName()); } @AfterClass public static void closeBrowser(){ driver.quit(); } }
What Just Happened...
Let simply comprehend it block by block:-
a) Before Class Annotation
@BeforeClass public static void openBrowser() { driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); }
Explanation before class will advise Junit to run this piece of code prior to beginning any test. As you can see we began no browser in the test method 'valid_UserCredential()' and 'Invalid_UserCredential()'. So our test needs a browser to execute steps. With the assistance of the BeforeClass we would have the option to open the browser prior to running the test.
b) Test Method
@Test public void valid_UserCredential(){ System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName()); driver.get("https://www.facebook.com"); driver.findElement(By.xpath(".//*[@id='login']/a")).click(); driver.findElement(By.id("log")).sendKeys("simple_3"); driver.findElement(By.id("pwd")).sendKeys("simple@123"); driver.findElement(By.id("login")).click(); try{ element = driver.findElement (By.xpath(".//*[@id='login_logout']/a")); }catch (Exception e){ } Assert.assertNotNull(element); System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName()); }
This is a basic trial of Sign In usefulness of our Demo application. Yet, what you can see in it is try/catch block and affirm proclamation. This try block is to test the presence of the component, on the off chance that it doesn't find it the component will stay as null. With that declare articulation will check that the component isn't null. The fundamental rule of affirm articulation is that they act just on disappointment.
c) After Class Method
@AfterClass public static void closeBrowser(){ driver.quit(); }
This is to tell Junit engine that execute this piece of code once all the test has been executed. This will at last shut the browser down, as after this no test would be required any program to execute any steps.
Conclusion
In this article we learnt JUnit testing is the most favored testing technique on the off chance that the venture has been created in Java. It is strong and persistently developing for better experiment execution. It has turned into a favored decision for Test-driven development cycle.