我正在使用这个依赖项:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是容器,通过我的测试启动:
@ClassRule
public static PostgreSQLContainer postgres = new PostgreSQLContainer()
.withDatabaseName("user")
.withUsername("postgres")
.withPassword("postgres");
Run Code Online (Sandbox Code Playgroud)
但是当我尝试建立与数据库的连接时,user我收到异常:
PSQLException: FATAL: database "user" does not exist
Run Code Online (Sandbox Code Playgroud)
在这个容器中仅存在默认数据库"postgres"。
如何创建数据库"user"?
当我尝试使用 MockServer 模拟外部 HTTP API 时,mockserver 返回java.lang.IllegalArgumentException
这是测试代码:
new MockServerClient("localhost", 1080)
.when(request("/messages")
.withMethod("POST")
.withQueryStringParameters(
param("subject", "integration-test-subject")
)
).respond(response().withStatusCode(200));
Run Code Online (Sandbox Code Playgroud)
这是例外:
java.lang.IllegalArgumentException: Exception while parsing
[
{
"httpRequest":{
"method":"POST",
"path":"/messages",
"queryStringParameters":{
"subject":[
"integration-test-subject"
]
}
},
"httpResponse":{
"statusCode":200
},
"times":{
"remainingTimes":0,
"unlimited":true
},
"timeToLive":{
"unlimited":true
}
}
] for Expectation
Run Code Online (Sandbox Code Playgroud)
这是杰克逊的例外:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of FIELD_NAME token
at
[
Source:(String)" {
"httpRequest":{
"method":"POST",
"path":"/messages",
"queryStringParameters":{
"subject":[
"integration-test-subject"
]
}
},
"httpResponse":{
"statusCode":200
},
"times":{
"remainingTimes":0,
"unlimited":true
}, …Run Code Online (Sandbox Code Playgroud) 我有这样的旧式代码:
ObjectToInstantiate instance = new ObjectToInstantiate();
Thread getFirstValueThread = new Thread(() -> instance.setFirstValue(service.getFirstValue));
Thread getSecondValueThread = new Thread(() -> instance.setSecondValue(service.getSecondValue));
getFirstValueThread.start();
getSecondValueThread.start();
try {
getFirstValueThread.join();
getSecondValueThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
return instance;
Run Code Online (Sandbox Code Playgroud)
FirstValue和SecondValue是不同的类,我也可以将它们传递给构造函数,而不仅仅是setter.
我怎样才能使用ExecutorService或其他东西来避免新的线程创建?
我有这样的旧式代码:
if (setContainer.getSet() == null) {
return null;
}
for (SetElement setElement : setContainer.getSet()){
if ("SomeString".equals(SetElement.getCode())) {
return setElement.getValue();
}
}
return null;
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不会检查Set是否为null:
setContainer.getSet()
.stream()
.filter(setValue -> "SomeValue".equals(setElement.getCode()))
.map(SetElement::getValue)
.findAny()
.orElse(null) ;
Run Code Online (Sandbox Code Playgroud)
据我所知,我应该使用,Stream.of()
但我不明白如何执行上面的代码,因为它总是返回Optional<Set<SetElement>>