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

ClassNotFoundException when running in UnitTest #120

Closed
rekire opened this issue Oct 19, 2021 · 4 comments
Closed

ClassNotFoundException when running in UnitTest #120

rekire opened this issue Oct 19, 2021 · 4 comments

Comments

@rekire
Copy link

rekire commented Oct 19, 2021

I'm evaluating this library for a project and I started using it in a UnitTest, but it crashes:

WARNUNG: Exception in findDNSByReflection
java.lang.ClassNotFoundException: android.os.SystemProperties
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:315)
	at org.minidns.dnsserverlookup.AndroidUsingReflection.getDnsServerAddresses(AndroidUsingReflection.java:37)
	at org.minidns.DnsClient.findDNS(DnsClient.java:253)
	at org.minidns.DnsClient.findDnsAddresses(DnsClient.java:309)
	at org.minidns.DnsClient.getServerAddresses(DnsClient.java:102)
	at org.minidns.DnsClient.query(DnsClient.java:147)
        ...

In my opinion you should check first if you are running on Android before executing Android specific code. I just want to quote the first sentence of the readme: "MiniDNS [...] is a DNS library for Android and Java SE." A unit test falls in my opinion in that case.

I like the idea of fallback to a fixed DNS server, but why just one IPv4 and IPv6? Google has 2 of both and there are also public alternatives like 1.1.1.1 or 9.9.9.9

When I find some free time I will create a PR in order to fix that. If anyone else is quicker then go on, that part is no rocket science.

@Flowdalic
Copy link
Collaborator

Flowdalic commented Oct 19, 2021

In my opinion you should check first if you are running on Android before executing Android specific code.

You mean like

public boolean isAvailable() {
return PlatformDetection.isAndroid();
}

I am not sure why this doesn't work for you. How do you invoke the unit tests?

I like the idea of fallback to a fixed DNS server, but why just one IPv4 and IPv6?

Hardcoded DNS servers are a sensitive topic due the privacy implications. I tend to change MiniDNS so that they are not populated by default, and provide convenience methods like addGoogleDnsServersAsFallback(). Furthermore there is currently no method to modify the hardcoded server set (you can do it via reflection though). Please also check the existing issues around hardcoded servers, like #72 and #102.

@rekire
Copy link
Author

rekire commented Oct 19, 2021

Thank you for your answer. After reviewing my code I found out that it is not crashing, but it is logging that exception. However since I still get the error logged that PlatformDetection seems to be not bulletproof. Might be a check for Class.forName("org.junit.Test") could fix it.

To be honest I just executed the test from Android Studio, I would guess that this executes in the background e.g. gradle test task.

@Flowdalic
Copy link
Collaborator

I think what basically happens is that Android Studio executes the unit tests in an Android(-ish) environment, so that PlatformDetection.isAndroid()returnstrue. However modern Android versions to no longer provide access to android.os.SystemProperties`. Hence the exception.

Might be a check for Class.forName("org.junit.Test") could fix it.

How would that fix it?

@rekire
Copy link
Author

rekire commented Oct 19, 2021

That code could detect if junit is present and therefor not on a real device, however your solution is much better.

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

No branches or pull requests

2 participants