mmader-kb / fizzbuzz-livecoding

You can write a Fizz Buzz program, can you test it ?

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fizz Buzz - Live Coding

You can write a Fizz Buzz program, can you test it ?

Fizz Buzz

The "Fizz Buzz" is a famous interview question designed to filter out candidates who can't seem to program basic rules (see C2's wiki). It's originally based on a math game in primary school.

Here's the assignment:

  • Write a program that prints one line for each number from 1 to 100. But,
  • for multiples of three print Fizz instead of the number,
  • for multiples of five print Buzz instead of the number,
  • for numbers which are multiples of both three and five print FizzBuzz instead of the number.

There's also a more complex variant called FooBarQix.

Testing Fizz Buzz

Closed-box approach

Closed-box approach, or black-box testing, is a method of software testing that examines the functionality of an application without peering into its internal structures or workings.

Since our "box" is closed, we are only aware that a particular input returns a certain, invariable output. Test cases are built around specifications and requirements, without any knowledge of the "how".

Think of decision table, state transition, use case, user story.

Open-box approach

Open-box approach, or white-box testing, is a method of software testing that tests internal structures or workings of an application, as opposed to its functionality (black box testing). An internal perspective of the system is used to design test cases.

With the knowledge of the "how", test cases can give a specific input to exercise paths through the code and determine the expected outputs. We can test paths at the unit, integration and system levels.

Think of control flow, data flow, branch testing.

Testing tools

Unit testing with JUnit

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage.

  • JUnit Platform is the foundation for launching testing frameworks on the JVM.
  • JUnit Jupiter is the combination of a programming model and an extension model for writing tests and extensions in
  • JUnit 5.
  • JUnit Vintage provides a test engine for legacy tests.

See also TestNG, etc.

Property-Based Testing with jqwik

jqwik enables Property-Based Testing (PBT) to the JVM. Jqwik can work with or without the JUnit engine (Jupiter/Vintage) and with your favorite assertion library.

import net.jqwik.api.*;
import org.assertj.core.api.*;

class PropertyBasedTests {

	@Property
	boolean absoluteValueOfAllNumbersIsPositive(@ForAll int anInteger) {
		return Math.abs(anInteger) >= 0;
	}

	@Property
	void lengthOfConcatenatedStringIsGreaterThanLengthOfEach(
		@ForAll String string1, @ForAll String string2
	) {
		String conc = string1 + string2;
		Assertions.assertThat(conc.length()).isGreaterThan(string1.length());
		Assertions.assertThat(conc.length()).isGreaterThan(string2.length());
	}
}

See also JUnit QuickCheck, Vavr Test, Scala Check, etc.

Test doubles with Mockito

Mockito enables mock creation, verification and stubbing. Mock objects are simulated objects that mimic the behavior of real objects in controlled ways.

Mock example:

import static org.mockito.Mockito.*;

// mock creation
List mockedList = mock(List.class);

// using mock object - it does not throw any "unexpected interaction" exception
mockedList.add("one");
mockedList.clear();

// selective, explicit, highly readable verification
verify(mockedList).add("one");
verify(mockedList).clear();

Stubbing example:

// you can mock concrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);

// stubbing appears before the actual execution
when(mockedList.get(0)).thenReturn("first");

// the following prints "first"
System.out.println(mockedList.get(0));

// the following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));

See also BDDMockito, JMockit, EasyMock, etc.

About

You can write a Fizz Buzz program, can you test it ?

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Java 100.0%