JUnit test methods are designed to return ‘void’. So the execution will fail.
Manual testing is performed by humans, so it is time-consuming and costly. Automated testing is performed by testing tools or programs, so it is fast and less costly.
Test Suite usually refers to the principle of compiling a variety of unit test cases to run it concurrently. In this context, it is interesting to note that in JUnit, both Run With and Suite comments are being used to avail maximum benefits.
If there is a pop up for logging in, we need to use the explicit command and verify if the alert is actually present. The below code helps you understand the use of explicit wait commands.
1. WebDriverWait wait = new WebDriverWait(driver, 10);
2. Alert alert = wait.until(ExpectedConditions.alertIsPresent());
3. alert.authenticateUsing(newUserAndPassword(**username**, **password**));
If frame name and frame id is not available, then we can use frame by index. For example, there are 3 frames in a web page and if none of them have a frame name and frame id, then we can still select those frames by using frame (zero-based) index attribute. All the frames will have an index number like the first frame would be at index “0”, the second at index “1” and the third at index “2”.
efect cascading is a defect which is caused by another defect. One defect triggers the other defect. When a defect is present in any stage but is not identified, hide to other phases without getting noticed. This will result in an increase in the number of defects.
Let us understand this by an example.
You are designing the Login Module of a WebPage:
>> In phase 1 – You are designing a Register User Module for Login and mobile number is mandatory but you can leave it blank due to a bug that gets unnoticed.
>> In Phase 2 – You will design the login form having username and password. The password is OTP which will be sent to User’s registered mobile number.
Now as the Register module has a bug that mobile numbers can be left blank so this may lead to Login failure or maybe some system error or crash if a null mobile number is not handled. This is known as defect cascading.
To test the protected method, the test class is declared in the same package as the target class.
Object Repository refers to the collection of web elements belonging to Application Under Test (AUT) along with their locator values. With respect to Selenium, objects can be stored in an excel sheet which can be populated inside the script whenever required.
It is an advanced framework which is designed in a way to leverage the benefits by both the developers and testers. It also has an inbuilt exception handling mechanism which lets the program to run without terminating unexpectedly.
To access a website, a user sends a “request” to that website’s server, and the server sends back a response in the form of the website you want to access. To load test a website, quality assurance engineers and automation engineers just need to multiply the number of responses sent to simulate different traffic loads. The web server’s response to the influx of virtual users can then be measured. This is used to determine performance issues and server capacity.
Automation testing is very useful in agile methodology and helps in achieving maximum test coverage in a lesser time of the sprint.
Usually, black box test cases are written first and white box test cases later. To write black box test cases we need the requirement document and design or project plan. These documents are easily available at the initial start of the project. White box test cases cannot be started in the initial phase of the project because they need more architectural clarity which is not available at the start of the project. So normally white box test cases are written after black box test cases are written.
Long method, Large conditionals
There are different methods of exception handling in JUnit
<> Try catch idiom
<> With JUnit rule
<> With @Test annotation
<> With catch exception library
<> With customs annotation
Boundary Value Analysis (BVA) is a black box test design technique which is applied to see if there are any bugs at the boundary of the input domain.
1. Regression Testing: It is defined as a type of software testing to confirm that a recent code change has not adversely affected existing features.
2. Confirmation Testing: When a test fails because of the defect, the defect is reported. Then a new version of the software is submitted whose defect is fixed. This is called confirmation testing or re-testing.
A fixture is also known as a constant state of a collection of objects that can be used for the execution of tests. The main aim of using a test fixture lies in the fact that there should be a familiar and fixed environment. Moreover, these tests are run so that the results are repeatable. It comprises the following procedures.
<> The setup method which runs every test
<> The teardown procedure which runs after the execution of a particular test
Cactus is a simple test framework. It is for unit testing server-side java code such as servlets, EJBs and Tag Libs. The idea of Cactus is to minimize the cost of writing tests on server-side code. It uses Junit internally and extends the same. The way cactus implements it is through an in-container strategy.
Cactus Ecosystem is made of many components
Cactus Framework is the core of Cactus. To write Cactus tests, it has an API.
Cactus Integration Modules is another component which are front ends and frameworks that provide quicker ways of using the Cactus Framework be it Ant scripts or Eclipse plugin or Maven plugin.
Testing is always done after the build and execution phases Earlier we catch a defect, the more cost effective it is. For example, fixing a defect in maintenance is ten times more costly than fixing it during execution.
Selenium Grid can be used to execute same or different test scripts on multiple platforms and browsers concurrently so as to achieve distributed test execution, testing under different environments and saving execution time remarkably.
It is the direct way to find the element, but the disadvantage of the absolute XPath is that, if there are any changes made in the path of the element then that XPath gets failed. For example: /html/body/div/section/div/div
For Relative XPath, the path starts from the middle of the HTML DOM structure. It begins with the double forward slash (//), which means it can search the element anywhere at the webpage. For example: //input[@id=‘ap_email’]
Different components of Selenium are:
Selenium Integrated Development Environment (IDE)
Selenium Remote Control (RC)
Benefits of Automation testing are:
>> Supports execution of repeated test cases
>> Aids in testing a large test matrix
>> Enables parallel execution
>> Encourages unattended execution
>> Improves accuracy thereby reducing human-generated errors
>> Saves time and money
The parameter used in software testing to describe the extent to which the source code is tested is known as coverage. There are three basic types of coverage techniques and they are:
>> Statement coverage: It ensures that each line of source code has been executed and tested.
>> Decision coverage: It assures that every decision (true/false) in the source code has been executed and tested.
>> Path coverage: Here we ensure that every possible route through a given part of the code is executed and tested.
Basically, the acceptance document is prepared using the following inputs.
>> Requirement document: It specifies what exactly is needed in the project from the customers perspective.
>> Input from the customer: This can be discussions, informal talks, emails, etc.
>> Project plan: The project plan prepared by the project manager also serves as good input to finalize your acceptance test.
It is a testing methodology where the end customer is asked to use the software to see if the product is easy to use, to see the customer’s perception and task time. An accurate way to finalize the customer point of view for usability is by using prototype or mock-up software during the initial stages.
Verification: It is a static analysis technique. Here, testing is done without executing the code. Examples include – Reviews, Inspection, and walkthrough.
Validation: It is a dynamic analysis technique where testing is done by executing the code. Examples include functional and non-functional testing techniques.
In the V model, the development and QA activities are done simultaneously. There is no discrete phase called Testing, rather testing starts right from the requirement phase. The verification and validation activities go hand in hand.
Some of the advantages of automated testing are:
>> It is very fast.
>> Investment is very less.
>> Testing is more reliable.
>> The testing can be programmed.
No. We need not write an independent test class for every class that needs to be tested. If there is a small group of tests
sharing a common test fixture, you may move those tests to a new test class. If you have two groups of tests that you think you'd like
to execute separately from one another, it is wise to place them in separate test classes.
You should do this test to check if a property has already been set (in the constructor) at the point you wish to call getX().
In this case you must test the constructor, and not the getX() method. This kind of test is especially useful if you have multiple
Debugging the code using system.out.println() will lead to manual scanning of the whole output every time the program is
run to ensure the code is doing the expected operations. Moreover, in the long run, it takes lesser time to code Junit methods and test
them on our files.
JUnit TestCase is the base class, junit.framework.TestCase, that allows you to create a test case. (Although, TestCase class
is no longer supported in JUnit 4.4.)
A test case defines the fixture to run multiple tests. To define a test case
-Implement a subclass of TestCase
-Define instance variables that store the state of the fixture
-Initialize the fixture state by overriding setUp
-Clean-up after a test by overriding tearDown
To write a test case, follow these steps:
Define a subclass of TestCase.
Override the setUp() method to initialize object(s) under test.
Optionally override the tearDown() method to release object(s) under test.
Define one or more public testXYZ() methods that exercise the object(s) under test and assert expected results.
Tests are written before the code during development in order to help coders write the best code. Test-driven development
is the ideal way to create a bug free code. When all tests pass, you know you are done! Whenever a test fails or a bug is reported, we
must first write the necessary unit test(s) to expose the bug(s), and then fix them. This makes it almost impossible for that particular
bug to resurface later.
Junit provides a framework to achieve all the following-:
Test early and often automated testing.
Junit tests can be integrated with the build so regression testing can be done at unit level.
Test Code reusage.
Also when there is a transfer Junit tests act as a document for the unit tests.
Used by developers to implement unit tests in Java. Junit is designed for unit testing, which is really a coding process, not a
testing process. But many testers or QA engineers are also required to use Junit for unit testing.
Cactus is a simple test framework. It is for unit testing server-side java code such as servlets, EJBs and Tag Libs. The idea of Cactus is to minimize the cost of writing tests on server-side code. It uses Junit internally and extends the same. The way cactus implements are through an in-container strategy.
Cactus Ecosystem is made of many components −
1. Cactus Framework is the core of Cactus. To write Cactus tests, it has an API.
2. Cactus Integration Modules is another component which are front ends and frameworks that provide quicker ways of using the Cactus Framework be it Ant scripts or Eclipse plugin or Maven plugin.
1. Verify interactions 2. Test the block of code in isolation
Long methods, Long conditionals, mixing concerns, bulky constructors
Ideally you should not do this. All the JUnit test methods should have a void return type. If you change the return type then
the test method would not be considered as a test method and would be ignored during execution of tests.
Following are some of the JUnit extensions −
XMLUnit is used as a single JUnit extension class, XMLTestCase, and a set of supporting classes. Supporting classes assertions are made about following−
The distinction between two pieces of XML such as through Diff and DetailedDiff classes.
The validation of a piece of XML through the Validator class.
The result of transforming a piece of XML using XSLT through Transform class.
The evaluation of an XPath expression from an XML through classes that implement the XPath engine interface.
Individual nodes in a piece of XML which are exposed by DOM Traversal through Node Test class.
Assert works only if assertions ( -ea ) are enabled which is not required for Verify. Assert throws an exception and hence doesn’t continue with the test if assert evaluates to false whereas it’s not so with Verify.
JUnit Framework can be easily integrated with either of the followings − Eclipse
The JUnit runner will declare that test as fail when the test method throws an exception.
The private methods cannot be tested as any private method only be accessed within the same class. The private method need to tested manually or be converted to protected method.
Executng Junit from command line involves 2 steps. set the ClassPath to include JUnit core libraries. set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%junit.jar
2.Invoke JunitCore. java org.junit.runner.JUnitCore
There are different ways of exception handling in JUnit. Try catch idiom.
Using JUnit rule.
Using catch exception library.
Using customs annotation.
Test-along if not test-driven; Writing unit tests towards end is not very effective. Test-along technique recommends developers to write the unit tests as they go with their development.
The advantages of writing unit tests include Design testability, Code testability and Code maintainability as good unit tests enforces Object Oriented principles such as Single Responsibility etc. which enables people to avoid code smells such as long classes, long methods, large conditionals etc.