JUnit Questions and Answers – Running Parameterized Tests

«
»

This set of JUnit Questions and Answers for Freshers 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

Answer: a
Explanation: The Parameterized test runner allows to run a test many times with different sets of parameters.
advertisement

2. A parameterised test class must carry which annotation?
a) @Test
b) @ParameterisedClass
c) @Runwith
d) @Class
View Answer

Answer: c
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

Answer: b
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?

  1.   //The JUnit files are imported
  2. 	public class TestClass {
  3. 		@Test
  4. 		public void testingMethod() {
  5. 			String message = “Test”;
  6. 			assertEquals(3,message.length());
  7. 		}
  8. 	}

a) Compilation Error
b) Runtime Error
c) Test Ran with Success
d) Test Ran with Failure
View Answer

Answer: d
Explanation: The string “Test” has a length of 4 whereas the expected value is 3.
advertisement

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

Answer: a
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

Answer: b
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

Answer: c
Explanation: The tearDown() method is called after the execution of every @Test method.
advertisement

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

Answer: a
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

Answer: a
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

Answer: d
Explanation: getName() returns a string denoting the name of the TestCase.

Sanfoundry Global Education & Learning Series – JUnit.
To practice all areas of JUnit for Freshers, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn