This set of JUnit Multiple Choice Questions & Answers (MCQs) focuses on “Running Parameterized Tests”.
1. What are Parameterised tests used for in JUnit?
a) Run a test many times with different sets of parameters
b) Run a test with no parameters
c) Run a test with only String parameters
d) Run a test once with fixed set of parameters
View Answer
Explanation: The Parameterized test runner allows to run a test many times with different sets of parameters.
2. A parameterised test class must carry which annotation?
a) @Test
b) @ParameterisedClass
c) @Runwith
d) @Class
View Answer
Explanation: A class that is annotated with @RunWith or extends a class that is annotated with @RunWith, JUnit will invoke the class it has referenced to run the tests in that class instead of the runner that is built into JUnit.
3. The test class must carry the @RunWith annotation with the ______ class as its argument.
a) Default
b) Parameterised
c) Super
d) Inherited
View Answer
Explanation: “Parameterised.class” should be the argument. The full syntax is @RunWith(value=Parameterized.class).
4. What will be the outcome for the following piece of code?
//The JUnit files are imported
public class TestClass {
@Test
public void testingMethod() {
String message = “Test”;
assertEquals(3,message.length());
}
}
a) Compilation Error
b) Runtime Error
c) Test Ran with Success
d) Test Ran with Failure
View Answer
Explanation: The string “Test” has a length of 4 whereas the expected value is 3.
5. What does the fail() method do in JUnit?
a) Throws an assertion error unconditionally
b) Calls the default constructor
c) Outputs the message “Fail” to the console
d) Pauses the test for 1 second
View Answer
Explanation: The method throws an assertion error unconditionally. This might be helpful to show an incomplete test (maybe still being worked upon) or to ensure that an expected exception is thrown.
6. Which annotation must be used to define suite classes?
a) @RunWith
b) @SuiteClasses
c) @Suite
d) @SuiteClass
View Answer
Explanation: The SuiteClasses annotation is used to specify the classes to be run when a class that is annotated with @RunWith(Suite.class) has been run.
7. When is the tearDown() method called in JUnit?
a) After all the tests have run
b) At the beginning of every test case
c) After each test case has run
d) At the beginning of the first test case
View Answer
Explanation: The tearDown() method is called after the execution of every @Test method.
8. What does the assertTrue(“message”,A) do?
a) Asserts that the condition A is true
b) Asserts that “message” = A
c) Asserts that A contains “message”
d) Asserts that the condition A is false
View Answer
Explanation: assertTrue requires A to be a boolean expression and the “message” is displayed.
9. How can a method be made to run before the execution of every test case?
a) Annotate the method with @Before
b) Prefix the method name with startfirst
c) Annotate the method with a @BeforeClass
d) Such a method cannot be made
View Answer
Explanation: Annotating a method with @Before implies that method to be run before the every test method. The @Before methods of superclasses are run before those of the current class.
10. Which method from TestCase class returns the name of a Test case?
a) String testCaseName()
b) String getTest()
c) String getTestCaseName()
d) String getName()
View Answer
Explanation: getName() returns a string denoting the name of the TestCase.
Sanfoundry Global Education & Learning Series – JUnit.
To practice all areas of Junit, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Programming Books
- Check JUnit Books
- Practice Programming MCQs
- Apply for Computer Science Internship