我有以下测试,我需要验证是否正在调用Person类的所有getter.到目前为止,我已经使用了mockito的verify()来确保调用每个getter.有没有办法通过反思来做到这一点?可能是这样的情况,新的getter被添加到Person类,但测试将错过.
public class GetterTest {
class Person{
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
}
@Test
public void testAllGettersCalled() throws IntrospectionException{
Person personMock = mock(Person.class);
personMock.getFirstname();
personMock.getLastname();
for(PropertyDescriptor property : Introspector.getBeanInfo(Person.class).getPropertyDescriptors()) {
verify(personMock, atLeast(1)).getFirstname();
//**How to verify against any getter method and not just getFirstName()???**
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一小部分测试代码示例,我尝试将 Map 转换为 JSON 字符串并返回。从 JSON 字符串解析时,生成的映射包含字符串键“1”而不是整数键“1”,从而使测试失败。用作此映射键的 POJO 也会发生同样的情况。这种行为是预期的还是我省略了 JSON 转换器的一些配置?
public class SampleConverterTest {
@Test
public void testIntegerKey() {
// Register an Integer converter
JSON.registerConvertor(Integer.class, new JSONPojoConvertor(Integer.class));
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "sample");
// Convert to JSON
String msg = JSON.toString(map);
// Retrieve original map from JSON
@SuppressWarnings("unchecked")
Map<Integer, String> obj = (Map<Integer, String>) JSON.parse(msg);
assertTrue(obj.containsKey(1));
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 jetty-util 7.6.10.v20130312
我正在尝试使用maven-antrun-plugin来解压缩文件。如何使用正则表达式定义这个文件?例如,解压缩所有匹配的文件:sample[0-9].zip.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>prepare</id>
<phase>initialize</phase>
<configuration>
<tasks>
<unzip src="${project.build.directory}/{REGEX_GOES_HERE}.zip" dest="${project.build.directory}/dest/" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud) 是否可以获取java.util.function.Function的方法名称.我想每次都记录正在使用的方法的名称.下面的示例打印Lambda对象,但我还没有找到一种简单的方法来获取方法名称:
public class Example {
public static void main(String[] args) {
Example ex = new Example();
ex.callService(Integer::getInteger, "123");
}
private Integer callService(Function<String, Integer> sampleMethod, String input) {
Integer output = sampleMethod.apply(input);
System.out.println("Calling method "+ sampleMethod);
return output;
}
}
Run Code Online (Sandbox Code Playgroud)