我正在使用MockMvc进行一些测试,我想验证JSON响应的结构.具体来说,我想确保属性的键存在,并且该值是某个类型或null.
{
"keyToNull": null, # This may be null, or a String
"keyToString": "some value"
}
Run Code Online (Sandbox Code Playgroud)
以下对我有用,但我想知道是否有办法将每组两个期望合并为一行,因为我有很多要检查的属性:
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.hamcrest.Matchers.*;
.andExpect(jsonPath("$").value(hasKey("keyToNull")))
.andExpect(jsonPath("$.keyToNull").value(
anyOf(any(String.class), nullValue(String.class))))
.andExpect(jsonPath("$").value(hasKey("keyToString")))
.andExpect(jsonPath("$.keyToString").value(
anyOf(any(String.class), nullValue(String.class))))
Run Code Online (Sandbox Code Playgroud)
这hasKey()是必要的,因为另一个断言本身通过,因为MockMvc的实现映射中不存在的键映射为null:
.andExpect(jsonPath("$.notAKey").value(
anyOf(any(String.class), nullValue(String.class)))) // ok
Run Code Online (Sandbox Code Playgroud)
jsonPath().exists()也不起作用,因为在内部它比较了价值null.
我考虑过这样一个单独的方法:
private static <T> void assertNullableAttr(ResultActions res, String jsonPath, Class<T> type) throws Exception {
int split = jsonPath.lastIndexOf('.');
String prefix = jsonPath.substring(0, split), key = jsonPath.substring(split+1);
res.andExpect(jsonPath(prefix).value(hasKey(key)))
.andExpect(jsonPath(jsonPath).value(anyOf(any(type), nullValue(type))));
}
Run Code Online (Sandbox Code Playgroud)
但后来它迫使我以不自然的方式分割我的代码:
ResultActions res = mockMvc.perform(get("/api"))
// these …Run Code Online (Sandbox Code Playgroud) 来自Java背景,我仍然对在C++中分配内存感到困惑.我很确定前两个陈述是正确的:
void method() {
Foo foo; // allocates foo on the stack, and the memory is freed
// when the method exits
}
void method2() {
Foo *foo = new Foo(); // allocates foo on the heap
delete foo; // frees the memory used by foo
}
Run Code Online (Sandbox Code Playgroud)
但是这样的事情怎么样?
void method3() {
Foo foo = *new Foo(); // allocates foo on the heap, and then copies it to the stack?
// when the method exits, the stack memory is freed, but …Run Code Online (Sandbox Code Playgroud) 我已经意识到这里已经存在很多"多重定义"的问题了,但是我花了2个多小时寻找解释而没有找到解释.很抱歉,如果这是重复的话.
现在我有2个类:Array.h和Vector.h.既没有任何全局变量,也没有依赖于另一个(即Array不使用Vector而Vector不使用Array).实现在.h文件中.
这是我的Main.cpp:
#include <iostream>
#include "Array.h"
#include "Vector.h"
using namespace std;
int main() {
cout << "Done" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
......一切都运行良好.但是,当我使用#include语句创建另一个.cpp文件时......
DataReader.cpp
#include "Array.h"
#include "Vector.h"
Run Code Online (Sandbox Code Playgroud)
...然后一切都爆炸了,我在Vector中的每个方法,构造函数和运算符重载都得到了一大堆错误:
DataReader.o: In function `Vector':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:49: multiple definition of `Vector::Vector()'
TestMain.o:C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:49: first defined here
DataReader.o: In function `Vector':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:53: multiple definition of `Vector::Vector(int const&, int const&, int const&)'
TestMain.o:C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:53: first defined here
DataReader.o: In function `Vector':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:56: multiple definition of `Vector::Vector(double const&, double const&, double const&)' …Run Code Online (Sandbox Code Playgroud)