Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Change tests in ArrayList using Reflection API #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

Copy link

@ivanvirchenko ivanvirchenko commented Jan 29, 2021

No description provided.

Copy link
Contributor

@tboychuk tboychuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 overall looks good! 👍
Please address comments and don't forget to extract try-catch blocks from all tests.

assertThat(arrayList.get(0)).isEqualTo(10);
assertThat(arrayList.get(1)).isEqualTo(15);
assertThat(arrayList.get(2)).isEqualTo(20);
Object[] elements = (Object[]) getObjectByName("elementData");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 it's not safe to get this object by name since people can use other names for this field. That's why you should find array type filed, as I did in PoC

arrayList.add(15);
arrayList.add(20);

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 test should not contain these try-catch blocks. Please create a helper method setTestSize(size) and move this logic to that method. You can also mark that method with @SneakyThrows

arrayList.add(10);
arrayList.add(15);
arrayList.add(20);
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 this logic looks like a good candidate for a test method:

 @SneakyThrows
 private void fillTestArray(Object... elements){
 getFieldByName("elementData").set(arrayList, elements);
 getFieldByName("size").set(arrayList, elements.length);
 }

@@ -94,9 +109,16 @@ public void getLastOfEmptyList() {
public void listWithSpecificCapacity() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 let's rename this method to createListWithSpecificArrayCapacity

arrayList.add(10);
arrayList.add(15);
arrayList.add(20);
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 this logic is redundant, we just need to verify that array capacity equals the one we passed to the constructor. So please remove this logic as well as assertions, verify that the inner array length is correct.


assertThat(elements[2]).isEqualTo(10);
assertThat(elements[3]).isEqualTo(78);
assertThat(size).isEqualTo(4);
}

@Test
@Order(22)
public void setElementByIndexEqualToSize() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 let's rename this test method to setElementByIndexThrowsExceptionWhenIndexIsOutOfBound


assertThat(elements[2]).isEqualTo(78);
assertThat(elements[1]).isEqualTo(69);
assertThat(size).isEqualTo(4);
assertThat(removedElement).isEqualTo(58);
}

@Test
@Order(25)
public void removeElementByIndexEqualToSize() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 let's remant test method to removeElementByIndexThrowsExceptionWhenIndexEqualsSize

int size = (int) getObjectByName("size");

assertThat(elements[3]).isEqualTo(78);
assertThat(size).isEqualTo(4);
assertThat(removedElement).isEqualTo(100);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 please remove this last assertsion

@@ -287,7 +375,11 @@ public void removeLastElementByIndex() {
@Test
@Order(27)
public void removeElementByIndexOutOfBounds() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 please rename to removeElementByIndexThrowsExceptionWhenIndexIsOutOfBounds

arrayList.clear();

assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> arrayList.get(0));
}

private Object getObjectByName(String name) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erneman1 please remove all unnecessary blank lines

Copy link
Contributor

@shryhus shryhus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a new testing logic we can change our test ordering to a logical and natural sequence of the exercise performing. For instance performing all the "add" test firstly.

@ivanvirchenko ivanvirchenko deleted the GP-49-RefactorArrayListTestUsingReflectionAPI branch January 29, 2021 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@tboychuk tboychuk tboychuk requested changes

@shryhus shryhus shryhus requested changes

@v-ghost03 v-ghost03 Awaiting requested review from v-ghost03

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /