Saturday, July 13, 2024

Java Unit Test MCQ

 Question 1


Which framework is most commonly used for unit testing in Java?


JUnit  

TestNG  

Mockito  

Selenium

Answer:  


JUnit



Question 2

In JUnit 5, which annotation is used to indicate a test method?


@TestCase  

@RunWith  

@Test  

@Before

Answer:  


@Test



Question 3

Which JUnit annotation is used to execute some code before each test method?


@BeforeAll  

@After  

@BeforeEach  

@BeforeTest

Answer:  


@BeforeEach



Question 4

Which method in JUnit is used to check if two objects are equal?


assertSame  

assertTrue  

assertEquals  

assertNotNull

Answer:  


assertEquals



Question 5

Which of the following is a mocking framework often used in Java unit tests?


TestNG  

Mockito  

JUnit  

Cucumber

Answer:  


Mockito



Question 6

In Mockito, which method is used to create a mock object?


mock()  

createMock()  

mockObject()  

newMock()

Answer:  


mock()



Question 7

What does the @Mock annotation do in Mockito?


It creates a real object  

It creates a mock object  

It verifies a method call  

It initializes a mock object

Answer:  


It creates a mock object



Question 8

Which JUnit annotation is used to run a piece of code after all tests in the test class have been run?


@AfterEach  

@AfterAll  

@AfterTest  

@After

Answer:  


@AfterAll



Question 9

In TestNG, which annotation is equivalent to JUnit's @BeforeEach?


@BeforeTest  

@BeforeMethod  

@BeforeClass  

@BeforeSuite

Answer:  


@BeforeMethod



Question 10

Which Mockito method is used to verify that a method was called with specific arguments?


verify()  

check()  

assert()  

confirm()

Answer:  


verify()



Question 11

In JUnit 5, which annotation is used to disable a test method?


@Ignore  

@Disabled  

@Skip  

@Deactivate

Answer:  


@Disabled



Question 12

Which of the following is not a lifecycle method in JUnit 5?


@BeforeEach  

@AfterEach  

@BeforeClass  

@BeforeAll

Answer:  


@BeforeClass



Question 13

Which of the following assertions is used to check if a condition is false in JUnit?


assertTrue()  

assertFalse()  

assertNull()  

assertNotNull()

Answer:  


assertFalse()



Question 14

What is the primary purpose of unit testing?


To test the entire application as a whole  

To test individual units or components in isolation  

To test the user interface  

To test the performance of the application

Answer:  


To test individual units or components in isolation



Question 15

In Mockito, which method is used to return a specific value when a method is called?


when().thenReturn()  

doReturn().when()  

mock().thenReturn()  

verify().thenReturn()

Answer:  


when().thenReturn()



Question 16

Which JUnit annotation is used to provide a timeout for a test method?


@Timeout  

@Test(timeout = 1000)  

@TimeLimit  

@Test(timeout = 1)

Answer:  


@Timeout



Question 17

Which of the following is not a valid JUnit assertion?


assertEquals()  

assertNotNull()  

assertThrows()  

assertEmpty()

Answer:  


assertEmpty()



Question 18

Which JUnit 5 annotation is used to run a test multiple times?


@Repeat  

@RepeatedTest  

@LoopTest  

@TestRepeat

Answer:  


@RepeatedTest



Question 19

In TestNG, which annotation is used to indicate that a method should be executed before any test methods in the current class?


@BeforeTest  

@BeforeClass  

@BeforeMethod  

@BeforeSuite

Answer:  


@BeforeClass



Question 20

In Mockito, how can you mock a method to throw an exception?


when(methodCall).thenThrow(new Exception())  

doThrow(new Exception()).when(methodCall)  

throwException(new Exception()).when(methodCall)  

when(methodCall).throw(new Exception())

Answer:  


when(methodCall).thenThrow(new Exception())

No comments:

Post a Comment

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <...

Followers

Search This Blog

Popular Posts