Java参数化测试中Arguments.of()的作用及参数访问疑问
Stream.of and Arguments.of Let me break down exactly how this test works, and how your BofAAdapter interacts with the parameters you're curious about:
First, the Test Run Logic
Let's walk through what JUnit does step by step:
- The
@ParameterizedTestannotation tells JUnit this test will run with sets of input data. @MethodSource("providerBankAccountsGetDelinquent")points JUnit to the method that supplies the test data.- The
providerBankAccountsGetDelinquent()method uses:Arguments.of(...): JUnit's utility to package multiple values into a single "argument set" for the test. In your case, it's bundling three values: aBofAAdapterinstance, the integer500, and your expected result map.Stream.of(...): Java's standard stream method to wrap that single argument set into a stream (JUnit requires the data source to return a stream or iterable for parameterized tests).
- JUnit takes that argument set and injects each value into the corresponding parameter of your test method:
IBankAccounts adapter= yournew BofAAdapter(new BofA())instanceint threshold=500Map<Integer, Integer> expected= yourMap.ofEntries(...)instance
How to Access the Parameters in BofAAdapter
Here's the key part you're asking about:
Accessing the 500 threshold
This value is passed directly to your adapter's getDelinquent method when the test runs:
Map<Integer, Integer> actual = adapter.getDelinquent( threshold ); // threshold is 500 here
So inside your BofAAdapter class, the getDelinquent method's parameter will hold this value. For example:
public class BofAAdapter implements IBankAccounts { private BofA bofa; public BofAAdapter(BofA bofa) { this.bofa = bofa; } @Override public Map<Integer, Integer> getDelinquent(int threshold) { // Here, 'threshold' is the 500 from your test case! // Use this value to fetch/filter delinquent accounts from the BofA instance return bofa.getDelinquentAccountsAboveThreshold(threshold); // example logic } }
Accessing the expected result map
This map is not passed to your BofAAdapter at all. It's only used in the test method to verify that the output of getDelinquent matches what's expected. Your adapter's job is to generate the correct actual map based on its own logic (using the BofA instance and the threshold), and the test checks if that matches the pre-defined expected map.
Quick Recap of Stream.of and Arguments.of
Arguments.of(...): Think of it as a wrapper that lets you group multiple test inputs/expected values into a single object that JUnit can inject into your test method.Stream.of(...): Converts that single argument group into a stream (you could add moreArguments.of(...)calls insideStream.ofto run the test with multiple sets of data, if needed).
内容的提问来源于stack exchange,提问作者jjordan373

