Top JUnit/TestNG Interview Questions for SDET

Beknazar
3 min readDec 20, 2023

--

Hi there! I put together the top JUnit/TestNG interview questions for SDET or Test Automation roles.

Please consider taking my Udemy course Interview Preparation for SDET(Java) if you prefer video sessions with more detailed explanations.

What are the JUnit and TestNG?

  • JUnit and TestNG are both testing frameworks for the Java programming language, designed to facilitate the development of automated tests and simplify the testing process.
  • JUnit was primarily created to be used for unit testing in Java.
  • TestNG is a testing framework that extends JUnit and introduces new features.

Why do you need JUnit or TestNG in your framework?

My framework is a Java-based framework with Maven, Selenium, Cucumber, and JUnit tools. The main role of JUnit is to assert tests and provide a run engine.

What is the difference between TestNG and JUnit?

TestNG provides more extensive annotations. For example, it includes annotations for parameterized tests (@Parameters), grouping tests (@Groups), and dependency management (@DependsOnMethods).

  • TestNG natively supports parameterized tests through the @Parameters annotation. JUnit 4 introduced parameterized tests using the @Parameters annotation, allowing you to run the same test with different input values.
  • JUnit does not have built-in support for managing test dependencies. TestNG allows you to specify dependencies between test methods using annotations like @DependsOnMethods.

JUnit does not have built-in support for parallel test execution. Where TestNG provides built-in support for parallel execution of tests.

TestNG allows you to configure tests using XML files, providing additional flexibility for settings like parallel execution, thread counts, and more.

JUnit does not have a built-in concept of data providers. TestNG introduces the concept of data providers, allowing you to supply data to your test methods from external sources.

What is the difference between soft and hard assert?

A hard assert is a traditional assertion mechanism that stops the test execution immediately upon encountering the first assertion failure. But soft assert allows the test execution to continue even if an assertion fails. All assertions are executed, and the results are collected in the end.

What kind of annotations do you know in JUnit? What do they do?

@Test — Marks a method as a test method.

@Before — Marks a method to be run before each test method in the class.

@After — Marks a method to be run after each test method in the class.

@BeforeClass — Marks a method to be run once before any of the test methods in the class.

@AfterClass — Marks a method to be run once after all the test methods in the class have been executed.

@Ignore — Marks a test method or a class to be ignored during test execution.

@RunWith — Specifies a custom test runner for a test class.

Thank you for your attention. Please consider taking my online courses:

--

--

Beknazar
Beknazar

No responses yet