Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

ExpectedRecords Matcher

Alexander Kolb edited this page Jan 6, 2016 · 1 revision

ExpectedOutput lets you define a collection of expected records, similar to the InputBuilder:

ExpectedOutput<Tuple2<String, Integer>> expectedOutput = ExpectedOutput
			.create(Tuple2.of("test", 1))
			.expect(Tuple2.of("why", 2),times(3))
			.repeatAll(times(2));
  • .create(record) provides you with an instance of ExpectedOutput that has the generic type of the passed record.
  • .expect(record) adds a record to the collection of expected output.
  • .expect(record, int times) expects the record a number of times.
  • .repeatAll(int times) add records up to this point multiple times to the collection.

By default the matcher is just expecting your output to contain these records. It does not care about order, duplicates or other records in the output. You can change this by calling .refine():

expectedOutput.refine().only();
  • .only() allows no other records in the output than those you've added to collection.
  • .noDuplicates() checks if the frequency of the expected records is the same in the actual output. This implies it will only look for duplicates of the records you expected.

You can also enforce order:

expectedOutput.refine().inOrder(strict).all();
  • .inOrder(strict).all() only matches if all specified records are in a strict order, with no other records between them.
  • .inOrder(nonStrict).all() matches if all specified records are in order, allowing for other records in between and duplicates.

Besides .all() you can also enforce order for only a part of the collection:

expectedOutput.refine().inOrder(nonStrict).from(3);
  • .from(3) applies the order statement starting from the 4th record (the index starts at 0) until the last.
  • .to(11) order is enforced from the first to the 12th record.
  • .from(3).to(11) the records from the 4th to the 12th record have to be ordered.
  • .indices(0, 3, 5, 6) the records with the indices 0, 3, 5 and 6 have to occur in that order. Note: You can use the indices out of order i.e.: .indices(0, 5, 3, 6), but it makes the test harder to understand.

It is possible to define multiple order requirements for one ExpectedOutput object:

ExpectedOutput<Integer> expected = ExpectedOutput.create(asList(1,2,3,4));
expected.refine().inOrder(strict).from(1).to(2);
expected.refine().inOrder(nonStrict).indices(0,3);

In this case you have to add the order statements in two steps as the interface is not fluent.