Assert same condition on all elements of a collection
I'm working with AssertJ and I need to check that all objects in a list have intField > 0. Something like this:
assertThat(myObjectList).extracting(p -> p.getIntField()).isGreaterThan(0);
What's the correct way to achieve this? Should I use some other library?
java unit-testing assertj
add a comment |
I'm working with AssertJ and I need to check that all objects in a list have intField > 0. Something like this:
assertThat(myObjectList).extracting(p -> p.getIntField()).isGreaterThan(0);
What's the correct way to achieve this? Should I use some other library?
java unit-testing assertj
add a comment |
I'm working with AssertJ and I need to check that all objects in a list have intField > 0. Something like this:
assertThat(myObjectList).extracting(p -> p.getIntField()).isGreaterThan(0);
What's the correct way to achieve this? Should I use some other library?
java unit-testing assertj
I'm working with AssertJ and I need to check that all objects in a list have intField > 0. Something like this:
assertThat(myObjectList).extracting(p -> p.getIntField()).isGreaterThan(0);
What's the correct way to achieve this? Should I use some other library?
java unit-testing assertj
java unit-testing assertj
asked Nov 28 '18 at 18:48
daviooohdavioooh
10.7k2698200
10.7k2698200
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Seems the library does not provide an option to assert the way you described (even AbstractIntArrayAssert does not allow to do so), but you can:
1) use AbstractIterableAssert#allMatch(Predicate)
assertThat(asList(0, 2, 3))
.allMatch(i -> i > 0);
2) If you really want to use something like isGreaterThan on ListAssert, you can create a custom ListAssert:
public class CustomListAssert<E> extends ListAssert<E> {
public static <E> CustomListAssert<E> assertThat(List<E> list) {
return new CustomListAssert<>(list);
}
public CustomListAssert(List<? extends E> actual) {
super(actual);
}
public IntListAssert extractingInt(ToIntFunction<E> mapper) {
return new IntListAssert(actual.stream()
.mapToInt(mapper).boxed().collect(toList()));
}
}
public class IntListAssert extends ListAssert<Integer> {
public IntListAssert(List<Integer> actual) {
super(actual);
}
public IntListAssert allGreaterThan(int i) {
allMatch(n -> n > i);
return this;
}
}
And use it like this:
@Test
public void test() {
CustomListAssert.assertThat(asList(0, 2, 3))
.extractingInt(i -> i * 2)
.allGreaterThan(0);
}
What in my case yields:
java.lang.AssertionError:
Expecting all elements of:
<[0, 4, 6]>
to match given predicate but this element did not:
<0>
I didn't knowallMatchmethod. That's exacly what I was looking for. Thank you very much!
– davioooh
Nov 29 '18 at 15:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53526156%2fassert-same-condition-on-all-elements-of-a-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Seems the library does not provide an option to assert the way you described (even AbstractIntArrayAssert does not allow to do so), but you can:
1) use AbstractIterableAssert#allMatch(Predicate)
assertThat(asList(0, 2, 3))
.allMatch(i -> i > 0);
2) If you really want to use something like isGreaterThan on ListAssert, you can create a custom ListAssert:
public class CustomListAssert<E> extends ListAssert<E> {
public static <E> CustomListAssert<E> assertThat(List<E> list) {
return new CustomListAssert<>(list);
}
public CustomListAssert(List<? extends E> actual) {
super(actual);
}
public IntListAssert extractingInt(ToIntFunction<E> mapper) {
return new IntListAssert(actual.stream()
.mapToInt(mapper).boxed().collect(toList()));
}
}
public class IntListAssert extends ListAssert<Integer> {
public IntListAssert(List<Integer> actual) {
super(actual);
}
public IntListAssert allGreaterThan(int i) {
allMatch(n -> n > i);
return this;
}
}
And use it like this:
@Test
public void test() {
CustomListAssert.assertThat(asList(0, 2, 3))
.extractingInt(i -> i * 2)
.allGreaterThan(0);
}
What in my case yields:
java.lang.AssertionError:
Expecting all elements of:
<[0, 4, 6]>
to match given predicate but this element did not:
<0>
I didn't knowallMatchmethod. That's exacly what I was looking for. Thank you very much!
– davioooh
Nov 29 '18 at 15:47
add a comment |
Seems the library does not provide an option to assert the way you described (even AbstractIntArrayAssert does not allow to do so), but you can:
1) use AbstractIterableAssert#allMatch(Predicate)
assertThat(asList(0, 2, 3))
.allMatch(i -> i > 0);
2) If you really want to use something like isGreaterThan on ListAssert, you can create a custom ListAssert:
public class CustomListAssert<E> extends ListAssert<E> {
public static <E> CustomListAssert<E> assertThat(List<E> list) {
return new CustomListAssert<>(list);
}
public CustomListAssert(List<? extends E> actual) {
super(actual);
}
public IntListAssert extractingInt(ToIntFunction<E> mapper) {
return new IntListAssert(actual.stream()
.mapToInt(mapper).boxed().collect(toList()));
}
}
public class IntListAssert extends ListAssert<Integer> {
public IntListAssert(List<Integer> actual) {
super(actual);
}
public IntListAssert allGreaterThan(int i) {
allMatch(n -> n > i);
return this;
}
}
And use it like this:
@Test
public void test() {
CustomListAssert.assertThat(asList(0, 2, 3))
.extractingInt(i -> i * 2)
.allGreaterThan(0);
}
What in my case yields:
java.lang.AssertionError:
Expecting all elements of:
<[0, 4, 6]>
to match given predicate but this element did not:
<0>
I didn't knowallMatchmethod. That's exacly what I was looking for. Thank you very much!
– davioooh
Nov 29 '18 at 15:47
add a comment |
Seems the library does not provide an option to assert the way you described (even AbstractIntArrayAssert does not allow to do so), but you can:
1) use AbstractIterableAssert#allMatch(Predicate)
assertThat(asList(0, 2, 3))
.allMatch(i -> i > 0);
2) If you really want to use something like isGreaterThan on ListAssert, you can create a custom ListAssert:
public class CustomListAssert<E> extends ListAssert<E> {
public static <E> CustomListAssert<E> assertThat(List<E> list) {
return new CustomListAssert<>(list);
}
public CustomListAssert(List<? extends E> actual) {
super(actual);
}
public IntListAssert extractingInt(ToIntFunction<E> mapper) {
return new IntListAssert(actual.stream()
.mapToInt(mapper).boxed().collect(toList()));
}
}
public class IntListAssert extends ListAssert<Integer> {
public IntListAssert(List<Integer> actual) {
super(actual);
}
public IntListAssert allGreaterThan(int i) {
allMatch(n -> n > i);
return this;
}
}
And use it like this:
@Test
public void test() {
CustomListAssert.assertThat(asList(0, 2, 3))
.extractingInt(i -> i * 2)
.allGreaterThan(0);
}
What in my case yields:
java.lang.AssertionError:
Expecting all elements of:
<[0, 4, 6]>
to match given predicate but this element did not:
<0>
Seems the library does not provide an option to assert the way you described (even AbstractIntArrayAssert does not allow to do so), but you can:
1) use AbstractIterableAssert#allMatch(Predicate)
assertThat(asList(0, 2, 3))
.allMatch(i -> i > 0);
2) If you really want to use something like isGreaterThan on ListAssert, you can create a custom ListAssert:
public class CustomListAssert<E> extends ListAssert<E> {
public static <E> CustomListAssert<E> assertThat(List<E> list) {
return new CustomListAssert<>(list);
}
public CustomListAssert(List<? extends E> actual) {
super(actual);
}
public IntListAssert extractingInt(ToIntFunction<E> mapper) {
return new IntListAssert(actual.stream()
.mapToInt(mapper).boxed().collect(toList()));
}
}
public class IntListAssert extends ListAssert<Integer> {
public IntListAssert(List<Integer> actual) {
super(actual);
}
public IntListAssert allGreaterThan(int i) {
allMatch(n -> n > i);
return this;
}
}
And use it like this:
@Test
public void test() {
CustomListAssert.assertThat(asList(0, 2, 3))
.extractingInt(i -> i * 2)
.allGreaterThan(0);
}
What in my case yields:
java.lang.AssertionError:
Expecting all elements of:
<[0, 4, 6]>
to match given predicate but this element did not:
<0>
edited Nov 28 '18 at 19:30
answered Nov 28 '18 at 19:20
caco3caco3
1,9531720
1,9531720
I didn't knowallMatchmethod. That's exacly what I was looking for. Thank you very much!
– davioooh
Nov 29 '18 at 15:47
add a comment |
I didn't knowallMatchmethod. That's exacly what I was looking for. Thank you very much!
– davioooh
Nov 29 '18 at 15:47
I didn't know
allMatch method. That's exacly what I was looking for. Thank you very much!– davioooh
Nov 29 '18 at 15:47
I didn't know
allMatch method. That's exacly what I was looking for. Thank you very much!– davioooh
Nov 29 '18 at 15:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53526156%2fassert-same-condition-on-all-elements-of-a-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown