@SpringBootTest
public class IpAddressServiceImplIntegrationTest {
@MockBean
private IpCheckService ipCheckService;
@Autowired
private IpAddressService ipAddressService;
@BeforeEach
void setUp() {
// 如果有需要初始化的数据或状态,可以在这里设置
}
@Test
void testAllIpAddressCheckWithValidIps() throws InterruptedException {
// 准备测试数据
AllIpAddressCheckRequest request = new AllIpAddressCheckRequest();
request.setIpAddressList(Arrays.asList("192.168.1.1", "10.0.0.1"));
// 模拟异步 IP 检查服务的行为
doAnswer(invocation -> {
String ipAddress = invocation.getArgument(0);
Map<String, Boolean> map = invocation.getArgument(1);
CountDownLatch latch = invocation.getArgument(2);
map.put(ipAddress, true);
latch.countDown();
return null;
}).when(ipCheckService).asyncIpCheck(anyString(), anyMap(), any(CountDownLatch.class));
// 执行测试
AllIpAddressCheckResponse response = ipAddressService.allIpAddressCheck(request);
// 验证结果
assertTrue(response.getResult());
assertEquals(2, response.getMap().size());
assertTrue(response.getMap().get("192.168.1.1"));
assertTrue(response.getMap().get("10.0.0.1"));
}
标签:assertTrue,springboot,示例,单元测试,request,getMap,getArgument,invocation,response
From: https://www.cnblogs.com/lmzzr24/p/18562054