JUnit Questions and Answers – Composing Tests with a Suite


This set of JUnit Questions and Answers for Experienced people focuses on “Composing Tests with a Suite”.

1. The ______ is a container used to gather tests for the purpose of grouping and invocation.
a) Result
b) TestCase
c) Suite
d) Test
View Answer

Answer: c
Explanation: JUnit provides test Suite to facilitate the task of running more than one test file.

2. What happens if the tester does not define a Suite?
a) The test runner automatically creates a Suite
b) Compilation Error
c) Every test fails
d) Every test passes
View Answer

Answer: a
Explanation: The default Suite scans the test class for any methods annotated with @Test. Internally, the default Suite creates an instance of the test class for each @Test method. JUnit then executes every @Test method independently from the others to avoid potential side effects.

3. The Suite object is a _____ that executes all of the @Test annotated methods in the test class.
a) Result
b) FolderConfigurationTest
c) FileConfigurationTest
d) Runner
View Answer

Answer: d
Explanation: Suite is under org.junit.runners.Suite, Suite objects are Runners.

4. Suite class is the JUnit 4 equivalent of what feature of JUnit 3.8.x?
a) static Test suite() method
b) Test suite() method
c) static void suite() method
d) void suite() method
View Answer

Answer: a
Explanation: The suite is made accessible to a TestRunner tool with the static method suite() which returns a Test suite.

5. For a Suite class, the @RunWith annotation has the value of which class?
a) org.junit.runners.class
b) org.junit.Suite.class
c) org.runners.Suite.class
d) org.junit.runners.Suite.class
View Answer

Answer: d
Explanation: “org.junit.runners.Suite.class” contains the definition for the Suite class and a the declaration is @RunWith(value=org.junit.runners.Suite.class).

6. Which annotation is used to list all the classes in a suite?
a) @RunWith
b) @SuiteClasses
c) @Classses
d) @SuiteClass
View Answer

Answer: b
Explanation: The SuiteClasses annotation specifies the classes to be run when a class annotated with @RunWith(Suite.class) is run. The formal definition of the annotation is “Annotation Type Suite.SuiteClasses”.

7. If we want to run test files Test1 and Test2 together, the @SuiteClasses annotation will be?
a) @SuiteClasses(value={Test1.class,Test2.class})
b) @SuiteClasses(value=All)
c) @SuiteClasses(Test1, Test2);
d) @SuiteClasses()
View Answer

Answer: a
Explanation: The classes we want to test together are given as a tuple to the value of the SuiteClasses annotation.

8. JUnit Suites are independent of the capability of the ______ system.
a) Run
b) Class
c) Test
d) Build
View Answer

Answer: d
Explanation: JUnit Suites are useful to organize tests in Java, independent of the capability of the build system, because it’s common for someone or a group other than the developers to maintain builds.

9. Will the second and third assert failures be reported?

  1. 	public class TestFile 
  2.         {
  3. 		@Test
  4. 		public void testMethod()
  5.                 {
  6. 			assertTrue(false);
  7. 			assertTrue(false);
  8. 			assertTrue(false);
  9. 		}
  10. 	}

a) Yes
b) No
View Answer

Answer: b
Explanation: JUnit only reports the first failure in a single test.

10. Which attribute is added to the @Test annotation so that the test passes when an expected exception is thrown?
a) exception
b) throws
c) expected
d) expectedException
View Answer

Answer: c
Explanation: @Test(expected=IndexOutOfBoundsException.class) is the example of a method expecting the IndexOutOfBoundsException.

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

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