6
@DataProvider
public Iterator<Object[]> validLogin() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(new File("src/test/java/resources/UserData/login.xml")));
    String xml = "";
    String line = reader.readLine();
    while (line != null) {
        xml += line;
        line = reader.readLine();
    }
    XStream xStream = new XStream();
    xStream.processAnnotations(User.class);
    List<User> users = (List<User>) xStream.fromXML(xml);
    return users.stream().map((g) -> new Object[]{g}).collect(Collectors.toList()).iterator();
}

I see this warning

unchecked cast 'java.lang.object' to 'java.util.list '   

at

List<User> users = (List<User>) xStream.fromXML(xml) ;

How can I avoid them?

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user3649636
  • 71
  • 1
  • 1
  • 4

1 Answers1

14

In your example, it does not look like you need this to be a List<User>, so you can do

List<?> users = (List<?>) xStream.fromXML(xml);

If you do need it to be typed, there is no way to avoid the warning. If you are confident that the type is correct, use @SuppressWarnings("unchecked"). If there is a doubt, write some code to assert that all elements are indeed of the correct type (I wish the JDK had a helper method for that).

Thilo
  • 257,207
  • 101
  • 511
  • 656