我正在尝试使用嘲笑Mvc测试我的其余api。
mockMvc.perform(get("/users/1/mobile")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andExpect(content().string("iPhone"))
Run Code Online (Sandbox Code Playgroud)
测试失败的原因是:
java.lang.AssertionError: Response content
Expected :iPhone
Actual :
Run Code Online (Sandbox Code Playgroud)
从的输出中print(),我可以知道API实际上返回了预期的字符串“ iPhone”。
ModelAndView:
View name = users/1/mobile
View = null
Attribute = treeNode
value = "iPhone"
errors = []
Run Code Online (Sandbox Code Playgroud)
而且我猜上面的空“ Actual”是由下面的空“ Body”引起的
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = users/1/mobile
Redirected URL = null
Cookies = []
Run Code Online (Sandbox Code Playgroud)
我的问题是:
MockHttpServletResponse's身体是空的?我正在学习如何使用 CompletableFuture 使协程与 Java 库一起工作。下面是我的代码:
// x invokes y invokes z invokes Java client
suspend fun x(i: Int, client: FakeJavaClient): Int {
fun z(k: Int): Int {
println("z: $k")
return client.query(k).get()
}
tailrec // with 'tailrec', the code never terminates
suspend fun y(j: Int): Int {
val ret = z(j)
if (ret > 10) {
return ret
}
return y(j + 1)
}
return y(i)
}
fun main() {
runBlocking {
launch(Dispatchers.IO) {
FakeJavaClient().use { x(0, it) }
}
} …Run Code Online (Sandbox Code Playgroud) 有没有办法可以用lombok定义"PostConstruct"初始化方法?
@RequiredArgsConstructor(staticName = "of")
class MyObj {
private final int x;
private final int y;
private int z;
// not work
@PostConstruct
private void someInitLogic {
z = x + y;
}
public void start() {
// code use "z"
}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以初始化一个对象:
MyObj obj = MyObj.of(1, 2);
obj.start();
Run Code Online (Sandbox Code Playgroud) 在访问struct字段时,我不理解struct literal和struct指针之间的区别.有没有不同的内部行为?
type Person struct {
Name string
}
p := &Person{Name: "Alice"}
u := Person{Name: "Bob"}
fmt.Println(p.Name) // any difference ?
fmt.Println(u.Name) // any difference ?
Run Code Online (Sandbox Code Playgroud)
我搜索了这个但发帖我发现所有解释了值和指针之间的区别,或者"传递值"与"传递指针"到方法.他们不是我想知道的.