assertj

Compare maps ignoring given fields

倖福魔咒の 提交于 2021-02-17 06:22:28
问题 I feel like I'm missing something obvious here, but the documentation is letting me down. I'm attempting to compare two maps while ignoring a set of fields in assertJ. I would like to see this assert pass: private static final String[] IGNORED_FIELDS = { "ignored", "another" }; private static final Map<String, Object> TEST_PAYLOAD = ImmutableMap.of("test", "payload", "example", "value", "ignored", "field"); private static final Map<String, Object> COMPARISON_PAYLOAD = ImmutableMap.of("test",

AssertJ `containsExactly` assertion on list with wildcard

给你一囗甜甜゛ 提交于 2020-05-15 10:17:05
问题 I have a getter returning a List with a wildcard: import java.util.List; public interface Foo { List<? extends Bar> getList(); } Where Bar is an other interface. When I write an assertion with AssertJ like this: assertThat(foo.getList()).containsExactly(bar1, bar3); EDIT: my complete usage is to chain a usingElementComparator and to provide a Comparator<Bar> to compare the expected Bar instances. Comparator<Bar> comparator = createBarComparator() assertThat(foo.getList())

聊一聊,单元测试应该测试什么?

情到浓时终转凉″ 提交于 2020-04-16 15:47:28
【推荐阅读】微服务还能火多久?>>> 现在大公司越来越重视项目的单元测试,甚至明确要求项目的单元测试覆盖率不能低于某个值,足可见单元测试的重要性; 试想如果没有单元测试,那么如何保证代码能够正常运行呢? 测试人员做的只是业务上的集成测试,也就是黑盒测试,对单个的方法是没有办法测试的,而且,测试出的 bug 的范围也会很广,根本不能确定 bug 的范围,还得去花时间来确定 bug 出在什么地方。 另外,一个最常见的问题:写单测浪费时间? 你有没有计算过你改bug的时间(定位+修复),算一下的话你会发现时间浪费的会更多。 参考建议 关于如何写好单元测试,下面有几条建议供大家参考: 1. 测试数据外部化 测试数据大致分为两种:变化的和不变化的,对于不变的测试数据,我们完全可以写在单元测试用例代码中,也可以将数据外部化。 而对于测试数据一直在变,并且测试数据量比较大的时候可以使用测试数据外部化将数据放在测试用例的外部进行统一管理。 什么是数据外部化 ?就是将数据放在单元测试用例的外部统一管理,比如我们可以将一个单元测试用例中的测试数据统一放在一个CSV文件中。 我们就可以通过比如junit5中的参数测试注解 @ParameterizedTest 和引入CVS文件的注解 @CsvFileSource 并指定其中的resources属性指定CSV文件,numLinesToSkip = n

Generics and wildcards with collections in Java

故事扮演 提交于 2020-01-13 12:17:38
问题 In a test class using AssertJ, I have code similar to the following: public void someTest() { assertThat(getNames()).has(sameNamesAs(getExpectedNames())); assertThat(getNames()).doesNotHave(sameNamesAs(getOtherNames())); } private List<String> getNames() { return null; } private List<String> getExpectedNames() { return null; } private List<String> getOtherNames() { return null; } private Condition<List<String>> sameNamesAs(List<String> rhs) { return new Condition<List<String>>("same names as

【Spring Boot 单元测试】1. 编写单元测试

≡放荡痞女 提交于 2020-01-06 23:26:35
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 编写单元测试可以帮助开发人员编写高质量的代码,提升代码质量,减少Bug,便于重构。Spring Boot提供了一些实用程序和注解,用来帮助我们测试应用程序,在Spring Boot中开启单元测试只需引入 spring-boot-starter-test 即可,其包含了一些主流的测试库。本文主要介绍基于 Service和Controller的单元测试。 引入 spring-boot-starter-test : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> JUnit,标准的单元测试Java应用程序; Spring Test & Spring Boot Test,对Spring Boot应用程序的单元测试提供支持; Mockito, Java mocking框架,用于模拟任何Spring管理的Bean,比如在单元测试中模拟一个第三方系统Service接口返回的数据,而不会去真正调用第三方系统; AssertJ,一个流畅的assertion库

AssertJ to log assertion's results

巧了我就是萌 提交于 2019-12-24 20:07:00
问题 I'm trying to understand if is possible to configure AssertJ to log the negative result of an assertion to a file without interrupting the routing that is running the comparison. The reason behind this request is that we are comparing the JSON generated by two version of a software to spot differences, but instead of manually creating all the checks I would like to leverage the functionalities already available in AssertJ. As a possible solution to this I was thinking of using a try/catch for

Java几种常用的断言风格你怎么选?

巧了我就是萌 提交于 2019-12-22 23:01:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 日常工作中,不管你是写Unit Test,还是采用TDD的编程方式进行开发,都会遇到断言。而断言的风格常见的会有Assert、BDD风格,对于这些常见的断言风格你怎么选择呢? 01 Assert风格 JUnit中提供了这样的assert断言风格,例如: [@Test](https://my.oschina.net/azibug) void should_be_unlocked_when_insert_coin_given_a_entrance_machine_with_locked_state() { EntranceMachine entranceMachine = new EntranceMachine(EntranceMachineState.LOCKED); String result = entranceMachine.execute(Action.INSERT_COIN); assertEquals("opened", result); assertEquals(EntranceMachineState, entranceMachineState.UNLOCKED); } Hamcrest和AssertJ都提供了assertThat()这样风格的断言,例如: AssertJ提供的assertThat

Is there a way to use AssertJ assertions with Spring MVC Test?

﹥>﹥吖頭↗ 提交于 2019-12-21 03:26:13
问题 I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers. But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC Test. Below is an example using the Hamcrest API. mockMvc .perform(get("/user?operation=userList")) .andExpect(status().isOk()) .andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, userList)) .andExpect(view().name(UserController.VIEW

Exceptions thrown while soft asserting fail the subsequent tests

风格不统一 提交于 2019-12-13 07:39:51
问题 As per title, I'm trying to run a test case in a loop. To be able to calculate the number of failed assertions, I'm expecting that if AssertJ is trying to assert the returned value from a method call, it should softly fail a single iteration and carry on. Otherwise, it defies the purpose of soft assertions. Here's a snippet illustrating this: public static void main(String[] args) { SoftAssertions softAssertions = new SoftAssertions(); softAssertions.assertThat(throwException(10)).isTrue();