标签: assertj

如何使用assertj提取地图属性

我正在使用AssertJ。我有一个像这样的班MyObj。我有一个ListMyObj

Class MyObj {
    ...
    Map<K,V> myMap;
    ...
}
Run Code Online (Sandbox Code Playgroud)

当我使用时:

  1. assertThat(list).extracting("myMap"),我无法使用.containsKey()方法。
  2. 我也尝试使用assertThat(list).extracting("myMap", Map.class),但是它也不起作用。

什么是正确的使用方式?

java junit assert assertj

5
推荐指数
3
解决办法
5495
查看次数

找不到java.nio.file.Path的类文件

我正在org.assertj:assertj-core:3.6.2测试我的android项目.根据官方讨论,我应该使用带有assertj 3.x的java 8.

这是我的测试类,我正在尝试验证何时执行单击代码可以启动预期的活动.

import android.content.Intent;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class LoginActivityTest {

    @Test
    public void testBtnLogin(){
        LoginActivity loginActivity = Robolectric.setupActivity(LoginActivity.class);

        loginActivity.findViewById(R.id.btnLogin)
                .performClick();

        Intent expectedIntent = new Intent(loginActivity,MainActivity.class);
        ShadowActivity shadowActivity = Shadows.shadowOf(loginActivity);
        Intent actualIntent = shadowActivity.getNextStartedActivity();
        Assertions.assertThat(actualIntent).isEqualTo(expectedIntent);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行测试时,我收到了这个错误:

:app:compileDebugUnitTestJavaWithJavac (Thread[Daemon worker,5,main]) started.
:app:compileDebugUnitTestJavaWithJavac
file or directory '/home/workspace/android/AndroidLib/app/src/testDebug/java', not found
Executing task ':app:compileDebugUnitTestJavaWithJavac' (up-to-date check …
Run Code Online (Sandbox Code Playgroud)

java android android-testing assertj

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

如何比较使用assertJ递归忽略给定字段?

AssertJisEqualToIgnoringGivenFieldsisEqualToComparingFieldByFieldRecursively.

但是,我无法通过忽略某些字段来递归地比较两个对象.根据这个讨论,它必须在开发中.

如何仍然让我的断言的返回值以递归方式进行比较,但忽略了一些字段.是否可以在任何其他库中使用或者我可以以某种方式使用它AssertJ

java unit-testing assertj

5
推荐指数
3
解决办法
4770
查看次数

如何使用assertJ递归地比较列表而忽略给定的字段?

首先,这不是这个问题的重复。在那里,它被专门询问一个对象。我想为一个容器做这个,特别是一个列表。

所以,我知道我可以在使用时忽略一个字段 usingElementComparatorIgnoringFields() 但这不会进行递归比较。

我知道我可以使用usingRecursiveFieldByFieldElementComparator(). 但这不允许我排除给定的字段。

如何递归比较,忽略字段?

java unit-testing assertj

5
推荐指数
2
解决办法
7633
查看次数

AssertJ 比较双精度值

我想将双精度值与 AssertJ 进行比较。我不明白为什么我的测试失败。

@Test
public void testInterestToQuote() {
    double result = BasicCalculator.accumulationFactorByYearsAndInterest(years, interest)
    Assertions.assertThat(result).isCloseTo(expected, Assertions.offset(0.1d))
}
Run Code Online (Sandbox Code Playgroud)

例外是:

java.lang.AssertionError: 
Expecting:
   <7.256571590148141E-5>
to be close to:
   <7.25>
by less than <0.1> but difference was <7.249927434284099>.
(a difference of exactly <0.1> being considered valid)
Run Code Online (Sandbox Code Playgroud)

为什么断言失败?

double compare offset assertj

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

Failing a unit test if an exception is thrown in another thread

Currently, whenever I need to fail a test in response to an exception thrown in another thread, I write something like this:

package com.example;

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.synchronizedList;
import static org.testng.Assert.fail;

public final class T {
  @Test
  public void testFailureFromLambda() throws Throwable {
    final List<Throwable> errors = synchronizedList(new ArrayList<>());

    asList("0", "1", "2").parallelStream().forEach(s -> {
      try {
        /*
         * The actual code under test here.
         */
        throw new Exception("Error " + s);
      } …
Run Code Online (Sandbox Code Playgroud)

junit testng unit-testing hamcrest assertj

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

如何开始:使用 AssertJ Swing 测试 Java Swing GUI

在使用 Swing 开发 Java 桌面应用程序时,我遇到了直接测试 UI 的需要,而不仅仅是通过单元测试测试底层控制器/模型类。

这个答案(关于“基于 Swing 的应用程序的最佳测试工具是什么?”)建议使用FEST,不幸的是,它已停止使用。然而,有一些项目是从 FEST 离开的地方继续进行的。特别是一个(在这个答案中提到)引起了我的注意,因为我之前在单元测试中使用过它:AssertJ

显然有AssertJ Swing,它基于 FEST 并提供了一些编写 Swing UI 测试的易于使用的方法。但是,进行初始/工作设置仍然很麻烦,因为很难说从哪里开始。


如何为以下示例 UI 创建最小的测试设置(仅包含两个类)?

约束:Java SE、Swing UI、Maven 项目、JUnit

public class MainApp {

    /**
     * Run me, to use the app yourself.
     *
     * @param args ignored
     */
    public static void main(String[] args) {
        MainApp.showWindow().setSize(600, 600);
    }

    /**
     * Internal standard method to initialize the view, returning the main JFrame (also to be …
Run Code Online (Sandbox Code Playgroud)

java swing ui-testing assertj

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

AssertJ:对于Pojo,如何检查一个链接语句中的每个嵌套属性/字段

有一个POJO如:

public class ClientWebRequest {

    private URI uri;
    private HttpMethod httpMethod;

    private final Header header;
    private final Body body;


    public ClientWebRequest(){
        header = new Header();
        body = new Body();
    }

    //getters and setters
Run Code Online (Sandbox Code Playgroud)

关于JUnit使用AssertJ以下方法有效,@Test方法通过:

    assertThat(clientWebRequest).isNotNull();

    assertThat(clientWebRequest.getHeader()).isNotNull()
    assertThat(clientWebRequest.getHeader().getAccept()).isNotNull();
    assertThat(clientWebRequest.getHeader().getAcceptLanguage()).isNull();
    assertThat(clientWebRequest.getHeader().getContentType()).isNull();

    assertThat(clientWebRequest.getBody()).isNotNull();
    assertThat(clientWebRequest.getBody().getBody()).isNull();
Run Code Online (Sandbox Code Playgroud)

即使工作,在某种程度上也是冗长的。

我想知道是否可以用一句话重写以上所有内容,检查每个嵌套的属性/字段。因此,我尝试了以下示例:

    assertThat(clientWebRequest.getHeader()).isNotNull()
                                           .hasFieldOrProperty("accept").isNotNull()
                                           .hasFieldOrProperty("acceptLanguage").isNull();
Run Code Online (Sandbox Code Playgroud)

但是失败并显示以下错误消息:

org.junit.ComparisonFailure: expected:<null> 
but was:<Header [accept=[application/json;charset=UTF-8], acceptLanguage=null, contentType=null]>
        at
Run Code Online (Sandbox Code Playgroud)

我的主要目标是工作与周围isNotNullisNull为每个属性/字段POJO

Alpha:由于Joel的建议,现在可以进行以下工作:

assertThat(clientWebRequest).isNotNull()
                            .extracting("header.accept")
                            .doesNotContainNull();

assertThat(clientWebRequest).isNotNull()
        .extracting("header.acceptLanguage", …
Run Code Online (Sandbox Code Playgroud)

junit assertj

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

AssertJ:containsOnly和containsExactlyInAnyOrder之间有什么区别

AbstractIterableAssert#containsOnly说:

验证实际组是否仅按任何顺序包含给定值而不包含任何其他值.

AbstractIterableAssert#containsExactlyInAnyOrder说:

验证实际组是否包含完全给定的值,而不是任何其他顺序.

描述看起来几乎一样,那么唯一确切的实际区别什么?

junit unit-testing assert list assertj

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

JUnit5:如何通过单个assert调用来声明对象的几个属性?

我想用一个assert声明一个对象的几个属性。

使用JUnit 4和Hamcrest,我将编写如下内容:

assertThat(product, allOf(
        hasProperty("name", is("Coat")),
        hasProperty("available", is(true)),
        hasProperty("amount", is(12)),
        hasProperty("price", is(new BigDecimal("88.0")))
));
Run Code Online (Sandbox Code Playgroud)

问:如何使用JUnit 5和AssertJ在一个断言调用中断言几个属性?或者,在JUnit 5 Universe中,这样做的最佳方法是什么。

注意:我当然可以创建具有所有所需属性的对象并执行

assertThat(actualProduct, is(expectedProduct))
Run Code Online (Sandbox Code Playgroud)

但这不是重点。

java junit hamcrest assertj junit5

5
推荐指数
3
解决办法
210
查看次数