Bird
Raised Fist0
JUnittesting~20 mins

Test suites with @Suite in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit Suite Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of JUnit test suite execution
Given the following JUnit 5 test suite setup, what will be the output when running the suite?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({TestA.class, TestB.class})
public class MyTestSuite {}

class TestA {
    @Test
    void testA1() {
        System.out.println("TestA1 executed");
    }
}

class TestB {
    @Test
    void testB1() {
        System.out.println("TestB1 executed");
    }
}
ATestB1 executed\nTestA1 executed
BTestA1 executed\nTestB1 executed
CNo output, tests are not executed by @Suite
DCompilation error due to missing @Test annotations
Attempts:
2 left
💡 Hint
Remember that @Suite runs all tests in the selected classes in the order they are declared.
❓ assertion
intermediate
2:00remaining
Correct assertion to verify test suite execution count
You have a JUnit 5 test suite with two test classes, each containing two tests. Which assertion correctly verifies that the suite ran exactly 4 tests?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.TestExecutionSummary;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;

LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
    .selectors(DiscoverySelectors.selectClass(MyTestSuite.class))
    .build();
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.execute(request, listener);
TestExecutionSummary summary = listener.getSummary();

// Which assertion is correct here?
AassertEquals(4, summary.getTestsFailedCount());
BassertEquals(2, summary.getTestsSucceededCount());
CassertEquals(4, summary.getTestsSucceededCount());
DassertEquals(0, summary.getTestsStartedCount());
Attempts:
2 left
💡 Hint
Count all successful tests run by the suite.
🔧 Debug
advanced
2:00remaining
Identify the cause of suite not running tests
Given this JUnit 5 test suite, why does running MyTestSuite not execute any tests?
JUnit
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
import org.junit.jupiter.api.Test;

@Suite
@SelectClasses({TestX.class})
public class MyTestSuite {}

class TestX {
    void testX1() {
        System.out.println("TestX1 executed");
    }
}
ATestX's test method is missing the @Test annotation.
BThe @SelectClasses annotation must include the suite class itself.
CTestX class must be public for tests to run.
DThe @Suite annotation is missing @RunWith annotation.
Attempts:
2 left
💡 Hint
JUnit 5 requires test methods to be annotated with @Test to be recognized.
❓ framework
advanced
2:00remaining
Understanding @Suite and test discovery in JUnit 5
Which statement about JUnit 5's @Suite and test discovery is TRUE?
A@Suite requires specifying test classes explicitly with @SelectClasses or @SelectPackages.
B@Suite automatically discovers all test classes in the project without configuration.
C@Suite can only run tests annotated with @BeforeEach and @AfterEach.
D@Suite replaces the need for @Test annotations inside test classes.
Attempts:
2 left
💡 Hint
Think about how JUnit 5 knows which tests to run in a suite.
🧠 Conceptual
expert
3:00remaining
Best practice for organizing large test suites with @Suite
You have a large project with hundreds of test classes. What is the best practice to organize test suites using @Suite to keep tests maintainable and efficient?
AUse @Suite only for integration tests and run unit tests separately without suites.
BPut all test classes in one huge suite annotated with @SelectClasses listing every test class.
CAvoid using @Suite and run tests individually to reduce complexity.
DCreate multiple smaller suites grouping related tests by feature or module, then create a master suite to run all.
Attempts:
2 left
💡 Hint
Think about scalability and ease of maintenance.

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

  1. Step 1: Understand the role of @Suite

    @Suite is an annotation used to create a test suite that groups multiple test classes.
  2. 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.
  3. Final Answer:

    To group multiple test classes to run together as one unit -> Option A
  4. 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

  1. Step 1: Identify JUnit 5 suite syntax

    JUnit 5 uses @Suite and @SelectClasses annotations together to define a suite.
  2. Step 2: Check other options for JUnit 4 syntax or invalid annotations

    @RunWith and @SuiteClasses are JUnit 4 style; @TestSuite is invalid.
  3. Final Answer:

    @Suite @SelectClasses({TestA.class, TestB.class}) public class AllTests {} -> Option A
  4. 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

  1. Step 1: Understand @SelectClasses effect

    @SelectClasses lists test classes to include in the suite; all their tests run.
  2. 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.
  3. Final Answer:

    Both TestOne and TestTwo classes will run all their test methods -> Option B
  4. 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

  1. Step 1: Check the suite declaration syntax

    The suite must be a class, not a method. Here, 'public void AllTests()' is a method.
  2. Step 2: Validate other options

    @Test is not needed on suite, @SelectClasses supports multiple classes, and no inheritance is required.
  3. Final Answer:

    Suite class should not be a method; it must be a class declaration -> Option D
  4. 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

  1. Step 1: Use correct JUnit 5 annotations

    JUnit 5 uses @Suite and @SelectClasses to specify test classes in a suite.
  2. Step 2: Evaluate other options

    @SuiteClasses and @RunWith are JUnit 4 style; @SelectPackages is for packages, not specific classes.
  3. Final Answer:

    @Suite @SelectClasses({LoginTests.class, PaymentTests.class, LogoutTests.class}) public class FullSuite {} -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard