我有点困惑.
如何让TestNG报告测试错误?
// ...
@DataProvider(name = "foo")
public Object[][] provideData () {
throw new SomeRuntimeException("Some error occurred. The test configuration "
+ "is somehow incorrect.");
}
Run Code Online (Sandbox Code Playgroud)
这只会导致测试跳过.该异常甚至没有记录.将它移动到构造函数只会记录异常,但这还不够......
我想要一个很大的错误消息.
目前,使用专用(自我)测试方法完成了至少显示一些测试失败的工作......
无论如何,很高兴知道testNG对错误的定义是什么样的.
谢谢你的任何提示!
我想在我的应用程序的方法中添加字段验证(例如,字段不为null和正数)。
private void assertionTest(Integer i) {
// want to first assert here that i is not null and positive
// something like:
// Preconditions.checkArgument( i != null && i > 0, "i should be non null positive number!" );
//
System.out.println( "i: " + i);
}
Run Code Online (Sandbox Code Playgroud)
Java 8引入了Objects类,该类具有一些用于null检查验证的基本实用程序方法:
public static <T> T requireNonNull(T obj)
public static <T> T requireNonNull(T obj, String message)
Run Code Online (Sandbox Code Playgroud)
但是它仍然缺少番石榴的Preconditions类提供的详尽的验证列表,如下所示:
checkArgument(boolean) throws IllegalArgumentException
checkState(boolean) throws IllegalStateException
etc.
Run Code Online (Sandbox Code Playgroud)
Java 8+中是否有任何更好的方法可以进行字段验证,这样我就不必包括guava这样的外部库了?
查看以下输入 JSON:
{
"importantKey": "123xyz",
"nested": {
// more stuff goes here.
}
}
Run Code Online (Sandbox Code Playgroud)
Nested
由具有多种不同实现的接口表示。关键是,为了确定应该使用哪个实现进行反序列化,我需要检查importantKey's
值。
我目前的解决方案是立即反序列化整个事情:
@Override
public Foo deserialize(JsonParser jp, DeserializationContext dc)
throws IOException, JsonProcessingException {
ObjectMapper objectMapper = (ObjectMapper) jp.getCodec();
ObjectNode objectNode = objectMapper.readTree(jp);
String importantKey = objectNode.findValue("importantKey").asText();
JsonNode nestedNode = objectNode.findValue("nested");
NestedInterface nested;
nested = objectMapper.treeToValue(nestedNode, findNestedImplFor(importantKey));
// construct containing Foo and so on ...
}
Run Code Online (Sandbox Code Playgroud)
这是可行的,但由于几个不同的原因(特定于我的用例),如果有一个 Deserializer 只是为了nested
以某种方式可以读取或访问外部对象的字段,那将会更酷。
我在 dropwizard 上下文中运行 jackson 2.8.x。
有人有想法吗?提前致谢!
我正在使用一个支持es6模块的javascript构建环境(使用es6-module-transpiler),因此您可以轻松地跨不同文件导入内容。
现在,我有了一个第三方库,我希望它是“可导入的”。
该库按以下方式填充其功能:
(function () {/*...*/}).call(this);
Run Code Online (Sandbox Code Playgroud)
省略闭包并将其转换为:
export default function () {/* ... */};
Run Code Online (Sandbox Code Playgroud)
或者,还有更好的方法?
提前致谢!
我想在子资源中注入一个商务服务bean,该子资源在专用类中定义并由子资源定位器传递.
一些示例代码:
根资源
@RequestScoped
@Path("service")
public class MyResource {
@Context
ResourceContext resourceContext;
// Sub resource locator
@Path("subservice")
public MySubResource locateToSubResource () {
// I don't want to create it myself.
return resourceContext.getResource(MySubResource.class);
}
}
Run Code Online (Sandbox Code Playgroud)相应的子资源
@RequestScoped
public class MySubResource {
// Note that businessBean itself consists of
// multiple ejbs that also need to be injected so that it can do its job!
@Inject
private BusinessBean businessBean;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get () {
return businessBean.doStuff();
}
}
Run Code Online (Sandbox Code Playgroud)Jersey不会让CDI调用依赖项...请注意,资源是托管对象.否则甚至不可能在根资源中注入一个bean(这里我推动我的其他问题'查看计数以获得更多意见;-) …
我有一个有趣的用例,其中注释的类中使用的字段名称@ConfigurationProperties
应该与(yaml)配置文件中使用的相应键不同:
@ConfigurationProperties("foo")
class ConfProps {
private List<SomePojo> bar = new ArrayList<>();
// getter, setter
}
Run Code Online (Sandbox Code Playgroud)
这将"寻找" foo.bar
.是否可以将字段映射bar
到不同的属性键?
我阅读了文档和一些相关文章,但没有...
对我而言似乎要么是因为它绝对是微不足道的,要么就是某种非目标.
提前致谢!
问题很短:
让我们考虑有以下类:
public class someClass implements someInterface {
@NotNull
String someString;
// ... some cool logic, getters, setters, etc.
}
Run Code Online (Sandbox Code Playgroud)
还有这个界面:
public interface SomeInterface {
// cool stuff
}
Run Code Online (Sandbox Code Playgroud)
其他地方有这个方法:
public <T extends SomeInterface> T doStuff (T someInterface) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<T>> violatons = validator.validate();
// ...
// ...
// ...
return someInterface;
}
Run Code Online (Sandbox Code Playgroud)
我对 BV 没有太多经验。SomeClass 中的 someString 是否会被验证,或者我是否需要显式地将 someInterface 转换为 SomeClass?
这里的场景被简化了。实际上,我有不同的通用类和接口,这意味着需要做一些工作。这就是为什么我要仔细询问。
希望有一位专家在附近!
提前致谢!
java ×5
validation ×2
dropwizard ×1
ecmascript-6 ×1
ejb ×1
generics ×1
jackson ×1
java-8 ×1
javascript ×1
jax-rs ×1
jersey-2.0 ×1
json ×1
spring-boot ×1
testing ×1
testng ×1
unit-testing ×1