I am trying to run my soundkeeper class which is not an activity, but it requires a context and I am not sure as to how to provide one for it. I haven't done any unit tests before so I am quite new to doing unit tests, this is the code I have for the test class
public class SoundKeeperTest {
SoundKeeper sounds;
Context mContext;
#Before
public void setUp() throws Exception {
sounds = new SoundKeeper();
}
#After
public void tearDown() throws Exception {
}
#Test
public void testAddSound() {
sounds.initSounds(mContext);
}
#Test
public void testPlaySound() {
fail("Not yet implemented");
}
}
When I try to get the context method from InstrumentationTestCase, I receive the following error
java.lang.NoClassDefFoundError: android/test/InstrumentationTestCase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.test.InstrumentationTestCase
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 18 more
Android has a number of test case classes that you can use to test various behaviours.
For standard unit testing you extend the AndroidTestCase class which provides you with the ability to use the Junit 3 API to test classes with no Android runtime dependencies.
If however you wish to test classes that have dependencies on the Android runtime i.e. Activity subclasses then you need to look at some of the special TestCase classes.
InstrumentationTestCase is one of the most basic TestCase implementations and provides access to a test context. It is further subclasses by more specialist class that provide convenience methods for testing activity testing, i.e. ActivityInstrumentationTestCase2.
For your purposes you should get access to everything you need through subclassing InstrumentationTestCase and using the included Instrumentation object to get a reference to a Context:
getInstrumentation().getContext();
The Android Testing Documentation guide is a good reference point for understanding the available TestCase classes are their application.
Related
I am using the AWS cognito SDK for Android, version 2.4.3. It works perfectly fine on debug version, but on release versions, in case of error, I cannot get a proper description on the exception. Instead, I get this
com.amazonaws.AmazonClientException: Unable to unmarshall error response (<init> [class java.lang.String]). Response Code: 400, Response Text: Bad Request
at com.amazonaws.http.AmazonHttpClient.a(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.b(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.a(Unknown Source)
at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source)
at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoSuchMethodException: <init> [class java.lang.String]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at com.amazonaws.transform.AbstractErrorUnmarshaller.a(Unknown Source)
at com.amazonaws.transform.JsonErrorUnmarshaller.b(Unknown Source)
at com.amazonaws.services.cognitoidentityprovider.model.transform.InvalidParameterExceptionUnmarshaller.b(Unknown Source)
at com.amazonaws.http.JsonErrorResponseHandler.a(Unknown Source)
at com.amazonaws.http.JsonErrorResponseHandler.b(Unknown Source)
at com.amazonaws.http.JsonErrorResponseHandler.a(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.a(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.b(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.a(Unknown Source)
at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source)
at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source)
at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:818)
On debug version I get an exception indicating in detail the error. However, in both release and debug user registration succeeds in case all fields are well written. I am using proguard on release and I am aplying the rules from this file
Any help with this issue will be appreciated.
After trying different Proguard settings, following is the minimal configuration, additional to that suggested on AWS SDK repository, to make it work
-keep class com.amazonaws.services.cognitoidentityprovider.** { *; }
it must be added to app/proguard-rules.pro
Unmarshalling errors can also occur with classes in the AWS core library, not just com.amazonaws.services.cognitoidentityprovider. This rule will prevent this crash for all AWS lib exceptions:
-keep class * extends com.amazonaws.AmazonClientException { *; }
I got the error, whenever I run my app on my phone:
FATAL EXCEPTION: main
E/AndroidRuntime(18680): java.lang.NoSuchMethodError: No static method zzb(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzaa; or its super classes (declaration of 'com.google.android.gms.common.internal.zzaa' appears in /data/app/***/base.apk)
E/AndroidRuntime(18680): at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
E/AndroidRuntime(18680): at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
E/AndroidRuntime(18680): at android.app.ActivityThread.installProvider(ActivityThread.java:5159)
E/AndroidRuntime(18680): at android.app.ActivityThread.installContentProviders(ActivityThread.java:4754)
E/AndroidRuntime(18680): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4694)
E/AndroidRuntime(18680): at android.app.ActivityThread.access$1600(ActivityThread.java:150)
E/AndroidRuntime(18680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
E/AndroidRuntime(18680): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(18680): at android.os.Looper.loop(Looper.java:148)
E/AndroidRuntime(18680): at android.app.ActivityThread.main(ActivityThread.java:5423)
E/AndroidRuntime(18680): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(18680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
E/AndroidRuntime(18680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I imported Facebook SDK, Firebase, Adjust, Unity Purchasing and Smartfox to my Unity Project.
I mentioned the problem in my previous topic and I think that opening new topic for the problem will be better. Here is my previous topic which is related to this one for better understanding:
Google.JarResolver.ResolutionException: Cannot resolve com.google.firebase:firebase-analytics-unity:1.0.0()
If you need further information, let me know.
try to
1. in your build.gradle put this:
defaultConfig {
[...]
multiDexEnabled true
}
2. Make your application class extend MultiDexApplication
See answer at NoSuchMethodError: No static method zzb
compile 'com.google.firebase:firebase-core:10.0.0'
You lack a jar package play-server-basement.jar
I have around 100 unit test in my Android project. Everything work perfectly. I use JUnit + Mockito + PowerMock (when dealing with difficult static class).
While writing my latest test, I was surprise with this very strange error that I don't understand.
When I do this in the setUp() function:
playerAccessor = mock(PlayerAccessor.class);
All the tests crash with this very strange error:
java.lang.NoClassDefFoundError: android/media/MediaPlayer$OnCompletionListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:178)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)
at org.mockito.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:349)
at org.mockito.cglib.proxy.Enhancer.getMethods(Enhancer.java:422)
at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:457)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:124)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:60)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:52)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:43)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:32)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1258)
at org.mockito.Mockito.mock(Mockito.java:1135)
at com.stingray360.clap.ondemand.OnDemandMessageHandlerTest.setUp(OnDemandMessageHandlerTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:133)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.media.MediaPlayer$OnCompletionListener
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 62 more
My only clue is that the PlayerAccessor have a function called register which take an object Player which implement the OnCompletionListener interface. But I'm not mocking the Player, I'm mocking the PlayerAccessor.
Do I need to specify something in my mock function so it doesn't try to mock every object used by the PlayerAccessor? Is it something missing from my test (import???).
EDIT
One answer suggest me to put the needed library (com.google.android) in the "test" scope but since it's the android library, the scope NEED to be "provided". If I change it, it's true that my unit test start to work but when I run the app, I receive big errors in the console telling me that it's a very bad thing to do to include the android jar... So what are the other solution???
If the class you're mocking imports another class you have to have that class on the classpath or the class loader will fail, regardless whether you're actually using any of the functionality afforded by it.
If you're using maven for builds you can easily add a dependency jar with test scope
Eclipse should respect pom configuration
I want to use JUNIT for some network classes in my Android-Project. I am using Apache HttpClient which is already included in android. But when I run my junit test it comes to an exception. I included all my lib files in the Junit-Testrunner. But obviously it is not enough because the HttpClient is not found. Does some know what I am doing wrong?
Here is the exception
java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
at de.srs.android.pdixuploader.network.test.ClientAppTest.testCheckLogin(ClientAppTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 19 more
I resolved this by adding the JUnit library to my project:
Open project's Properties > Java Build Path
Open the Libraries tab
Click Add Library
Select JUnit
Click Next
Make sure that the correct version of JUnit is selected in the dropdown
Click Finish
I have created test project...I have updloaded junit.jar in the (Run time configuration class path --> user entries --> junit.jar
I am getting below errror when i run the code...
java.lang.NoClassDefFoundError: android/test/ActivityInstrumentationTestCase2
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.test.ActivityInstrumentationTestCase2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 17 more
You need to run it as Android JUnit test, instead of JUnit test.
Go to the project in the project explorer->right click->Android JUnit test
android.test.ActivityInstrumentationTestCase2 is not in the JUnit JAR that you claim to have added to classpath.
That looks like one of yours.
Did you compile the code? If not, please do so.
Did you add the path to the location where your .class files are written? If not, please do so.
Follow these instructions: http://developer.android.com/guide/developing/testing/testing_eclipse.html
Basically, you don't have to add junit.jar to your classpath.
I had the similar problem. For me Changing the "Order and Export" (properties->build Path->Order and Export) of robotium-solo.jar i.e moving it to the top and explicitly selecting run as 'android JUnit test' by right clicking the project solved the problem. Also try running the sample project provided.
It might sound strange. but all you need to do is import solo before you import the class and it works. At least it did for me. i dont know why i tried that but all it needed was to cut the import com.jayway.android.robotium.solo.Solo; and paste it just after the package.