0

My code is as follows.

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;

public class MailCheckService {
    public Record[] mailHostValidate(String email, MailEntity mailEntity) {
        Record[] records = null;
        String hostName = email.split("@")[1];
        try {
            Lookup lookup = new Lookup(hostName, Type.MX);
            lookup.run();
            records = lookup.getAnswers();
        } catch (IOException e) {
            throw e;
        }
        return records;
    }
}

how can i mock the Lookup class or rewrite it.

if possible, please provide the version of jar.

Thanks and best regards.

Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • Does this answer your question? [Java \`final\` class and mocking](https://stackoverflow.com/questions/13139581/java-final-class-and-mocking) – dominicoder Jan 05 '22 at 03:46
  • Does this answer your question? [How to mock a final class with mockito](https://stackoverflow.com/questions/14292863/how-to-mock-a-final-class-with-mockito) – Mureinik Jan 05 '22 at 08:48
  • 1. This does not compile. 2. Your method does very little over functionality of lookup - it splits a string. Your try-catch block is useless - you simply rethrow IOException. 3. I would consider introducing LookupService class and mock it instead. 4. Test LookupService by adding your own HostsFileReader (or mocking it) to avoid network calls. – Lesiak Jan 05 '22 at 09:38

0 Answers0