This set of JUnit Multiple Choice Questions & Answers (MCQs) 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
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
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
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
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
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
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
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
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?
public class TestFile
{
@Test
public void testMethod()
{
assertTrue(false);
assertTrue(false);
assertTrue(false);
}
}
a) Yes
b) No
View Answer
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
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, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Computer Science Internship
- Check Programming Books
- Practice Programming MCQs
- Check JUnit Books