在停止开发之后我使用FEST-Assert并移动了AssertJ.
最近我被指向谷歌存储库与另一个断言库Truth(http://google.github.io/truth/).
阅读这些例子,我找不到任何开始使用它的优点AssertJ.所以这只是味道的问题.但也许我错过了这一点,是吗?
我正在使用AssertJ,我试图断言两个List<String>包含相同的字符串,忽略顺序.
List<String> expected = Arrays.asList("Something-6144-77.pdf", "d-6144-77.pdf", "something-6144-78.pdf", "Something-6144-8068.pdf");
List<String> actual = new ArrayList<String>();
assertThat(actual.size()).isEqualTo(expected.size());
// This line gives the error: "The method containsExactlyInAnyOrder(String...) in the type ListAssert<String> is not applicable for the arguments (List<String>)"
assertThat(actual).containsExactlyInAnyOrder(expected);
Run Code Online (Sandbox Code Playgroud)
如何修复下面尝试使用时出现的编译错误containsExactlyInAnyOrder()?
"ListAssert类型中的方法containsExactlyInAnyOrder(String ...)不适用于参数(List)"
我已经定义了一个静态assertThat方法来扩展AssertJ.此方法接受类型的lambda表达式:
@FunctionalInterface
public interface Action {
void execute() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
签名如下所示:
public static ExceptionAssert assertThat(Action action)
Run Code Online (Sandbox Code Playgroud)
我想将此方法与静态导入一起使用.但它含糊不清.编译器不知道是否应该使用assertThat(Iterable)或我的方法.我不明白void方法如何与返回的方法冲突Iterator<T>.
知道如何解决这个冲突(没有在前面写过类名assertThat)?
我正在将一些测试从Hamcrest转换为AssertJ.在Hamcrest中,我使用以下代码段:
assertThat(list, either(contains(Tags.SWEETS, Tags.HIGH))
.or(contains(Tags.SOUPS, Tags.RED)));
Run Code Online (Sandbox Code Playgroud)
也就是说,列表可以是那个或那个.我如何在AssertJ中表达这一点?该anyOf功能(当然,任何别的东西比任何,但是这将是第二个问题)需要Condition; 我已经实现了这一点,但感觉好像这应该是一个常见的情况.
这是我的一部分build.gradle有冲突:
...
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
}
...
testCompile( 'com.squareup.assertj:assertj-android:1.0.0' )
...
Run Code Online (Sandbox Code Playgroud)
我在日志中看到的问题:
WARNING: Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (21.0.3) and test app (20.0.0) differ.
Run Code Online (Sandbox Code Playgroud)
显然它从类路径中删除了冲突的依赖.我不确定它是插件gradle还是android gradle插件.
我接下来试过了:
testCompile( 'com.squareup.assertj:assertj-android:1.0.0' ) {
exclude group: 'com.android.support', module: 'support-annotations'
}
Run Code Online (Sandbox Code Playgroud)
但我仍然有编译错误,因此排除了依赖.
我接下来试过了:
configurations.all {
resolutionStrategy {
// fail eagerly on version conflict (includes transitive dependencies)
// e.g. multiple different versions of the same dependency (group and name are equal)
failOnVersionConflict()
// force certain …Run Code Online (Sandbox Code Playgroud) android gradle android-gradle-plugin assertj android-assertj
我有一个有效的 hamcrest 断言:
assertThat(mylist, contains(
containsString("15"),
containsString("217")));
Run Code Online (Sandbox Code Playgroud)
预期的行为是:
mylist == asList("Abcd15", "217aB") => 成功myList == asList("Abcd15", "218") => 失败如何将此表达式迁移到 assertJ。当然,存在一些幼稚的解决方案,例如断言第一个和第二个值,如下所示:
assertThat(mylist.get(0)).contains("15");
assertThat(mylist.get(1)).contains("217");
Run Code Online (Sandbox Code Playgroud)
但这些是对列表元素的断言,而不是对列表的断言。在列表上尝试断言将我限制为非常通用的函数。所以也许它只能通过自定义断言来解决,如下所示就可以了:
assertThat(mylist).elements()
.next().contains("15")
.next().contains("217")
Run Code Online (Sandbox Code Playgroud)
但是在我编写自定义断言之前,我会对其他人如何解决这个问题感兴趣?
编辑:一个额外的非功能性要求是,测试应该可以通过额外的约束轻松扩展。在 Hamcrest 中很容易表达额外的约束,例如
assertThat(mylist, contains(
emptyString(), //additional element
allOf(containsString("08"), containsString("15")), //extended constraint
containsString("217"))); // unchanged
Run Code Online (Sandbox Code Playgroud)
对于此示例,依赖于列表索引的测试必须重新编号,使用自定义条件的测试必须重写完整条件(请注意,中的约束allOf不限于子字符串检查)。
我正在尝试编写一个断言函数来检查给定的对象是否属于某个类型T:
@UseExperimental(ExperimentalContracts::class)
inline fun <reified T> assertIsInstance(value: Any?) {
contract {
returns() implies (value is T)
}
Assertions.assertThat(value).isInstanceOf(T::class.java)
}
Run Code Online (Sandbox Code Playgroud)
该函数使用 AssertJ 来做具体的断言,但我愿意让编译器知道在它执行后,valueis 的类型T是可以进行智能广播的。这似乎不起作用,因为:
Error in contract description: references to type parameters are forbidden in contracts
有没有另一种方法来实现这种行为?这里有什么问题?这最终会成为可能吗?
(使用 Kotlin v1.3)
generics kotlin assertj kotlin-reified-type-parameters kotlin-contracts
鉴于以下课程...
public class UserAccount {
private Long id;
private String email;
private String activationCode;
private Date createDate;
}
Run Code Online (Sandbox Code Playgroud)
...我需要使用 AssertJ 将实际对象与预期对象进行比较。然而, fieldsid和具有动态值,我无法将activationCode其createDate硬编码到断言中。
所以下面的断言将会失败:
assertThat(actualUserAccount).isEqualTo(expectedUserAccount);
然后我发现以下内容会忽略某些字段:
assertThat(actualUserAccount)
.usingRecursiveComparison()
.ignoringFields("id", "activationCode", "createDate")
.isEqualTo(expectedUserAccount);
Run Code Online (Sandbox Code Playgroud)
但我实际上正在寻找的是通过对某些字段进行以下特殊检查来断言对象是否相等:
id任何Long价值?activationCode任何String价值?createDate任何Date价值?或者除了为每个字段编写一个断言之外没有其他方法吗?
我有一个禁用的JTable,它提供了一个弹出菜单:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
public class DisabledTableFrame extends JFrame {
public DisabledTableFrame() {
setSize(200, 100);
setTitle(getClass().getCanonicalName());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTable table = new JTable();
add(table);
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
mouseReleased(e);
}
@Override
public void mouseReleased(MouseEvent e) {
new PopupMenu();
}
});
table.setEnabled(false);
setVisible(true);
}
public static void main(String[] args) {
new DisabledTableFrame();
}
private class PopupMenu extends JPopupMenu {
public PopupMenu() {
JMenuItem menuItem = new JMenuItem("TEST");
add(menuItem); …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 assertJ 上设置乘法条件,但在 examplesGit 中找不到。
我目前写:
assertThat(A.getPhone())
.isEqualTo(B.getPhone());
assertThat(A.getServiceBundle().getId())
.isEqualTo(B.getServiceBundle().getId());
Run Code Online (Sandbox Code Playgroud)
但想要有类似的东西:
assertThat(A.getPhone())
.isEqualTo(B.getPhone())
.And
(A.getServiceBundle().getId())
.isEqualTo(B.getServiceBundle().getId());
Run Code Online (Sandbox Code Playgroud)
好像我使用链接这行不通,因为我需要差异数据(id 而不是电话)。有没有可能将它全部混合到一个单一的assertJ命令中?看起来似乎没有任何可能性(算法明智),但也许还有一些其他想法可以在语句上使用 && ?
谢谢
assertj ×10
java ×5
junit ×2
android ×1
fest-assert ×1
generics ×1
gradle ×1
hamcrest ×1
java-8 ×1
kotlin ×1
kotlin-reified-type-parameters ×1
lambda ×1
swing ×1
unit-testing ×1