2

When I run a Andriod test (junit) test in Android Studio I get the folowing errormessage. Exception in thread “main” java.lang.NoClassDefFoundError: junit/textui/ResultPrinter.

I assume the error is because it cannot find the junit.jar file. When I follow below junit setup it does not fix the error:

http://www.tutorialspoint.com/junit/junit_environment_setup.htm

I cannot get above setup working. It still cannot find the junit classes. Any ideas how to fix the problem.

Martin
  • 21
  • 3
  • Did you set up system variables? Wont work with user set variabele – ImAtWar Feb 27 '15 at 17:46
  • this may answer your question: http://stackoverflow.com/questions/19516289/exception-in-thread-main-java-lang-noclassdeffounderror-junit-textui-resultpr – IntegralOfTan Feb 27 '15 at 18:06
  • 1
    @IntegralOfTan This does not lead to a solution. – Martin Mar 01 '15 at 00:48
  • @ImAtWar yes system variables in windows 7. Altered the classpath. Should work but does not but that does not matter Android testcases just needs to work in Android Studio and it currently does not. – Martin Mar 01 '15 at 00:49

1 Answers1

0

Did you put your test class in correct place in the folder structure, Martin?

Look at the table under "Flavors and build types support" on http://tools.android.com/tech-docs/unit-testing-support

It resolved the issue for me, but I had to move the test class (and create folders) manually - while Android Studio can generate the test suite automatically for you (Go to... Test, after right-clicking on the class header), it doesn't offer to put it in right location by default, so it can be confusing.

Also make sure to set Test Artifact to "Unit Tests" in Build Variants tab, but the official tutorial says that more explicitly so I assume that's not what you overlooked.

I understand your question was dated, but maybe someone stumbles upon it like I only just did struggling with this issue myself ;) And guys, remember - highest voted answers on StackOverflow that tell you to stick to Android tests and drop JUnit are obsolete now, Android Studio supports JUnit tests that run on your PC directly, as explained in the link I posted above. Of course they don't give you access to instrumentation - I use them to test my "pure" Java logic which is platform agnostic.

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91