小编Mik*_*kov的帖子

如何对 Java 8 流进行单元测试?

list.stream().forEach(e -> { dbCall.delete(e.Id());});

列表中的每一项都会从数据库中删除。

假设列表中有3项,如何进行单元测试:

  1. 删除被调用了 3 次。
  2. 删除被称为“按顺序/顺序”,即列表中元素的顺序?

java testing unit-testing java-stream

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

字段jdbcTemplate需要找不到类型为'org.springframework.jdbc.core.JdbcTemplate'的bean

我制作了一个简单的应用程序以连接到MySQL数据库,但出现此错误:

org.springframework.jdbc.core.JdbcTemplate

在我的配置中(com.kubamadry.dao.MySqlStudentDao)

*****************************
APPLICATION FAILED TO START
*****************************

Description:

Field jdbcTemplate in com.kubamadry.dao.MySqlStudentDao required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:49838', transport: 'socket'

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

项目结构:

图片

班级

主要

package com.kubamadry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String args[]) {
        SpringApplication.run(Main.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

学生

package …
Run Code Online (Sandbox Code Playgroud)

java spring spring-jdbc spring-bean spring-boot

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

Itext Java 11:com.itextpdf.io.source.ByteBufferRandomAccessSource$1 的非法反射访问

最近升级到 Java 11 并开始执行回归检查。当前在尝试调用 时收到非法反射访问错误com.itextpdf.text.pdf.PdfReader.close。目前在 Itext 5.5.13 版上,但也在 itext 7.0.0 上尝试过并且遇到了同样的问题。

有没有人对如何解决 Java-11 和 Itext 之间的兼容性问题有任何建议?

警告:发生非法反射访问操作警告:com.itextpdf.io.source.ByteBufferRandomAccessSource$1 非法反射访问(文件:...repository/com/itextpdf/io/7.0.0/io-7.0.0.jar ) 方法 java.nio.DirectByteBuffer.cleaner() 警告:请考虑将此报告给 com.itextpdf.io.source.ByteBufferRandomAccessSource$1 的维护者 警告:使用 --illegal-access=warn 启用进一步非法反射访问的警告操作警告:在未来的版本中将拒绝所有非法访问操作

java itext itext7 java-11

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

性能:Apache HttpAsyncClient与多线程URLConnection

我正在尝试选择最好的方法来并行进行大量的http请求。以下是我到目前为止拥有的两种方法:

  1. 使用Apache HttpAsyncClient和CompletableFutures:

    try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
    .setMaxConnPerRoute(2000).setMaxConnTotal(2000)
    .setUserAgent("Mozilla/4.0")
    .build()) {
    httpclient.start();
    HttpGet request = new HttpGet("http://bing.com/");
    long start = System.currentTimeMillis();
    CompletableFuture.allOf(
            Stream.generate(()->request).limit(1000).map(req -> {
                CompletableFuture<Void> future = new CompletableFuture<>();
                httpclient.execute(req, new FutureCallback<HttpResponse>() {
                    @Override
                    public void completed(final HttpResponse response) {
                        System.out.println("Completed with: " + response.getStatusLine().getStatusCode())
                        future.complete(null);
                    }
                    ...
                });
                System.out.println("Started request");
                return future;
    }).toArray(CompletableFuture[]::new)).get();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 常规的每请求线程数方法:

    long start1 = System.currentTimeMillis();
    URL url = new URL("http://bing.com/");
    ExecutorService executor = Executors.newCachedThreadPool();
    
    Stream.generate(()->url).limit(1000).forEach(requestUrl ->{
        executor.submit(()->{
            try {
                URLConnection conn = requestUrl.openConnection();
                System.out.println("Completed …
    Run Code Online (Sandbox Code Playgroud)

java performance asynchronous http apache-httpasyncclient

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

从列表中筛选具有相同成员的对象

我有一个对象列表.该对象如下所示:

public class Slots {
  String slotType;
  Visits visit;
}


public class Visits {
  private long visitCode;
  private String agendaCode;
  private String scheduledTime;
  private String resourceType;
  private String resourceDescription;
  private String visitTypeCode;
  ...
}
Run Code Online (Sandbox Code Playgroud)

我需要找到具有相同元素的元素agendaCode,visitTypeCode并且scheduledTime在我的生活中我无法完成它.

我试过这个:

Set<String> agendas = slotsResponse.getContent().stream()
    .map(Slots::getVisit)
    .map(Visits::getAgendaCode)
    .collect(Collectors.toUnmodifiableSet());

Set<String> visitTypeCode = slotsResponse.getContent().stream()
    .map(Slots::getVisit)
    .map(Visits::getVisitTypeCode)
    .collect(Collectors.toUnmodifiableSet());

Set<String> scheduledTime = slotsResponse.getContent().stream()
    .map(Slots::getVisit)
    .map(Visits::getScheduledTime)
    .collect(Collectors.toUnmodifiableSet());

List<Slots> collect = slotsResponse.getContent().stream()
    .filter(c -> agendas.contains(c.getVisit().getAgendaCode()))
    .filter(c -> visitTypeCode.contains(c.getVisit().getVisitTypeCode()))
    .filter(c -> scheduledTime.contains(c.getVisit().getScheduledTime()))
    .collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

但它没有做我想的那样.理想情况下,我会有一个列表列表,其中每个子列表是共享相同的Slots对象的列表agendaCode, …

java functional-programming java-stream vavr

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

为什么从Java模块中打开一个不存在的包?

JLS 11 “ 7.7.2。导出和打开的包”说:

允许opens指定一个与当前模块关联的编译单元未声明的包。

这将是什么情况?为什么需要这个?

java jls java-module

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

使用Java 8流的两个int数组的笛卡尔积

我有两个int数组,a = {10,20,30}{1,2,3}.

我想获得这两个阵列的笛卡尔积.

当我使用ListIntegerS中的逻辑正常工作:

    List<Integer> intList = Arrays.asList(10,20,30);
    List<Integer> intList1 = Arrays.asList(1,2,3);
    intList.stream().flatMap(i -> intList1.stream().map(j -> new int[]{i,j})).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

但是,当我使用时int[],我收到编译错误.

错误:(16,79)java:不兼容类型:lambda表达式中的错误返回类型没有变量类型的实例U存在以便java.util.stream.Stream符合java.util.stream.IntStream

    int[] intArray = {10,20,30};
    int[] intArray1 = {1,2,3};
    Arrays.stream(intArray).flatMap(i -> Arrays.stream(intArray1).mapToObj(j -> new int[]{j,i})).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

请帮我理解这里有什么问题.

PS

Arrays.stream(intArray).mapToObj(i -> new int[]{i,i+1}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

产生出局, {(10,11),(20,21),(30,31)}.

java integer java-stream

4
推荐指数
1
解决办法
558
查看次数

如何在没有上传弹出对话框的情况下使用 selenium 上传多个文件?

我正在使用Selenium webdriverJava.

该工具将通过Jenkins调用在无头服务器上运行。我的问题是处理多个文件上传。Selenium无法打开浏览器,所以当我点击网站上的上传按钮时,也没有出现选择文件弹出窗口

我想知道是否还有其他解决方案可以让我在自动化测试期间处理上传的多文件

PS我可以使用sendkeys功能处理单个文件上传。但我不能这样做来处理多个文件上传。

PPS 我不能使用AutoITRobotclass 来处理弹出对话框(因为无头服务器没有弹出对话框)

java selenium automated-tests

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

Jetty(9.4.12):上下文上下文oejwWebAppContext @ 2af004b webapp /的启动失败,不可用

我正在使用码头(9.4.12)在macOS(Java 11)上提供Web应用程序。

尽管码头已经启动,但该服务仍然不可用。我和往常一样使用了相同的War文件,但是唯一的区别是,我已经将Java从版本10更新到了版本11。这个问题是否有可能的修复?

错误

Failed startup of context o.e.j.w.WebAppContext@2af004b{jaltantra,/jaltantra,file:///private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/,UNAVAILABLE}{/jaltantra.war}
MultiException[java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/optimizer/Pipe.class, 
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/optimizer/Pipe$FlowType.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/EPATool$NodeVariableType.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/quality/QualitySim.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/quality/QualitySim$1.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/EPATool.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Link$LinkType.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Control.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Curve.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Source.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Field$RangeType.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/NUConvert.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Tank.class
java.lang.RuntimeException: Error scanning file /private/var/folders/s8/sgl02rb122xcl3bfc22wtfch0000gn/T/jetty-0.0.0.0-8080-jaltantra.war-_jaltantra-any-4267422187513106243.dir/webapp/WEB-INF/classes/org/addition/epanet/network/structures/Link.class
java.lang.RuntimeException: …
Run Code Online (Sandbox Code Playgroud)

java jetty jetty-9 java-11

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

当corePoolSize = 0时,ScheduledExecutorService消耗100%的CPU

我在制作中遇到了一个有趣的问题.

我有以下ScheduledThreadPool分配代码:

ScheduledExecutorService executorService =
            Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() - 1);
Run Code Online (Sandbox Code Playgroud)

线程池定期处理队列中的一些任务.一切都运行正常,直到服务部署在单个核心环境中.显然,上面的行转换为:

ScheduledExecutorService executorService = Executors.newScheduledThreadPool(0);
Run Code Online (Sandbox Code Playgroud)

从那时起,JVM进程的CPU利用率一直在100%左右.在我改变Runtime.getRuntime().availableProcessors() - 1为不变1的那一刻,问题已经消失.

找出根本原因花了一些时间,但我仍然不知道背后的原因.ScheduledExecutorServiceJavaDoc声明:

/**
 * Creates a thread pool that can schedule commands to run after a
 * given delay, or to execute periodically.
 * @param corePoolSize the number of threads to keep in the pool,
 * even if they are idle
 * @return a newly created scheduled thread pool
 * @throws IllegalArgumentException if {@code corePoolSize …
Run Code Online (Sandbox Code Playgroud)

java performance threadpool threadpoolexecutor

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