Java Questions & Answers – JUnits

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

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

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: @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.
advertisement
advertisement

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

Answer: d
Explanation: 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()
View Answer

Answer: a
Explanation: assert method is used to compare actual and expected results in Junit. It has various implementation like assertEquals, assertArrayEquals, assertFalse, assertNotNull, etc.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

Answer: c
Explanation: == 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)
View Answer

Answer: d
Explanation: @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.
advertisement

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

Answer: c
Explanation: 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
View Answer

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

advertisement
@InjectMocks
MyDictionary dic = new MyDictionary();

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

  1.  <dependency>
  2.     <groupId>junit</groupId>
  3.     <artifactId>junit</artifactId>
  4.     <version>4.8.1</version>
  5.  </dependency>

b)

  1.  <dependency>
  2.     <groupId>org.junit</groupId>
  3.     <artifactId>junit</artifactId>
  4.     <version>4.8.1</version>
  5.  </dependency>

c)

  1.  <dependency>
  2.     <groupId>mock.junit</groupId>
  3.     <artifactId>junit</artifactId>
  4.     <version>4.8.1</version>
  5.  </dependency>

d)

  1.  <dependency>
  2.     <groupId>junits</groupId>
  3.     <artifactId>junit</artifactId>
  4.     <version>4.8.1</version>
  5.  </dependency>
View Answer
Answer: a
Explanation: JUnits can be used using dependency tag in maven in pom.xml. The version as desired and available in repository can be used.
 
 

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.