Reputation: 917
I am trying to test my project, and I am having the following error :
Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
I tried to install JUnit as mentioned in here (without 6-7 steps which I didn't understand) and here . But still having the same problem.
When digging in the exception, I found that some files are using the library libcore which cannot be found (showing cannot resolve symbol 'libcore'
). The same is for the library dalvik.system.VMStack
(showing cannot resolve symbol 'VMStack'
).
Upvotes: 2
Views: 2850
Reputation: 177
I had same issue, but solved it! JUnit test framework provides the following important features :
I successfully used all feature except CREATING TEST RUNNER CLASS like below (I got error every time ran it.)
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
TestJunit.class is Your Test Class like below :
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
@Test
public void testAdd() {
String str = "Junit is working fine";
assertEquals("Junit is working fine", str);
}
}
Finally, Solution : Go to Menu Run --> Edit Configuration --> Left Panel --> Under Android JUnit Option --> Select Every item and click Minus sign.
This Link Covers Tutorial about JUnit Classes : https://www.tutorialspoint.com/junit/junit_test_framework.htm
Attention : After EVERY Changes in ExampleUnitTest Class, First Right Click On ExampleUnitTest.class and run it then Run TestRunner.
Good Luck!
Upvotes: 0
Reputation: 161
I've had the same problem and it turned out to be a result of my settings in Android Studio. Essentially the Android Studio default (or perhaps something I had set at some point?) was running my tests in Java JUnit and not Android JUnit.
To check for this issue, go to the top bar and look for the "Run" dropdown, then try clicking on "Edit Configurations". You should see a "Run/Debug Configurations" window pop up.
In the leftmost pane, there should be only "Android Application", "Android Tests", and "Defaults" sections. If any files are in the "JUnit" folder and NOT the "Android Tests" folder, your tests probably won't run properly. To fix it, select the plus sign in the top left corner and select "Android Tests". This action allows you to create a new run configuration as an Android Test. Enter any important information at the top for the file/files your changing run configurations for. For example, if "TestDB" was initially under "JUnit", make a new Android Test configuration, select the "Class" radio button, and find the appropriate class in the new text bar. You can also use "all in app" or "all in package" to create settings for your whole test set.
It's very important to select everything under "JUnit" and click the minus button at the top to delete those run configurations. Also important: If you ever get the chance to choose run configurations (i.e. if you right click on a file/package, press "run," and several run options show up), make sure you always pick the one with the Android symbol!
Upvotes: 5
Reputation: 637
I was able to solve this issue with Android Studio. I followed the instructions in http://tools.android.com/tech-docs/unit-testing-support. Ensure that you rename the /src//... where the testing source code lies to /src/test/... using the Rename refactor option in AS.
And finally include the snippet for "Method ... not mocked." error if encountered.
Upvotes: 0
Reputation: 2950
It looks like you forgot to import the result printer.
import junit.textui.ResultPrinter
Upvotes: 0