小编Jok*_*oka的帖子

为什么MockMvc总是返回空的content()?

我正在尝试使用嘲笑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)

我的问题是:

  1. 为什么MockHttpServletResponse's身体是空的?
  2. 如何正确测试API的响应。

java spring spring-boot

6
推荐指数
2
解决办法
5346
查看次数

Kotlin:tailrec 让暂停的乐趣永无止境

我正在学习如何使用 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)

coroutine kotlin kotlin-coroutines

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

lombok有"PostConstruct"功能吗?

有没有办法可以用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)

lombok

3
推荐指数
2
解决办法
1234
查看次数

访问struct字段时Golang结构文字和指针之间的区别

在访问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)

我搜索了这个但发帖我发现所有解释了值和指针之间的区别,或者"传递值"与"传递指针"到方法.他们不是我想知道的.

struct pointers go

0
推荐指数
1
解决办法
595
查看次数