Reputation: 1
I've been given test cases that are parameterized and use Stream.of and Arguments.of which I am not familiar with. As evidenced below the test case references the constructor of my class BofAAdapter and passes a BofA object. I'm wondering how to access in my class the integer (500) and the map of entries, or where they are being passed. I can't edit the test cases so I can't simply have them test with a different constructor. I'm just pretty lost here with what this test is doing.
@ParameterizedTest
@MethodSource("providerBankAccountsGetDelinquent")
public void testBankAccountsGetDelinquent(IBankAccounts adapter, int threshold, Map<Integer, Integer> expected) {
Map<Integer, Integer> actual = adapter.getDelinquent( threshold );
assertEqualMaps( expected, actual );
}
static Stream<Arguments> providerBankAccountsGetDelinquent() {
return Stream.of(
Arguments.of( new BofAAdapter( new BofA() ), 500,
Map.ofEntries(
Map.entry( 1, 500),
Map.entry( 5, 20)
)),
Upvotes: 0
Views: 622
Reputation: 103244
Arguments
is a concept from junit 5.
You can read about what Arguments is about and how to use it, and what's happening here over at the junit site linked above.
To give you a quick overview: normally, a test method has no arguments. The test framework (junit5) will see a @Test
annotation and will just run it.
But this is a different beast: This test method has.. parameters! Walk in the shoes of the test framework for a moment: How would you invoke this? What do you pass for 'adapter' and 'treshold'?
That's what the @MethodSource
annotation is about: It's telling junit: FIRST, run the providerBankAccountsGetDelinquent
method, which junit can do, because there are no arguments to pass. Then take the arguments that this method gave you, which is a stream (so, any number of instances of the Arguments
class), and run this test method once for each Arguments object you got.
So, this ends up happening:
Stream<Arguments>
object.testBankAccountsGetDelinquent
with those arguments and print 'succeed' or 'fail' depending on the result of that test (which will depend on whether those maps are equal)In other words, all that code is a convoluted and silly way to just write:
@Test
public void testBankAccountsGetDelinquent() {
IBankAccounts adapter = new BofAAdapter( new BofA() );
int threshold = 500;
Map<Integer, Integer> expected = Map.ofEntries(
Map.entry( 1, 500),
Map.entry( 5, 20)
);
Map<Integer, Integer> actual = adapter.getDelinquent( threshold );
assertEqualMaps(expected, actual);
}
Of course, presumably this code is either an example to show you how @ParameterizedTest
works, or someone has a plan to expand on providerBankAccountsGetDelinquent
.
Note that the general idea is that you use this @ParameterizedTest
mechanism to create dynamic inputs. Imagine, for example, you want to run this test for all thresholds from 400 to 500, with the map changing too. Then this is one way to do that.
Upvotes: 3