This set of Java Multiple Choice Questions & Answers (MCQs) on “JUnits”.
1. JUnits are used for which type of testing? Answer: a 2. Which of the below statement about JUnit is false? Answer: d 3. Which of the below is an incorrect annotation with respect to JUnits? Answer: c 4. Which of these is not a mocking framework? Answer: d 5. Which method is used to verify the actual and expected results in Junits? Answer: a 6. What does assertSame() method use for assertion? Answer: c 7. How to let junits know that they need to be run using PowerMock? Answer: d 8. How can we simulate if then behavior in Junits? Answer: c 9. What is used to inject mock fields into the tested object automatically? 10. How can junits be implemented using maven? b) c) d) Answer: a
a) Unit Testing
b) Integration Testing
c) System Testing
d) Blackbox Testing
Clarification: JUnit is a testing framework for unit testing. It uses java as a programming platform. It is managed by junit.org community.
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
Clarification: JUnits test can be run automatically and they check their own results and provide immediate feedback.
a) @Test
b) @BeforeClass
c) @Junit
d) @AfterEach
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.
a) EasyMock
b) Mockito
c) PowerMock
d) MockJava
Clarification: EasyMock, jMock, Mockito, Unitils Mock, PowerMock and JMockit are a various mocking framework.
a) assert()
b) equals()
c) ==
d) isEqual()
Clarification: assert method is used to compare actual and expected results in Junit. It has various implementation like assertEquals, assertArrayEquals, assertFalse, assertNotNull, etc.
a) equals() method
b) isEqual() method
c) ==
d) compare() method
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.
a) @PowerMock
b) @RunWith(PowerMock)
c) @RunWith(Junits)
d) @RunWith(PowerMockRunner.class)
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.
a) if{..} else{..}
b) if(..){..} else{..}
c) Mockito.when(…).thenReturn(…);
d) Mockito.if(..).then(..);
Clarification: Mockito.when(mockList.size()).thenReturn(100); assertEquals(100, mockList.size()); is the usage to implement if and then behavior.
a) @InjectMocks
b) @Inject
c) @InjectMockObject
d) @Mock
Clarification: @InjectMocks annotation is used to inject mock fields into the tested object automatically.
@InjectMocks
MyDictionary dic = new MyDictionary();
a)
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.8.1version>
dependency>
<dependency>
<groupId>org.junitgroupId>
<artifactId>junitartifactId>
<version>4.8.1version>
dependency>
<dependency>
<groupId>mock.junitgroupId>
<artifactId>junitartifactId>
<version>4.8.1version>
dependency>
<dependency>
<groupId>junitsgroupId>
<artifactId>junitartifactId>
<version>4.8.1version>
dependency>
Clarification: JUnits can be used using dependency tag in maven in pom.xml. The version as desired and available in repository can be used.