You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java参数化测试中Arguments.of()的作用及参数访问疑问

Understanding Parameterized Test Flow with 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:

  1. The @ParameterizedTest annotation tells JUnit this test will run with sets of input data.
  2. @MethodSource("providerBankAccountsGetDelinquent") points JUnit to the method that supplies the test data.
  3. 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: a BofAAdapter instance, the integer 500, 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).
  4. JUnit takes that argument set and injects each value into the corresponding parameter of your test method:
    • IBankAccounts adapter = your new BofAAdapter(new BofA()) instance
    • int threshold = 500
    • Map<Integer, Integer> expected = your Map.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 more Arguments.of(...) calls inside Stream.of to run the test with multiple sets of data, if needed).

内容的提问来源于stack exchange,提问作者jjordan373

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 13:43:14