Questions and coding challenges
In this section, we'll cover 15 questions and coding challenges related to unit testing that are very popular in interviews. Let's begin!
Coding challenge 1 – AAA
Problem: What is AAA in unit testing?
Solution: The AAA acronym stands for [A]rrange, [A]ct, [A]ssert, and it represents an approach to structuring tests to sustain clean code and readability. Today, AAA is a testing pattern that's almost a standard across the industry. The following snippet of code speaks for itself:
@Test public void givenStreamWhenSumThenEquals6() { // Arrange Stream<Integer> theStream = Stream.of(1, 2, 3); // Act int sum = theStream.mapToInt(i -> i).sum(); // Assert assertEquals(6, sum); }
Arrange section: In this section, we prepare or set up the test. For example, in the preceding code, we prepared a stream of integers where the elements are 1, 2, and...