小编Vin*_*t F的帖子

让其他组件知道@WebIntegrationTest随机端口

我最近几天一直在玩Spring引导,我非常喜欢@WebIntegrationTest注释,它允许启动Tomcat并自动部署Spring启动应用程序.它提供了在随机端口上启动Tomcat的选项,如果您正在测试webapp,这非常容易.但我的情况略有不同.整个源代码可在此处获得:

https://github.com/vincent-fuchs/spring-projects

使用Spring Batch,我需要解析Excel文件,并将项目写入REST webService.当我事先知道编写者需要使用的URL时,它能很好地工作,并且我能够使用Spring Boot在几秒钟内测试整个流程,方法是加载一个"空心"@RestController,它接收请求并使它们可用对断言的测试.

(注意:我确信这对很多人来说都非常有用.如果你试图测试Spring Integration配置,它也可以工作)

这是我要测试的批处理,而不是webService.但是为了测试这批产品的输出,我需要这个"空心"的web服务.

     @RunWith(SpringJUnit4ClassRunner.class)
     @SpringApplicationConfiguration(classes = 
     {
      //the webservice client that the batch should use (planning for SOAP impl later)
      RestCustomerWsClient.class,
      //the "target" web server, with the 'hollow' endpoint listening
      TargetRESTSystem.class,
      //the application under test and its config
      TestSpecificConfiguration.class,
      CustomerBatchConfiguration.class}
                                )
    @WebIntegrationTest("server.port:8080")
    @IntegrationTest({"spring.batch.job.enabled=false"})
    public class CustomerBatchWithRestTest {

    @Value("${local.server.port}") 
    public int targetWebServerPort;


    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Autowired
    private DummyCustomerController endpoint ;

    @Autowired
    private ConfigurableApplicationContext  appCtx ;

    @Autowired
    private ConfigurableEnvironment env;


    @Before
    public void loadDynamicProperty() { …
Run Code Online (Sandbox Code Playgroud)

spring spring-annotations spring-batch spring-boot

5
推荐指数
0
解决办法
1220
查看次数

使用Mockito模拟方法行为时以任何顺序匹配List

我有一个使用Mockito的测试有一个非常奇怪的行为:它在调试中工作但在正常运行时失败.经过一番调查后,我意识到这是因为我在嘲笑方法行为,传递一系列要匹配的元素.但由于某种原因,列表中的顺序并不总是相同,所以它不匹配,我期望我的模拟返回不返回,因为2个列表不是"等于"

 when(mockStatusCalculatorService.calculateStatus(Arrays.asList(IN_PROGRESS, ABANDONNED,EXPIRED))).thenReturn(ConsolidatedStatus.EXPIRED);
Run Code Online (Sandbox Code Playgroud)

就我而言,要匹配的元素顺序无关紧要.那么在配置我的模拟时如何指定呢?

java unit-testing mockito

5
推荐指数
2
解决办法
5495
查看次数

Kotlin - 从地图列表,到按键分组的地图

我有一个List<Map<Branch,Pair<String, Any>>>我想在单个Map<Branch,List<Pair<String, Any>>>.

因此,如果我有一个只有 2 个元素的初始列表:

列表

1. branch1 -> Pair(key1,value1)

   branch2 -> Pair(key2,value2)


2. branch1 -> Pair(key1a,value1a)
Run Code Online (Sandbox Code Playgroud)

我想结束:

地图

branch1 -> Pair(key1,value1)

           Pair(key1a,value1a)

branch2 -> Pair(key2,value2)
Run Code Online (Sandbox Code Playgroud)

所以一种 gr​​oupBy,使用初始嵌套映射中键的所有值..

我试过

list.groupBy{it-> it.keys.first()} 
Run Code Online (Sandbox Code Playgroud)

但显然它不起作用,因为它只使用第一个键。我想要相同的,但使用所有键作为单独的值。

在 Kotlin 中这样做最惯用的方法是什么?我有一个丑陋的 Java 工作版本,但我很确定 Kotlin 有一个很好的方法。只是到目前为止我还没有找到它!

任何的想法 ?

谢谢

kotlin

5
推荐指数
2
解决办法
1万
查看次数

升级到 Spring Boot 2.6 时 Spring Security 和 org.springdoc.ui.SwaggerConfig 之间的循环引用

将我的应用程序从 Spring Boot 2.5 升级到 2.6 时,出现此错误:

创建名称为“securityConfig”的 bean 时出错:通过方法“setContentNegotationStrategy”参数 0 表达的依赖关系不满足;

嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration”的 bean 时出错:通过方法“setConfigurers”参数 0 表示不满足的依赖关系;

嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:通过字段“swaggerIndexTransformer”表达的依赖关系不满足;

嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?

此错误记录在https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#circular-references-prohibited-by-default上,我知道我可以“修复”通过设置一个属性并返回到 Spring Boot 2.5 行为来实现它。但是如果我可以借此机会修复循环引用,我也可以在将来这样做。

我的securityConfig很简单,因为我的应用程序是一个公共应用程序,向所有调用者返回一些 html 内容,无需身份验证。这是我的配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity security) throws Exception {
    //application is open to everyone - no security
    security.httpBasic().disable();
  }
}
Run Code Online (Sandbox Code Playgroud)

setContentNegotationStrategy错误提到的方法是我WebSecurityConfigurerAdapter没有覆盖的方法,所以我无法理解我需要做什么。

如果我删除我的SecurityConfig课程,那么我仍然有一个错误,与以前相同,但没有提及我的SecurityConfig课程:

创建名称为“org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration”的bean时出错:通过方法“setConfigurers”参数0表示不满足的依赖关系;

嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:通过字段“swaggerIndexTransformer”表达的依赖关系不满足;

嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?

处理 Spring Security 和 org.springdoc.ui.SwaggerConfig 之间的循环引用的推荐方法是什么?

java spring-security spring-boot springdoc

5
推荐指数
1
解决办法
6816
查看次数

在 spring boot starter 中注册自定义日志附加程序

我正在使用 Spring Boot 1.5.4 并且我正在尝试编写一个启动器,其中包含用于使用它的任何应用程序的自定义日志附加程序。这是我注册它的方式:

@Override
synchronized public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    if (!addedCustomAppender && metricProperties.isEnabled()) {
        //Add log appender for Monitoring
        EventsPublisher eventsPublisher = contextRefreshedEvent.getApplicationContext().getBean(EventsPublisher.class);
        final Appender<ILoggingEvent> newAppender = new CustomLogbackAppender(eventsPublisher);
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        final Logger root = context.getLogger("ROOT");
        newAppender.setName("Custom Appender");
        newAppender.setContext(context);
        newAppender.start();
        root.addAppender(newAppender);
        addedCustomAppender = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

这被调用,我可以看到 ROOT 记录器具有附加程序。它实际上在应用程序启动期间工作了几秒钟。

但是很快,在应用程序启动的后期,一个 ApplicationEnvironmentPreparedEvent 被触发,这会触发 LoggingApplicationListener 中的一种重新初始化。

这发生在我在日志中看到后:

INFO 15888 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147482648
Run Code Online (Sandbox Code Playgroud)

在 LogbackLoggingSystem 中,我可以看到对 stopAndReset 方法的调用,该方法调用记录器上的 recursiveReset 方法。之后,Spring …

java logback spring-boot

3
推荐指数
1
解决办法
4378
查看次数

在测试中替换 OAuth2 WebClient

我有一个小型 Spring Boot 2.2 批次,用于写入 OAuth2 REST API。

我已经能够配置以下WebClienthttps://medium.com/@asce4s/oauth2-with-spring-webclient-761d16f89cdd并且它按预期工作。

    @Configuration
    public class MyRemoteServiceClientOauth2Config {

        @Bean("myRemoteService")
        WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
            ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
                    new ServerOAuth2AuthorizedClientExchangeFilterFunction(
                            clientRegistrations,
                            new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
            oauth.setDefaultClientRegistrationId("myRemoteService");

            return WebClient.builder()
                    .filter(oauth)
                    .build();
        }

    }
Run Code Online (Sandbox Code Playgroud)

但是,现在我想为我的批次编写集成测试,并且我想避免使用“真实”授权服务器来获取令牌:如果外部服务器关闭,我不希望我的测试失败。我希望我的测试是“自主的”。

在我的测试过程中,我调用的远程服务被mockserver假的服务所取代。

在这种情况下,最佳做法是什么?

  • 对我有用的是仅在测试之外启用上述配置@Profile("!test")并使用 运行我的测试@ActiveProfiles("test")。我还在测试中导入了测试特定配置:
    @Configuration
    @Profile("test")
    public class BatchTestConfiguration {

        @Bean("myRemoteService")
        public WebClient webClientForTest() {

            return WebClient.create();
        }

    }
Run Code Online (Sandbox Code Playgroud)

但我觉得必须添加@Profile("!test")我的生产配置并不是很好。

  • 有没有一种“更干净”的方法来替换我正在使用的 WebClient bean,通过调用我的假远程服务而无需先尝试获取令牌的方法?我尝试@Primary在我的 webClientForTest bean 上添加一个,但它不起作用:生产 bean 仍然启用,并且出现异常:

没有类型为“org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository”的合格 bean

这是生产bean需要的参数类型 …

spring-boot spring-webclient

3
推荐指数
1
解决办法
3622
查看次数

docker: 来自守护进程的错误响应:获取 https://registry-1.docker.io/v2/: proxyconnect tcp: EOF

在 Windows 10 上使用全新的 Docker 桌面 (2.3.0.5) 安装,我无法提取任何映像。我收到此错误:

docker: Error response from daemon: Get https://registry-1.docker.io/v2/: proxyconnect tcp: EOF 
Run Code Online (Sandbox Code Playgroud)

我支持公司代理,所以我尝试配置它:

  • 使用凭据,遵循 http://domain\username:password@proxy-url.com:port 格式
  • 像上面一样,但使用特殊字符编码:https : //www.url-encode-decode.com/
  • 没有凭据
  • 使用 http/https
  • 没有 http/https

但似乎没有任何效果..

日志中确实没有太多帮助:我还能尝试什么?

docker docker-for-windows

3
推荐指数
1
解决办法
3489
查看次数