Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为啥出现名为PotentialStubbingProblem异常 #8

Open
bigerro opened this issue Nov 17, 2019 · 2 comments
Open

为啥出现名为PotentialStubbingProblem异常 #8

bigerro opened this issue Nov 17, 2019 · 2 comments

Comments

@bigerro
Copy link
Contributor

bigerro commented Nov 17, 2019

我把老师的MultiThreadServiceDataProcessor类中的代码改成了

public class MultiThreadServiceDataProcessor {
    // 线程数量
    private final int threadNumber;
    // 处理数据的远程服务
    private final RemoteService remoteService;
    // 所有数据处理成功标志位
    private boolean dataHandleSuccess = true;

    public MultiThreadServiceDataProcessor(int threadNumber, RemoteService remoteService) {
        this.threadNumber = threadNumber;
        this.remoteService = remoteService;
    }

    // 将所有数据发送至远程服务处理。若所有数据都处理成功(没有任何异常抛出),返回true;
    // 否则只要有任何异常产生,返回false
    public boolean processAllData(List<Object> allData) {
        int groupSize =
                allData.size() % threadNumber == 0
                        ? allData.size() / threadNumber
                        : allData.size() / threadNumber + 1;
        List<List<Object>> dataGroups = Lists.partition(allData, groupSize);

        try {
            List<Thread> threads = new ArrayList<>();
            for (List<Object> dataGroup : dataGroups) {
                Thread thread = new Thread(() -> dataGroup.forEach(data -> {
                    try {
                        remoteService.processData(data);
                    } catch (Exception e) {
                        dataHandleSuccess  = false;
                        System.out.println(e.getClass().getSimpleName());
                    }
                }));
                thread.start();
                threads.add(thread);
            }

            for (Thread thread : threads) {
                thread.join();
            }
        } catch (Exception e) {
            dataHandleSuccess = false;
        }
        return dataHandleSuccess;
    }
}

但是用老师的测试代码测试时发现除了捕获到IllegalStateException异常还捕获到了下面这个名称的异常这是为什么?

PotentialStubbingProblem

@hcsp-bot
Copy link
Contributor

@bigerro 我用你的代码没能重现出来问题,你能贴一下截图或者异常的栈轨迹,或者直接提一个PR过来么?我看不到这个问题没法回答。

@bigerro
Copy link
Contributor Author

bigerro commented Nov 17, 2019

@hcsp-bot 下面是报的这个异常的详细说明

org.mockito.exceptions.misusing.PotentialStubbingProblem: 
Strict stubbing argument mismatch. Please check:
 - this invocation of 'processData' method:
    remoteService.processData(
    java.lang.Object@5a55884b
);
    -> at com.github.hcsp.MultiThreadServiceDataProcessor.lambda$null$0(MultiThreadServiceDataProcessor.java:34)
 - has following stubbing(s) with different arguments:
    1. remoteService.processData(
    java.lang.Object@52a71cf
);
      -> at com.github.hcsp.MultiThreadServiceDataProcessorTest.failureIfAThreadIsBad(MultiThreadServiceDataProcessorTest.java:30)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
  - stubbing the same method multiple times using 'given().will()' or 'when().then()' API
    Please use 'will().given()' or 'doReturn().when()' API for stubbing.
  - stubbed method is intentionally invoked with different arguments by code under test
    Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
For more information see javadoc for PotentialStubbingProblem class.
	at com.github.hcsp.MultiThreadServiceDataProcessor.lambda$null$0(MultiThreadServiceDataProcessor.java:34)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at com.github.hcsp.MultiThreadServiceDataProcessor.lambda$processAllData$1(MultiThreadServiceDataProcessor.java:32)
	at java.lang.Thread.run(Thread.java:748)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants