<commit_before># Junit Theories by Example

This tutorial walks through some examples of using JUnit theories to effectively test complex code.

The Java 8 `java.time` api is used as the code to be tested. Not so much because it needs testing,
but because calendar handling is a rich source of special cases and glitches. Real tests would more likely be of code that used that api.

The full source code for these examples is [here](../src/test/java/com/github/radm/theories/test/ExampleTest.java)

The examples rely on Java 8 and junit 4.12.

## Basic JUnit test

In JUnit, a test case is just a function annotated with `@Test' and no arguments.

```java

	@Test
	public void twentyTwelveIsLeapYear() {
		assertTrue(Year.of(2012).isLeap());
	}
```

This bundles together the test data and the checking logic, which is of
course the right thing to do if the two depend on each other, as above.

## Cover multiple test cases with a single Theory

One test is rarely enough, and creating multiple similar tests by simple cut and paste
One way of defining multiple related tests cases is to use a
[parameterised test runner](https://github.com/junit-team/junit/wiki/Parameterized-tests).
JUnit [theories](https://github.com/junit-team/junit/wiki/Theories) are an alternative approach that is both more concise and more powerful.

In JUnit, the `Theory` annotation marks a single _test function_ as supporting multiple
_test cases_. The details of the test data are passed as arguments directly to the test.

```java


	@Theory
	public void leapYearsHaveMoreDaysInFebruary(Year year) {
		if (year.isLeap()) {
			assertEquals(Month.FEBRUARY.maxLength(),
					year.atMonth(Month.FEBRUARY).lengthOfMonth());
		} else {
			assertEquals(Month.FEBRUARY.minLength(),
					year.atMonth(Month.FEBRUARY).lengthOfMonth());<commit_msg>another tutorial update<commit_after># Junit Theories by Example

This tutorial walks through some examples of using JUnit theories to effectively test complex code.

The Java 8 `java.time` api is used as the code to be tested. Not so much because it needs testing,
but because calendar handling is a rich source of special cases and glitches. Real tests would more likely be of code that used that api.

The full source code for these examples is [here](../src/test/java/com/github/radm/theories/test/ExampleTest.java)

The examples rely on Java 8 and junit 4.12.

## Basic JUnit test

In JUnit, a test case is just a function annotated with `@Test' and no arguments.

```java

	@Test
	public void twentyTwelveIsLeapYear() {
		assertTrue(Year.of(2012).isLeap());
	}
```

This bundles together the test data and the checking logic, which is of
course the right thing to do if the two depend on each other, as above.

## Cover multiple test cases with a single Theory

One test is rarely enough, and creating multiple similar tests by simple cut and paste is little better.
One way of defining multiple related tests cases is to use a
[parameterised test runner](https://github.com/junit-team/junit/wiki/Parameterized-tests).
JUnit [theories](https://github.com/junit-team/junit/wiki/Theories) are an alternative approach that is both more concise and more powerful.

In JUnit, the `Theory` annotation marks a single _test function_ as supporting multiple
_test cases_. The details of the test data are passed as arguments directly to the test.

```java


	@Theory
	public void leapYearsHaveMoreDaysInFebruary(Year year) {
		if (year.isLeap()) {
			assertEquals(Month.FEBRUARY.maxLength(),
					year.atMonth(Month.FEBRUARY).lengthOfMonth());
		} else {
			assertEquals(Month.FEBRUARY.minLength(),
					year.atMonth(Month.FEBRUARY).lengthOfMonth());
{'fraction_non_alphanumeric': 0.07774140752864157, 'fraction_numerical': 0.004909983633387889, 'mean_word_length': 4.432592592592592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4191086', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1008, 'n_words': 462}