Hamcrest AssetThat equalTo失败,因为对象值数组中有一些随机字符

问题描述 投票:0回答:1

我正在使用Hamcrest在测试中进行断言。下面的代码段可用于其他字符串比较,但是以下语句失败,因为对象值数组中位于第0位的某些随机字符(?),如下图所示。

assertThat("failure, Publication did not match", book.getPublication(), is("Bloomsbury Publishing"));

这里是结果:

java.lang.AssertionError: failure, Publication did not match
Expected: is "Bloomsbury Publishing"
     but: was "‎Bloomsbury Publishing"
Expected :Bloomsbury Publishing
Actual   :‎Bloomsbury Publishing

Value array for object returned

如果有帮助,Book是Product实体的扩展JPA实体,其中Product具有注释@Inheritance(strategy = InheritanceType.JOINED。]

产品类别

private long id;
private String prodName;
private BigDecimal price; 

Book Class

private String genre;
private String author;
private String publication;

在我在data.sql的测试数据中,我具有:

INSERT INTO PRODUCT(ID, PROD_NAME, PRICE) VALUES (1, 'Harry Potter', 200.55);
INSERT INTO PRODUCT(ID, PROD_NAME, PRICE) VALUES (2, 'Chhawa', 450.45);
INSERT INTO PRODUCT(ID, PROD_NAME, PRICE) VALUES (3, 'Chatrapati Shivaji Maharaj', 1000.00);
INSERT INTO PRODUCT(ID, PROD_NAME, PRICE) VALUES (4, 'Asa Mi Asami', 99.99);
INSERT INTO BOOK(ID, GENRE, AUTHOR, PUBLICATION) VALUES (1, 'Contemporary Fantasy', 'J. K. Rollings', '‎Bloomsbury Publishing');
INSERT INTO BOOK(ID, GENRE, AUTHOR, PUBLICATION) VALUES (2, 'Action', 'Shivaji Savant', 'Mehta Publishing House');
INSERT INTO BOOK(ID, GENRE, AUTHOR, PUBLICATION) VALUES (3, 'Action', 'Krishanrao Arjun Kelusakar', 'Saraswati Publishing Co.Pvt.Ltd');
INSERT INTO BOOK(ID, GENRE, AUTHOR, PUBLICATION) VALUES (4, 'Comedy', 'Pu La Deshpande', 'SANSKRUTI BOOK HOUSE');

而且我正在将@GetMapping(path = "/products/{id}")返回的json解编为:

ResponseEntity<Book> response = restTemplate.exchange(
        productBaseUrl,
        HttpMethod.GET,
        null,
        Book.class);

Book book = response.getBody();

神秘地,我仅对于ID = 1才得到这个'\u200E' 8206 Unicode字符

这里是整个代码库的链接:https://bitbucket.org/tyro_02/demo.cart/

string hamcrest assertthat
1个回答
1
投票

8206,第一个字符,是Unicode左至右标记:

8206 Character

您可以使用字符类将字符替换为Java regex支持:

"\\p{C}"

Java Regex matchers

也就是说,如果您认为此测试应该通过。如果您认为在分析之后应该失败,那么结果按原样就失败了。 Book类也可以在其getPublication() getter中返回带剥离Unicode标点的String,如果可以修改此getter,则使用正则表达式replaceAll

另请参见Wikipedia控制字符U+200E(顺便说一下,这是一个控制字符。)

© www.soinside.com 2019 - 2024. All rights reserved.