MOTOSHARE 🚗🏍️

Rent Bikes & Cars Directly from Owners

Motoshare connects vehicle owners with people who need bikes and cars on rent. Owners earn from idle vehicles, and renters get flexible ride options.

Visit Motoshare

Top 50 Junit Interview Questions with Answers

Uncategorized
Junit Interview Questions with Answers

1) What is JUnit?

a) A database management system
b) A testing framework
c) A programming language
d) A web server

Answer: b) A testing framework

2) What is unit testing?

a) Testing the entire application
b) Testing the individual components of the application
c) Testing the user interface of the application
d) Testing the database of the application

Answer: b) Testing the individual components of the application

3) Which of the following is not a JUnit annotation?

a) @Test
b) @Before
c) @After
d) @Run

Answer: d) @Run

4) What is the purpose of @Before annotation?

a) It runs before every test method
b) It runs after every test method
c) It runs before the class is executed
d) It runs after the class is executed

Answer: a) It runs before every test method

5) What is the purpose of @After annotation?

a) It runs before every test method
b) It runs after every test method
c) It runs before the class is executed
d) It runs after the class is executed

Answer: b) It runs after every test method

6) What is the purpose of @Test annotation?

a) It marks a method as a test method
b) It marks a method as a before method
c) It marks a method as an after method
d) It marks a method as a setup method

Answer: a) It marks a method as a test method

7) Which of the following is not a JUnit assertion?

a) assertTrue
b) assertFalse
c) assertNull
d) assertUndefined

Answer: d) assertUndefined

8) What is the purpose of assertEquals assertion?

a) It checks if two objects are equal
b) It checks if two objects are not equal
c) It checks if an object is true
d) It checks if an object is false

Answer: a) It checks if two objects are equal

9) What is the purpose of assertSame assertion?

a) It checks if two objects are equal
b) It checks if two objects are not equal
c) It checks if two objects are referring to the same object
d) It checks if an object is null

Answer: c) It checks if two objects are referring to the same object

10) What is the syntax for using JUnit assertion assertEquals?

a) assertEquals(value1, value2)
b) assert(value1, value2)
c) assertEquals(value2, value1)
d) assertEqual(value1, value2)

Answer: a) assertEquals(value1, value2)

11) What is the purpose of @Ignore annotation?

a) It marks a test method as ignored
b) It marks a test method as failing
c) It marks a test method as a setup method
d) It marks a test method as a teardown method

Answer: a) It marks a test method as ignored

12) What is the purpose of JUnit test suite?

a) To group test cases
b) To run multiple test classes
c) To execute before each test method
d) To execute after each test method

Answer: b) To run multiple test classes

13) What is the syntax for creating a JUnit test suite?

a) @RunWith(Suite.class), @Suite.SuiteClasses({})
b) @RunWith(Suite.class), @SuiteClasses({TestClass1.class, TestClass2.class})
c) @RunWith(JUnit.class), @SuiteClasses({TestClass1.class, TestClass2.class})
d) @RunWith(JUnit.class), @SuiteClasses({})

Answer: b) @RunWith(Suite.class), @SuiteClasses({TestClass1.class, TestClass2.class})

14) What is the purpose of @RunWith annotation?

a) It specifies which runner to use for running the test class
b) It specifies which test suite to use for running the test class
c) It specifies which test method to execute first
d) It specifies which test method to execute last

Answer: a) It specifies which runner to use for running the test class

15) Which of the following is not a JUnit runner?

a) BlockJUnit4ClassRunner
b) Parameterized
c) JUnitCore
d) TestNG

Answer: d) TestNG

16) What is the purpose of @Parameters annotation?

a) It specifies the parameters for a test method
b) It specifies the parameters for a test suite
c) It specifies the parameters for a test case
d) It specifies the parameters for an assertion

Answer: a) It specifies the parameters for a test method

17) What is the purpose of JUnit Rule?

a) To change the behavior of a test method
b) To change the behavior of a test class
c) To change the behavior of the test environment
d) To change the behavior of a test suite

Answer: a) To change the behavior of a test method

18) What is the syntax for using JUnit Rule?

a) @Rule, public RuleName ruleName = new RuleName()
b) @RunWith(Rule.class), public RuleName ruleName = new RuleName()
c) @RunWith(JUnit.class), public RuleName ruleName = new RuleName()
d) @Rule, public void ruleName() {}

Answer: a) @Rule, public RuleName ruleName = new RuleName()

19) What is the purpose of Timeout Rule?

a) To specify a maximum time for a test method to run
b) To specify a maximum time for a test class to run
c) To specify a maximum time for a test suite to run
d) To specify a maximum time for an assertion to run

Answer: a) To specify a maximum time for a test method to run

20) What is the syntax for using Timeout Rule?

a) @Rule, public Timeout timeout = new Timeout(1000)
b) @RunWith(Rule.class), public Timeout timeout = new Timeout(1000)
c) @RunWith(JUnit.class), public Timeout timeout = new Timeout(1000)
d) @Rule, public void timeout() {}

Answer: a) @Rule, public Timeout timeout = new Timeout(1000)

21) What is the purpose of ErrorCollector Rule?

a) To collect and report errors in a test method
b) To collect and report errors in a test class
c) To collect and report errors in a test suite
d) To collect and report errors in an assertion

Answer: a) To collect and report errors in a test method

22) What is the syntax for using ErrorCollector Rule?

a) @Rule, public ErrorCollector errorCollector = new ErrorCollector()
b) @RunWith(Rule.class), public ErrorCollector errorCollector = new ErrorCollector()
c) @RunWith(JUnit.class), public ErrorCollector errorCollector = new ErrorCollector()
d) @Rule, public void errorCollector() {}

Answer: a) @Rule, public ErrorCollector errorCollector = new ErrorCollector()

23) What is the purpose of @ClassRule annotation?

a) It marks a rule as a class rule
b) It marks a method as a class rule
c) It marks a class as a rule
d) It marks a class as a test method

Answer: a) It marks a rule as a class rule

24) What is the purpose of ExternalResource Rule?

a) To manage resources external to the application
b) To manage resources internal to the application
c) To manage resources of a test method
d) To manage resources of a test suite

Answer: a) To manage resources external to the application

25) What is the syntax for using ExternalResource Rule?

a) @ClassRule, public ExternalResource externalResource = new ExternalResource()
b) @RunWith(ExternalResource.class), public ExternalResource externalResource = new ExternalResource()
c) @RunWith(JUnit.class), public ExternalResource externalResource = new ExternalResource()
d) @ClassRule, public void externalResource() {}

Answer: a) @ClassRule, public ExternalResource externalResource = new ExternalResource()

26) What is the purpose of TemporaryFolder Rule?

a) To create a temporary folder for a test method
b) To create a temporary folder for a test class
c) To create a temporary folder for a test suite
d) To create a temporary folder for an assertion

Answer: a) To create a temporary folder for a test method

27) What is the syntax for using TemporaryFolder Rule?

a) @Rule, public TemporaryFolder temporaryFolder = new TemporaryFolder()
b) @RunWith(Rule.class), public TemporaryFolder temporaryFolder = new TemporaryFolder()
c) @RunWith(JUnit.class), public TemporaryFolder temporaryFolder = new TemporaryFolder()
d) @Rule, public void temporaryFolder() {}

Answer: a) @Rule, public TemporaryFolder temporaryFolder = new TemporaryFolder()

28) What is the purpose of Category annotation?

a) To group test cases
b) To group test classes
c) To group test suites
d) To group test methods

Answer: b) To group test classes

29) What is the syntax for using Category annotation?

a) @Category({Category1.class})
b) @RunWith(Category.class), @Category({Category1.class})
c) @RunWith(JUnit.class), @Category({Category1.class})
d) @Category({Category1.class}), public void testMethod() {}

Answer: a) @Category({Category1.class})

30) What is the purpose of JUnit Parameterized Runner?

a) To run a test method with multiple test data inputs
b) To run a test class with multiple test data inputs
c) To run a test suite with multiple test data inputs
d) To run an assertion with multiple test data inputs

Answer: b) To run a test class with multiple test data inputs

31) What is the syntax for using JUnit Parameterized Runner?

a) @RunWith(Parameterized.class), public static Collection[]> data() {}
b) @RunWith(Parameterized.class), public static Object[] data() {}
c) @RunWith(JUnit.class), public static Collection[]> data() {}
d) @RunWith(JUnit.class), public static Object[] data() {}

Answer: a) @RunWith(Parameterized.class), public static Collection[]> data() {}

32) What is the purpose of Parameter annotation?

a) It specifies the parameters for a test method
b) It specifies the parameters for a test class
c) It specifies the parameters for a test case
d) It specifies the parameters for a test suite

Answer: a) It specifies the parameters for a test method

33) What is the purpose of @Parameterized.Parameters annotation?

a) It specifies the parameters for a test method
b) It specifies the parameters for a test class
c) It specifies the parameters for a test case
d) It specifies the parameters for a test suite

Answer: b) It specifies the parameters for a test class

34) What is the syntax for using @Parameterized.Parameters annotation?

a) @Parameterized.Parameters, public static Collection[]> data() {}
b) @RunWith(Parameterized.class), @Parameterized.Parameters, public static Collection[]> data() {}
c) @RunWith(JUnit.class), @Parameterized.Parameters, public static Collection[]> data() {}
d) @Parameterized.Parameters, public void data() {}

Answer: b) @RunWith(Parameterized.class), @Parameterized.Parameters, public static Collection[]> data() {}

35) Which of the following is not a JUnit extension?

a) TestWatcher
b) MethodRule
c) TestListener
d) JUnitExtender

Answer: d) JUnitExtender

36) What is the purpose of JUnit TestWatcher?

a) To watch and report the status of a test method
b) To watch and report the status of a test class
c) To watch and report the status of a test suite
d) To watch and report the status of an assertion

Answer: a) To watch and report the status of a test method

37) What is the syntax for using JUnit TestWatcher?

a) @Rule, public TestWatcher testWatcher = new TestWatcher()
b) @RunWith(TestWatcher.class), public TestWatcher testWatcher = new TestWatcher()
c) @RunWith(JUnit.class), public TestWatcher testWatcher = new TestWatcher()
d) @Rule, public void testWatcher() {}

Answer: a) @Rule, public TestWatcher testWatcher = new TestWatcher()

38) What is the purpose of JUnit MethodRule?

a) To modify the behavior of a test method
b) To modify the behavior of a test class
c) To modify the behavior of a test suite
d) To modify the behavior of an assertion

Answer: a) To modify the behavior of a test method

39) What is the syntax for using JUnit MethodRule?

a) @Rule, public MethodRule methodRule = new MethodRule()
b) @RunWith(MethodRule.class), public MethodRule methodRule = new MethodRule()
c) @RunWith(JUnit.class), public MethodRule methodRule = new MethodRule()
d) @Rule, public void methodRule() {}

Answer: a) @Rule, public MethodRule methodRule = new MethodRule()

40) What is the purpose of JUnit TestListener?

a) To listen for events during the test execution
b) To listen for events before the test execution
c) To listen for events after the test execution
d) To listen for events during the test setup

Answer: a) To listen for events during the test execution

41) What is the syntax for using JUnit TestListener?

a) @RunWith(TestListener.class), public TestListener testListener = new TestListener()
b) @Rule, public TestListener testListener = new TestListener()
c) @RunWith(JUnit.class), public TestListener testListener = new TestListener()
d) @TestListener, public void testListener() {}

Answer: a) @RunWith(TestListener.class), public TestListener testListener = new TestListener()

42) What is the purpose of @BeforeClass annotation?
a) It runs before every test method
b) It runs after every test method
c) It runs before the class is executed
d) It runs after the class is executed

Answer: c) It runs before the class is executed

43) What is the purpose of @AfterClass annotation?

a) It runs before every test method
b) It runs after every test method
c) It runs before the class is executed
d) It runs after the class is executed

Answer: d) It runs after the class is executed

44) Which of the following is not a JUnit testing level?

a) Unit testing
b) Integration testing
c) Acceptance testing
d) Exploratory testing

Answer: d) Exploratory testing

45) What is the purpose of Mockito?

a) To create mock objects for testing
b) To create actual objects for testing
c) To create test data for testing
d) To create test suites for testing

Answer: a) To create mock objects for testing

46) What is the syntax for creating a mock object using Mockito?

a) mock(Object.class)
b) Mockito.createMock(Object.class)
c) Mockito.mock(Object.class)
d) Mockito.create(Object.class)

Answer: c) Mockito.mock(Object.class)

47) What is the syntax for verifying a method call using Mockito?

a) verify(object, times(1)).method()
b) verify(object).method()
c) verify(object, atLeastOnce()).method()
d) verify(object, never()).method()

Answer: a) verify(object, times(1)).method()

48) What is the purpose of JUnit TestSuiteBuilder?

a) To build a test suite dynamically at runtime
b) To build a test suite statically at compile time
c) To build a test suite declaratively at runtime
d) To build a test suite declaratively at compile time

Answer: a) To build a test suite dynamically at runtime

49) What is the syntax for using JUnit TestSuiteBuilder?

a) TestSuiteBuilder builder = new TestSuiteBuilder(), builder.build()
b) TestSuiteBuilder.build(TestClass.class)
c) TestSuiteBuilder.create(TestClass.class)
d) TestSuiteBuilder.run(TestClass.class)

Answer: a) TestSuiteBuilder builder = new TestSuiteBuilder(), builder.build()

50) What is the purpose of @RunWith(SpringJUnit4ClassRunner.class) annotation?

a) To run a test class with SpringJUnit4ClassRunner
b) To run a test class with JUnitCore
c) To run a test class with TestNG
d) To run a test class with MockitoRunner

Answer: a) To run a test class with SpringJUnit4ClassRunner

Ashwani Kumar
Latest posts by Ashwani Kumar (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x