Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What Happens If A Test Method Throws An Exception?

Answer»

If you write a TEST method that throws an exception by itself or by the method being tested, the JUnit RUNNER will DECLARE that test as FAIL.

The example test below is designed to let the test fail by throwing the uncaught IndexOutOfBoundsException exception:

import org.junit.*;
import java.util.*;
public class UnexpectedExceptionTest2 {
// throw any unexpected exception
@Test public void testGet() throws Exception {
ARRAYLIST emptyList = new ArrayList();
Exception anyException = null; // don't catch any exception
Object o = emptyList.get(1); }
}

If you write a test method that throws an exception by itself or by the method being tested, the JUnit runner will declare that test as fail.

The example test below is designed to let the test fail by throwing the uncaught IndexOutOfBoundsException exception:

import org.junit.*;
import java.util.*;
public class UnexpectedExceptionTest2 {
// throw any unexpected exception
@Test public void testGet() throws Exception {
ArrayList emptyList = new ArrayList();
Exception anyException = null; // don't catch any exception
Object o = emptyList.get(1); }
}

2.

Do You Need To Write A Main () Method Compulsorily In A Junit Test Case Class?

Answer»

No. But still developers write the MAIN() method in a JUnit test case class to call a JUnit test runner to run all tests defined in this class like:

PUBLIC static void main(String[] args) {
junit.textui.TestRunner.run(Calculator.class);
}

Since you can call a JUnit runner to run a test case class as a system command, explicit main() for a Junit test case is not recommended. junit.textui.TestRunner.run() method takes the test class name as its argument. This method automatically finds all class methods whose name STARTS with test. Thus it will RESULT in below MENTIONED findings:

  1. testCreateLogFile()
  2. testExists()
  3. testGetChildList()

It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and give the result in console.

No. But still developers write the main() method in a JUnit test case class to call a JUnit test runner to run all tests defined in this class like:

public static void main(String[] args) {
junit.textui.TestRunner.run(Calculator.class);
}

Since you can call a JUnit runner to run a test case class as a system command, explicit main() for a Junit test case is not recommended. junit.textui.TestRunner.run() method takes the test class name as its argument. This method automatically finds all class methods whose name starts with test. Thus it will result in below mentioned findings:

It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and give the result in console.

3.

How Do You Test A “private” Method?

Answer»

A private METHOD only be ACCESSED within the same class. So there is no WAY to TEST a “private” method of a target class from any test class. A way out is that you can perform UNIT testing manually or can change your method from “private” to “protected”.

A private method only be accessed within the same class. So there is no way to test a “private” method of a target class from any test class. A way out is that you can perform unit testing manually or can change your method from “private” to “protected”.

4.

How Do You Test A “protected” Method?

Answer»

A PROTECTED method can only be ACCESSED within the same PACKAGE where the class is defined. So, testing a protected method of a target class MEANS we need to define your test class in the same package as the target class.

A protected method can only be accessed within the same package where the class is defined. So, testing a protected method of a target class means we need to define your test class in the same package as the target class.

5.

For Which Conditions, The Methods Get () And Set () Can Be Left Out For Testing?

Answer»

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 constructors.

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 constructors.

6.

The Methods Get () And Set () Should Be Tested For Which Conditions?

Answer»

Unit tests PERFORMED on java code should be designed to target areas that might BREAK. Since the set() and get() METHODS on simple DATA types are unlikely to break, there is no need to test them explicitly. On the other hand, set() and get() methods on complex data types are vulnerable to break. So they should be TESTED.

Unit tests performed on java code should be designed to target areas that might break. Since the set() and get() methods on simple data types are unlikely to break, there is no need to test them explicitly. On the other hand, set() and get() methods on complex data types are vulnerable to break. So they should be tested.

7.

Why Not Just Use System.out.println () For Unit Testing?

Answer»

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.

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.

8.

What Happens If A Junit Test Method Is Declared To Return “string”?

Answer»

If a JUNIT TEST METHOD is DECLARED to return “String”, the compilation will pass ok. But the execution will fail. This is because Junit requires that all test methods must be declared to return “void”.

If a Junit test method is declared to return “String”, the compilation will pass ok. But the execution will fail. This is because Junit requires that all test methods must be declared to return “void”.

9.

What Happens If A Junit Test Method Is Declared As “private”?

Answer»

If a JUNIT test method is declared as “PRIVATE”, the compilation will PASS ok. But the execution will FAIL. This is because Junit REQUIRES that all test methods must be declared as “public”.

If a Junit test method is declared as “private”, the compilation will pass ok. But the execution will fail. This is because Junit requires that all test methods must be declared as “public”.

10.

What Is Junit Test Fixture?

Answer»

A test fixture is a fixed state of a set of objects used as a baseline for running tests. Their purpose is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Examples of fixtures:

  • Loading a database with a SPECIFIC, known set of data
  • Copying a specific known set of files
  • Preparation of input data and setup/creation of FAKE or mock objects

If a group of tests shares the same fixtures, you should WRITE a SEPARATE setup code to create the common test fixture. If a group of tests REQUIRES different test fixtures, you can write code inside the test method to create its own test fixture.

A test fixture is a fixed state of a set of objects used as a baseline for running tests. Their purpose is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Examples of fixtures:

If a group of tests shares the same fixtures, you should write a separate setup code to create the common test fixture. If a group of tests requires different test fixtures, you can write code inside the test method to create its own test fixture.

11.

What Is Junit Testsuite?

Answer»

JUNIT TestSuite is a container CLASS under package junit.framework.TestSuite. It allows us to group multiple TEST cases into a COLLECTION and run them together.

JUnit TestSuite is a container class under package junit.framework.TestSuite. It allows us to group multiple test cases into a collection and run them together.

12.

What Is Junit Testcase?

Answer»

JUNIT TestCase is the base class, junit.framework.TestCase, that ALLOWS you to create a test case.

A test case defines the fixture to run multiple tests. To DEFINE a test case

  • mplement a subclass of TestCase
  • efine instance variables that STORE the state of the fixture
  • nitialize the fixture state by overriding SETUP
  • lean-up after a test by overriding tearDown

Each test runs in its own fixture so there can be no side effects among test runs.

JUnit TestCase is the base class, junit.framework.TestCase, that allows you to create a test case.

A test case defines the fixture to run multiple tests. To define a test case

Each test runs in its own fixture so there can be no side effects among test runs.

13.

How To Write A Simple Junit Test Class?

Answer»

To write a test case, follow these steps:

  1. Define a subclass of TestCase.
  2. Override the SETUP() METHOD to initialize object(s) under test.
  3. 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.

To write a test case, follow these steps:

Define one or more public testXYZ() methods that exercise the object(s) under test and assert expected results.

14.

What Are Unit Tests?

Answer»

A UNIT test is nothing more than the CODE wrapper around the application code that can be executed to view PASS – fail RESULTS.

A unit test is nothing more than the code wrapper around the application code that can be executed to view pass – fail results.

15.

Why Do You Use Junit To Test Your Code?

Answer»

Junit provides a framework to achieve all the FOLLOWING-:

  1. Test early and often automated TESTING.
  2. Junit TESTS can be INTEGRATED with the build so regression testing can be done at unit level.
  3. Test Code reusage.
  4. Also when there is a transfer Junit tests act as a DOCUMENT for the unit tests.

Junit provides a framework to achieve all the following-:

16.

Who Should Use Junit, Developers Or Testers?

Answer»

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.

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.

17.

How Can We Do Coverage Testing Using Vsts?

Answer»

Code coverage is a 3 STEPS process as shown below. The first step is to enable code coverage. So right CLICK on the ‘.testrunconfig’ file in the solution explorer.

The next step is to select the assembly / DLL which we want to monitor for code coverage.

Once you RUN the test, right click on the test results and select code coverage results. You will be shown a details result.

Code coverage is a 3 steps process as shown below. The first step is to enable code coverage. So right click on the ‘.testrunconfig’ file in the solution explorer.

The next step is to select the assembly / DLL which we want to monitor for code coverage.

Once you run the test, right click on the test results and select code coverage results. You will be shown a details result.

18.

How Can We Make The Automated Test Data Driven In Vsts?

Answer»
  • Once you have created the web TEST, right click on the web test and click add data SOURCE.
  • Once you have added the data source you can then specify the database fields as inputs to the text boxes.
  • We need to perform one more step to ensure that the data DRIVEN test runs FINE. Right click on the testrunconfig file and select one per data row.
  • Once you are done you can run the test and see how VSTS PICKS up row by row test cases and executes the test. 

19.

How Can We Do Automated Testing Using Vsts?

Answer»

In order to do automate testing in VSTS we need use the ‘WEB TEST’ template. So click on add new test and select ‘Web test’.

Once you have selected web test browser will open with a RECORD, pause and stop button as shown in the below figure. Once you START posting and requesting, the URL recorder starts recording every request and RESPONSE as shown in the below figure. On the right you can see how the recorder has recorded all the get and posts.

Below is a snapshot of simple login screen which is recorded. There are two requests, the first request is for the login page and the second request is when you post the login page with userid and password. You can see the values i.e. ‘Admin’ and ‘Admin’ in userid and password textboxes.

In order to do automate testing in VSTS we need use the ‘Web test’ template. So click on add new test and select ‘Web test’.

Once you have selected web test browser will open with a record, pause and stop button as shown in the below figure. Once you start posting and requesting, the URL recorder starts recording every request and response as shown in the below figure. On the right you can see how the recorder has recorded all the get and posts.

Below is a snapshot of simple login screen which is recorded. There are two requests, the first request is for the login page and the second request is when you post the login page with userid and password. You can see the values i.e. ‘Admin’ and ‘Admin’ in userid and password textboxes.

20.

How Can We Create Data Driven Unit Test Using Vsts Test?

Answer»

Creating data DRIVEN unit TEST is pretty simple in VSTS. First create a simple table with test data values. For instance you can see in the below figure we have created a table with 3 fields i.e. 2 input fields and 1 expected value. To consume this table apply ‘DataSource’ attribute with a proper database connection string as shown in the below figure. To get FIELD data from the table we can use the ‘DataRow’ with index.

We have 3 test cases in the table, so when we executed the above test’s we got the below test results in our visual studio IDE result window.

Creating data driven unit test is pretty simple in VSTS. First create a simple table with test data values. For instance you can see in the below figure we have created a table with 3 fields i.e. 2 input fields and 1 expected value. To consume this table apply ‘DataSource’ attribute with a proper database connection string as shown in the below figure. To get field data from the table we can use the ‘DataRow’ with index.

We have 3 test cases in the table, so when we executed the above test’s we got the below test results in our visual studio IDE result window.

21.

How Can We Do Unit Testing Using Vsts Test?

Answer»

In order to do unit testing USING VSTS right click on the visual studio solution explorer and select add new project. In the TEST project right click and select add new test you should be popped with a dialog box as shown in the below figure. From the dialog box select ‘Basic Unit test’.

In order to create a function which will execute our unit test you need to attribute the function with “[TestMethod]”. If you remember in Nunit it was ‘[Test]’. The assert function does not CHANGE.

Once you have done the unit testing coding compile the same, click on test, run and select ‘Tests in the current context”.

Once you do the previous test, the test starts running from PENDING to in PROGRESS and finally showing the results.

 

In order to do unit testing using VSTS right click on the visual studio solution explorer and select add new project. In the test project right click and select add new test you should be popped with a dialog box as shown in the below figure. From the dialog box select ‘Basic Unit test’.

In order to create a function which will execute our unit test you need to attribute the function with “[TestMethod]”. If you remember in Nunit it was ‘[Test]’. The assert function does not change.

Once you have done the unit testing coding compile the same, click on test, run and select ‘Tests in the current context”.

Once you do the previous test, the test starts running from pending to in progress and finally showing the results.

 

22.

How Can We Make Nunit Test Cases Data Driven?

Answer»

In the previous questions we had hardcoded the test data in the NUNIT test case itself. But in real time you would like to have test data inputs coming from a XML file or a database, in other words you would like to create data driven test cases. In order to create data driven in NUNIT we can attribute the UNIT test method with ‘TestCaseSource’ attribute as shown in the below figure. In ‘TestCaseSource’ we need to provide the function which will return the test case data, in this case it is ‘TestCases’.

Below is the code snippet which provides DYNAMIC data to unit test method. The function which is providing the dynamic data should return ‘IEnumerable’. NUNIT provides something called as ‘TestCaseData’ class. This class defines the test case data for NUNIT. The class provides ways by which you can specify input and the expected OUTPUT from the test case. Finally to return test case by test case we need to use ‘yield’ keyword with for each. You can SEE the below code snippet to understand how yield works.

In the previous questions we had hardcoded the test data in the NUNIT test case itself. But in real time you would like to have test data inputs coming from a XML file or a database, in other words you would like to create data driven test cases. In order to create data driven in NUNIT we can attribute the unit test method with ‘TestCaseSource’ attribute as shown in the below figure. In ‘TestCaseSource’ we need to provide the function which will return the test case data, in this case it is ‘TestCases’.

Below is the code snippet which provides dynamic data to unit test method. The function which is providing the dynamic data should return ‘IEnumerable’. NUNIT provides something called as ‘TestCaseData’ class. This class defines the test case data for NUNIT. The class provides ways by which you can specify input and the expected output from the test case. Finally to return test case by test case we need to use ‘yield’ keyword with for each. You can see the below code snippet to understand how yield works.

23.

Can We Start With A Simple Nunit Example?

Answer»

Ok so let’s say we want to do unit testing for a simple class ‘clsInvoiceCalculation’ shown below. This class calculates total cost by taking PER PRODUCT cost and NUMBER of products as input.

public class clsInvoiceCalculation
{
public int CalculateCost(int intPerProductCost, int intNumberOfProducts)
{
return intPerProductCost * intNumberOfProducts;
}
}

Let’s say we want to execute the below TEST case on the above class using NUNIT.

Ok so let’s say we want to do unit testing for a simple class ‘clsInvoiceCalculation’ shown below. This class calculates total cost by taking per product cost and number of products as input.

public class clsInvoiceCalculation
{
public int CalculateCost(int intPerProductCost, int intNumberOfProducts)
{
return intPerProductCost * intNumberOfProducts;
}
}

Let’s say we want to execute the below test case on the above class using NUNIT.

24.

What Are The Different Ways By Which You Can Do Unit Testing With .net?

Answer»

There are 2 PRIMARY / accepted ways of doing unit testing:-

There are 2 primary / accepted ways of doing unit testing:-