Testing / Junit Interview questions
The assert keyword is used in assert statement which is introduced in Java 1.4.
It helps the code developers to test assumptions and to assert if the outcome is as expected that enables detecting bug and fixing it.
public class AssertExample { public static void main(String[] args) { assert 0 > 1; System.out .println("This line is printed since -ea or -enableassertions is not enabled as VM argument."); } }
Format:
assert condition;
assert boolean_expression : string_expression;
assert <condition>: "Error message to display when condition failed";
When this statement is executed:
- If boolean_expression evaluates to true, the statement will pass normally.
- If boolean_expression evaluates to false, the statement will fail with an "AssertionError"
if (!<condition>)
throw new AssertionError();
Unit Testing refers to the process of verifying/testing the individual components, functional units of any application (methods/procedures/classes) that contains business logic, reusable components and ensure the working of the functional units.
Unit testing can be done automatically or manual.
Manual testing involves the preparation of test cases and those are tested by developers.
Automated Unit testing: Test cases are executed by automation tools such as JUnit.
Automated Unit testing is preferred over manual, as Automated test helps covering all the scenario/test any time required. Automated unit testing is fast compared to Manual, less expensive and accurate.
refers to the combination of possible inputs to the unit and desired output of based on the input, that helps to certify the functionality of the individual unit.
Unit test cases are prepared and used by Developers.
JUnit4 relies on annotations whereas JUnit3 use method names to identify tests.
Junit is an open source testing framework developed for unit testing Java based applications.
An API that enables developer to writes and executed repeatable automated test cases.
Enables writing test cases while developing the software that helps test early and detect issues.
Ensure the functionality is performing as expected every time the code is modified by the use of repeatable automated test cases.
supported by all IDE including Eclipse, Netbeans, RAD etc.
integrates with Ant and Maven(CI) that enables execuition of test suites or test cases as part of the build process, capturing test result and reporting.
accurate and efficient testing process.
in JUnit3, Java classes should extend junit.framework.TestCase .
JUnit 4, any public class with a no arg public constructor can behave as a JUnit test class.
in JUnit3, test methods should follow the naming convention as the pattern test
In Junit 4, test methods would be just annotated with @Test.
All the JUnit test methods should have a void return type. otherwise it will be ignored.
If the return type of a test method is changed, it would not be considered as a test method and would be ignored during execution of tests.
All test methods must be declared as public.
Junit5 published on March 21, 2016.
in Java, JUnit 4 and TestNG are popular unit test framework and look very similar in functionality.
TestNG supports annotation @BeforeSuite that enables running a method before even the suite runs whereas Junit 4 does not support.
TestNG supports @AfterSuite that enables running a method after the suite completes running whereas Junit 4 does not support.
Similarly TestNG adds support for test specific and groups specific before (setup )and After (teardown) annotation whereas Junit does not support.
@Test annotation is used to denote that the method is a test method.
@Ignore annotation ignore the annotated test method to be executed.
It is used when the test method is incomplete or needed to be not executed.
@Test.
The Test annotation indicates that the public void method to which it is attached can be run as a test case.
@Before.
The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.
@BeforeClass.
The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class.
@After.
The After annotation indicates that this method gets executed after execution of each test.
@AfterClass.
The AfterClass annotation can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the set-up.
@Ignores.
The Ignore annotation can be used when you want temporarily disable the execution of a specific test.
Junit 3
public void testGetElement() { try { (new ArrayList()).get(0); fail("The test should have failed"); } catch (NullPointerException e) { assertTrue("Sanity check", true); } }
Junit 4
Junit 4.x
@Test(expected = IndexOutOfBoundsException.class) public void testGetElement() { (new ArrayList()).get(0); }
@Before public void runBeforeEachTest()
This method gets executed before each test execution in the current Test class. This method usually helps initialize the resources required for the action test to execute. It is executed everytime before running a test method.
@BeforeClass public static void runBeforeAllTheTest()
This method executes before running any of the test in the current Test Class. It is executed only once.
Used for setuping database connectivity before running any of the test.
This method should be declared static whereas @Before don't have to be.
@After public void runAfterEachTest()
This method gets executed after every test and it is used to clean up the test and temporary data that are used by test.
@AfterClass public static void runAfterAllTheTest()
This method gets executed at the end, when all the tests have completed execution and gets executed only once.
Usually used for closing the database connection.
Method should be declared static.