0

I have a two string array in java where I am comparing the whole array using assertArrayEquals. example:

Expected:[CTSCAT, CTSSCH, PERSONS, ID, CTSCAT, CTSSCH, ORDERS, PERSONID, 1, 3, 3, FK_PERSONORDER, PK_PERSON, 7]

Actual:[CTSCAT, CTSSCH, PERSONS, ID, CTSCAT, CTSSCH, ORDERS, PERSONID, 1, 3, 3, FK_PERSONORDER, PK_PERSON, 7]

But know I want to compare some element of array instead of entire array. Like in same example I only want to check first two element.

3 Answers 3

1

You can compare with the help of assertEquals:

assertEquals(arr1[0],arr2[0]);

and so on..

Sign up to request clarification or add additional context in comments.

2 Comments

I thought of writing a function where it can use assertEqual for the indexes but i thought their might be some other way for this.
You can try with assertThat clause too...You can convert your arrays into list and then try: assertThat(list1, contains(list2.get(0),list2.get(1)); You can apply combinations further to that
0

Try using AssertJ library. It has very good support for arrays and its much more readable.

You can find the array example here

Comments

0

You can use Hamcrest matchers (javadoc):

assertThat(arr1, Matchers.arrayContaining(arr2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.