简体   繁体   English

Mockito ArgumentMatcher 仅匹配不在列表中的值

[英]Mockito ArgumentMatcher to match only value not from the list

For Unit Test, is there a matcher for "value not in the List"?对于单元测试,是否有“不在列表中的值”的匹配器?

JobService.java作业服务.java

@Service
public class JobService{
    public void job(String str1, String str2){
        //some logic to delete files
    }
}

JobServiceTest.java JobServiceTest.java

@SpringBootTest
public class HouseServiceTest{
    @MockBean
    JobService jobService;
    
    List<String> notJob = Arrays.asList("job1", "job2", "job3");

    @Test
    public void test1(){
        doNothing().when(jobService).job(
            ArgumentMatchers.anyString(),
            //how can I execute doNothing when this parameter not match with the notJob list? this 2nd parameter is the filename, so I only want to delete job1,job2, and job3 only. I don't want to delete other files.
        );
    }
}

我不确定你想做什么,但一般来说,当你想要匹配传递的参数不在你可以使用的列表中时:

ArgumentMatchers.argThat(p -> !notJob.contains(p)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM