junit 在单元测试中,URI.getAuthority()始终返回空值:80

zd287kbt  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(95)

我正在为一个负载平衡器编写单元测试,目标是能够过滤掉基于URI的某些服务。然而,测试总是失败,因为它根本没有得到正确的URI!下面是测试类:

public class FilteringDiscoveryClientTest {
   private static DiscoveryClient mockClient;

   @BeforeAll
   public static void setUp(){
      SimpleDiscoveryProperties properties = new SimpleDiscoveryProperties();
      Map<String, List<DefaultServiceInstance>> instances = new HashMap<>();

      DefaultServiceInstance testInstance1_1 = new DefaultServiceInstance();
      testInstance1_1.setUri(URI.create("http://my.service.id.1/1"));
      testInstance1_1.setServiceId("my.service.id.1");
      DefaultServiceInstance testInstance1_2 = new DefaultServiceInstance();
      testInstance1_2.setUri(URI.create("http://my.service.id.1/2"));
      testInstance1_2.setServiceId("my.service.id.1");
      DefaultServiceInstance testInstance2_1 = new DefaultServiceInstance();
      testInstance2_1.setUri(URI.create("http://my.service.id.2/1"));
      testInstance2_1.setServiceId("my.service.id.2");
      DefaultServiceInstance testInstance2_2 = new DefaultServiceInstance();
      testInstance2_2.setUri(URI.create("http://my.service.id.2/2"));
      testInstance2_2.setServiceId("my.service.id.2");
      instances.put(
         "my.service.id.1",
         Arrays.asList(testInstance1_1, testInstance1_2)
      );
      instances.put(
         "my.service.id.2",
         Arrays.asList(testInstance2_1, testInstance2_2)
      );

      properties.setInstances(instances);
      SimpleDiscoveryClient client = new SimpleDiscoveryClient(properties);

      mockClient = client;
   }

@Test
   public void testGetServicesFilteringOutSingleService() {
      FilteringDiscoveryClient client = new FilteringDiscoveryClient(
         mockClient,
         i -> !i.getUri().getAuthority().equals("my.service.id.1")
      );

      assertFalse(client.getServices().contains("my.service.id.1"), "list of services not expected to contain service 1");
      assertTrue(client.getServices().contains("my.service.id.2"), "list of services expected to contain service 2");
   }
}

assertFalse失败,因为i.getUri().getAuthority()返回null:80,而不是实际传递的URI。在setup()期间,根据调试器,这一切似乎都设置正确。您知道我的测试哪里出错了吗?我应该在开始时设置不同的URI吗?
编辑:也许我应该添加FilteringDiscoveryClient的外观

public class FilteringDiscoveryClient implements DiscoveryClient {
   private final DiscoveryClient delegate;
   private final Predicate<ServiceInstance> filter;

   public FilteringDiscoveryClient(DiscoveryClient delegate, Predicate<ServiceInstance> filter) {
      Assert.notNull(delegate, "delegate must not be null");
      Assert.notNull(delegate, "filter must not be null");

      this.delegate = delegate;
      this.filter = filter;
   }

   @Override
   public String description() {
      return delegate.description();
   }

   @Override
   public List<ServiceInstance> getInstances(String serviceId) {
      return delegate.getInstances(serviceId).parallelStream().filter(filter).collect(Collectors.toList());
   }

   @Override
   public List<String> getServices() {
      return delegate.getServices().parallelStream().filter(s -> !getInstances(s).isEmpty()).collect(Collectors.toList());
   }
}
9udxz4iz

9udxz4iz1#

也许不是最佳的解决方案,但至少是一个解决方案?结果是,使用.setUri()调用类DefaultInstanceService中被覆盖的URI,这并没有按照我期望的方式设置它。每次创建新的DefaultInstanceService时,它都会根据我所知道的创建一个新的URI,而这似乎并不采用我在setUri()中给予的URI。因此,我创建了如下构造函数:

DefaultServiceInstance testInstance1_1 = new DefaultServiceInstance("http://my.service.id.1/1", "my.service.id.1", "my.service.id.1", -1, false);

      DefaultServiceInstance testInstance1_2 = new DefaultServiceInstance("http://my.service.id.1/2", "my.service.id.1", "my.service.id.1", -1, false);

      DefaultServiceInstance testInstance2_1 = new DefaultServiceInstance("http://my.service.id.2/1", "my.service.id.2", "my.service.id.2", -1, false);

      DefaultServiceInstance testInstance2_2 = new DefaultServiceInstance("http://my.service.id.2/2", "my.service.id.2", "my.service.id.2", -1, false);

我的测试现在可以工作了,但是在尝试获取路径时出现了一个小问题,我希望路径是/2。实际上,根本没有设置路径,因为DefaultServiceInstance中的setUri()从不显式设置路径,所以它总是空字符串。我不能做像instance.getUri().getPath()这样的调用,因为它只会给予一个空字符串。为了得到“/2”,我'我在寻找我必须做instance.toString(),并从那里得到它。总而言之,不是一个伟大的变通方案,但它的进步!如果有人有一个更好的想法在未来我很想知道。

相关问题