The @Suite annotation groups multiple test classes to run them together as one test suite, making it easier to organize and execute related tests.
Click to reveal answer
beginner
How do you specify which test classes belong to a test suite using @Suite?
You use the @Suite.SuiteClasses annotation with an array of test class names inside the test suite class.
Click to reveal answer
beginner
Write a simple example of a JUnit test suite using @Suite.
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class MyTestSuite {
}
This runs TestClass1 and TestClass2 together.
Click to reveal answer
beginner
What annotation must be used on a class to make it a test suite in JUnit?
The class must be annotated with @RunWith(Suite.class) to tell JUnit to run it as a suite.
Click to reveal answer
beginner
Can a test suite class contain test methods?
No, a test suite class only groups other test classes and does not contain test methods itself.
Click to reveal answer
Which annotation is used to specify the classes included in a JUnit test suite?
A@Suite.SuiteClasses
B@TestClasses
C@RunWithClasses
D@IncludeTests
✗ Incorrect
The @Suite.SuiteClasses annotation lists the test classes to include in the suite.
What annotation must be placed on a class to run it as a JUnit test suite?
A@TestSuite
B@SuiteRunner
C@RunWith(Suite.class)
D@RunSuite
✗ Incorrect
The @RunWith(Suite.class) annotation tells JUnit to run the class as a test suite.
Can a JUnit test suite class contain test methods?
AYes, it can contain test methods.
BOnly if it extends a test class.
COnly if annotated with @Test.
DNo, it only groups test classes.
✗ Incorrect
A test suite class only groups other test classes and does not contain test methods.
What is the main benefit of using a test suite with @Suite in JUnit?
ATo run multiple test classes together easily.
BTo write fewer test methods.
CTo automatically generate test reports.
DTo skip tests during execution.
✗ Incorrect
Test suites help run multiple test classes together in one go.
Which import is necessary to use @Suite in JUnit 4?
Aimport org.junit.suite.Suite;
Bimport org.junit.runners.Suite;
Cimport org.junit.runner.Suite;
Dimport org.junit.suites.Suite;
✗ Incorrect
The correct import for @Suite is org.junit.runners.Suite.
Explain how to create a test suite in JUnit using @Suite and what annotations are required.
Think about how you tell JUnit to run multiple classes together.
You got /3 concepts.
Describe the benefits of grouping tests into a suite with @Suite in JUnit.
Consider how running tests together helps in real projects.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using @Suite in JUnit testing?
easy
A. To group multiple test classes to run together as one unit
B. To mark a method as a test case
C. To ignore a test class during execution
D. To specify the order of test methods within a class
Solution
Step 1: Understand the role of @Suite
@Suite is an annotation used to create a test suite that groups multiple test classes.
Step 2: Compare with other annotations
@Test marks methods as tests, @Ignore skips tests, and ordering is handled differently, so these do not match @Suite's purpose.
Final Answer:
To group multiple test classes to run together as one unit -> Option A
Quick Check:
@Suite groups classes = B [OK]
Hint: Remember: @Suite groups classes, not individual tests [OK]
Common Mistakes:
Confusing @Suite with @Test annotation
Thinking @Suite controls test method order
Assuming @Suite skips tests
2. Which of the following is the correct way to declare a JUnit 5 test suite using @Suite and @SelectClasses?
easy
A. @Suite
@SelectClasses({TestA.class, TestB.class})
public class AllTests {}
B. @SuiteClasses({TestA.class, TestB.class})
public class AllTests {}
C. @RunWith(Suite.class)
@SuiteClasses({TestA.class, TestB.class})
public class AllTests {}
D. @TestSuite
@SelectClasses({TestA.class, TestB.class})
public class AllTests {}
Solution
Step 1: Identify JUnit 5 suite syntax
JUnit 5 uses @Suite and @SelectClasses annotations together to define a suite.
Step 2: Check other options for JUnit 4 syntax or invalid annotations
@RunWith and @SuiteClasses are JUnit 4 style; @TestSuite is invalid.
Final Answer:
@Suite
@SelectClasses({TestA.class, TestB.class})
public class AllTests {} -> Option A
Quick Check:
JUnit 5 suite = @Suite + @SelectClasses [OK]
Hint: JUnit 5 suites use @Suite and @SelectClasses together [OK]
Common Mistakes:
Using JUnit 4 annotations in JUnit 5 code
Misspelling @SelectClasses
Omitting the public class declaration
3. Given the following test suite code, what will happen when you run AllTests?
@Suite
@SelectClasses({TestOne.class, TestTwo.class})
public class AllTests {}
medium
A. Only TestTwo's tests will run
B. Both TestOne and TestTwo classes will run all their test methods
C. Only TestOne's tests will run
D. No tests will run because the suite class is empty
Solution
Step 1: Understand @SelectClasses effect
@SelectClasses lists test classes to include in the suite; all their tests run.
Step 2: Confirm suite class content is irrelevant
The suite class itself is a container; it does not need test methods to run included tests.
Final Answer:
Both TestOne and TestTwo classes will run all their test methods -> Option B
Quick Check:
@SelectClasses runs all listed classes [OK]
Hint: Suite runs all classes listed in @SelectClasses [OK]
Common Mistakes:
Thinking suite class must have test methods
Assuming only first class runs
Believing empty suite class means no tests
4. Identify the error in this JUnit 5 test suite declaration:
@Suite
@SelectClasses({TestA.class, TestB.class})
public void AllTests() {}
medium
A. Suite class must extend a base test class
B. Missing @Test annotation on AllTests method
C. @SelectClasses cannot include multiple classes
D. Suite class should not be a method; it must be a class declaration
Solution
Step 1: Check the suite declaration syntax
The suite must be a class, not a method. Here, 'public void AllTests()' is a method.
Step 2: Validate other options
@Test is not needed on suite, @SelectClasses supports multiple classes, and no inheritance is required.
Final Answer:
Suite class should not be a method; it must be a class declaration -> Option D
Quick Check:
Suite must be a class, not a method [OK]
Hint: Suite must be a class, not a method [OK]
Common Mistakes:
Writing suite as a method instead of a class
Adding unnecessary @Test to suite
Thinking suite must extend another class
5. You want to create a test suite that runs LoginTests, PaymentTests, and LogoutTests classes. Which of the following is the best way to write this suite in JUnit 5?
hard
A. @Suite
@SelectPackages({"com.example.tests"})
public class FullSuite {}
B. @SuiteClasses({LoginTests.class, PaymentTests.class, LogoutTests.class})
public class FullSuite {}
C. @Suite
@SelectClasses({LoginTests.class, PaymentTests.class, LogoutTests.class})
public class FullSuite {}
D. @RunWith(Suite.class)
@SelectClasses({LoginTests.class, PaymentTests.class, LogoutTests.class})
public class FullSuite {}
Solution
Step 1: Use correct JUnit 5 annotations
JUnit 5 uses @Suite and @SelectClasses to specify test classes in a suite.
Step 2: Evaluate other options
@SuiteClasses and @RunWith are JUnit 4 style; @SelectPackages is for packages, not specific classes.
Final Answer:
@Suite
@SelectClasses({LoginTests.class, PaymentTests.class, LogoutTests.class})
public class FullSuite {} -> Option C
Quick Check:
JUnit 5 suite = @Suite + @SelectClasses [OK]
Hint: Use @Suite with @SelectClasses listing all test classes [OK]
Common Mistakes:
Mixing JUnit 4 and 5 annotations
Using @SelectPackages when classes are needed
Omitting public class declaration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.