Java generics error


I need your help because of a problem is a generics code. I reduced the problem to a test case. The code comprises of a class that is to be tested with JUnit, and the associated JUnit test case.

Something is wrong in my code, as the last line in the JUnit test fails. The Debugging class has a method that prints the content of a list:
 * Prints the content of the vector.
 * @param <T> the generic type
 * @param list the vector
public static <T> void printContent(final List<T> list) {
	for (final T element : list) {
		if (element instanceof List) {
			printContent((List<T>) element);
		} else {
			System.out.print(' ');

Open in new window

The list has elements after the method is called, and the contents are printed out by the printContent method as follows:

 generics.DataServiceTest$TrainClassDto@24e86073 generics.DataServiceTest$TrainClassDto@141430f3 generics.DataServiceTest$TrainClassDto@3999abd1 generics.DataServiceTest$TrainClassDto@17df43e2 generics.DataServiceTest$TrainClassDto@42e1b6ac

What I can't see is the error that results in the names of the DTO (data transfer object) objects instead of their contents. If their contents would have been translated, the 0. element would start with the ID of 1, which would make the second test case true.

Thank you for your help!
Ahmet Ekrem SABANSenior IT consultantAsked:
Who is Participating?
CEHJConnect With a Mentor Commented:
Nothing to do with generics. It's because TrainClassDto does not override toString
Ahmet Ekrem SABANSenior IT consultantAuthor Commented:
Thank you very much, sir! You opened my eyes. :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.