Link to home
Start Free TrialLog in
Avatar of Ahmet Ekrem SABAN
Ahmet Ekrem SABANFlag for Austria

asked on

Java generics error

Hello!

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
 */
@SuppressWarnings("unchecked")
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(element);
			System.out.print(' ');
		}
	}
		System.out.println();
}

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!
DataService.java
DataServiceTest.java
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ahmet Ekrem SABAN

ASKER

Thank you very much, sir! You opened my eyes. :-)
:)