250+ TOP MCQs on JUnits and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “JUnits”.

1. JUnits are used for which type of testing?
a) Unit Testing
b) Integration Testing
c) System Testing
d) Blackbox Testing

Answer: a
Clarification: JUnit is a testing framework for unit testing. It uses java as a programming platform. It is managed by junit.org community.

2. Which of the below statement about JUnit is false?
a) It is an open source framework
b) It provides an annotation to identify test methods
c) It provides test runners for running test
d) They cannot be run automatically

Answer: d
Clarification: JUnits test can be run automatically and they check their own results and provide immediate feedback.

3. Which of the below is an incorrect annotation with respect to JUnits?
a) @Test
b) @BeforeClass
c) @Junit
d) @AfterEach

Answer: c
Clarification: @Test is used to annotate method under test, @BeforeEach and @AfterEach are called before and after each method respectively. @BeforeClass and @AfterClass are called only once for each class.

4. Which of these is not a mocking framework?
a) EasyMock
b) Mockito
c) PowerMock
d) MockJava

Answer: d
Clarification: EasyMock, jMock, Mockito, Unitils Mock, PowerMock and JMockit are a various mocking framework.

5. Which method is used to verify the actual and expected results in Junits?
a) assert()
b) equals()
c) ==
d) isEqual()

Answer: a
Clarification: assert method is used to compare actual and expected results in Junit. It has various implementation like assertEquals, assertArrayEquals, assertFalse, assertNotNull, etc.

6. What does assertSame() method use for assertion?
a) equals() method
b) isEqual() method
c) ==
d) compare() method

Answer: c
Clarification: == is used to compare the objects not the content. assertSame() method compares to check if actual and expected are the same objects. It does not compare their content.

7. How to let junits know that they need to be run using PowerMock?
a) @PowerMock
b) @RunWith(PowerMock)
c) @RunWith(Junits)
d) @RunWith(PowerMockRunner.class)

Answer: d
Clarification: @RunWith(PowerMockRunner.class) signifies to use PowerMock JUnit runner. Along with that @PrepareForTest(User.class) is used to declare the class being tested. mockStatic(Resource.class) is used to mock the static methods.

8. How can we simulate if then behavior in Junits?
a) if{..} else{..}
b) if(..){..} else{..}
c) Mockito.when(…).thenReturn(…);
d) Mockito.if(..).then(..);

Answer: c
Clarification: Mockito.when(mockList.size()).thenReturn(100); assertEquals(100, mockList.size()); is the usage to implement if and then behavior.

9. What is used to inject mock fields into the tested object automatically?
a) @InjectMocks
b) @Inject
c) @InjectMockObject
d) @Mock

Answer: a
Clarification: @InjectMocks annotation is used to inject mock fields into the tested object automatically.

@InjectMocks
MyDictionary dic = new MyDictionary();

10. How can junits be implemented using maven?
a)

  1.  <dependency>
  2.     <groupId>junitgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

b)

  1.  <dependency>
  2.     <groupId>org.junitgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

c)

  1.  <dependency>
  2.     <groupId>mock.junitgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

d)

  1.  <dependency>
  2.     <groupId>junitsgroupId>
  3.     <artifactId>junitartifactId>
  4.     <version>4.8.1version>
  5.  dependency>

Answer: a
Clarification: JUnits can be used using dependency tag in maven in pom.xml. The version as desired and available in repository can be used.

 
 

Leave a Reply

Your email address will not be published. Required fields are marked *