Skip to content
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

Is it okay to create a method just for JUnit use? #85

Open
TerenceTanWT opened this issue Nov 1, 2019 · 3 comments
Open

Is it okay to create a method just for JUnit use? #85

TerenceTanWT opened this issue Nov 1, 2019 · 3 comments
Labels

Comments

@TerenceTanWT
Copy link

For my JUnit, I have to test a lot of methods that adds object into an array list. However, I am unable to directly access those objects for checking via JUnit's assertEquals because the array list is private.

My current implementation is to call my print() method and compare the output with a hardcoded expected output. The problem with this is that the print() method prints not just the object data, but also a lot of formatting such as headers and special characters in order to make the output easy for the user to read. Therefore any change in the printing format will result in all my test cases failing, and I will have to edit all the expected output.

What I am trying to do now is to compare the objects directly using JUnit's assertEquals by creating methods which returns an object from the array list according to index. My question is: does creating methods that are only used in JUnit test and not in my main code violate any guideline? Will I be penalised for that?

@okkhoy
Copy link
Collaborator

okkhoy commented Nov 2, 2019

Don't you have any method to access the arraylist once populated in your product (not specifically created for JUnit)? Your scenario sounds like the case where one of this holds:

  1. you create methods to access the data member just to unit test
  2. ignore unit test and only create integration test so that add + checking values are tested (possibly using 2 different classes/methods).

Calling print to check seems like a bad idea. Either of the above 2 is OK and acceptable (i.e., not penalized).

@okkhoy okkhoy added the Question label Nov 2, 2019
@TerenceTanWT
Copy link
Author

Thank you for your reply!

Just to clarify your point 1: If I were to create methods to access the data member just to unit test, and that method is not being called anywhere else in the main program, I will not be penalised?

Secondly, if point 1 is okay, is it acceptable for the method to return an entire object for comparison instead? This is because each object has 7 private data and so it will require up to 7 additional methods in order to compare.

The reason I am unable to directly access the getters is because the ArrayList is an object. In order for me to access the object inside the ArrayList, I will have to go through 2 layers of abstraction.

Card Object -> TransactionList Object -> Transaction Object

Transaction Object getters are only accessible in TransactionList Object. I am unable to call the getters from the Card Object, without creating additional getter methods inside the TransactionList Object. If I were to create additional getters, those getter methods will only be used in the JUnit test.

@okkhoy
Copy link
Collaborator

okkhoy commented Nov 3, 2019

Just to clarify your point 1: If I were to create methods to access the data member just to unit test, and that method is not being called anywhere else in the main program, I will not be penalised?

See above!

Secondly, if point 1 is okay, is it acceptable for the method to return an entire object for comparison instead? This is because each object has 7 private data and so it will require up to 7 additional methods in order to compare.
...

If you are returning an entire object, you (i) are probably not thinking unit test (ii) may be doing a shallow comparison by mistake (if not, you anyway access the members of the array list).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants