Android Unit Testing with JUNIT Framework -
i have started unit testing in android
placed test folder under src path /src/test.
my /src/main/java/com/example/prabhu/mathapplication consists of activities , java classes.
i want run test check wether method gets executed or doesn't unable resolve error understanding due not finding link.so while writing tests how resolve , how use mocks this.
my test code :
/** * created prabhu on 12/05/15. */ import com.example.prabhu.mathapplication.calcnative; import com.example.prabhu.mathapplication.mainactivity; import junit.framework.testcase; import org.junit.after; import org.junit.before; import org.junit.*; public class myaddertest extends testcase { private mainactivity mmyadder; // private calcnative do_calculation_in_native; int x,y; @before public void setup() throws exception { // code wish run before each test mmyadder = new mainactivity(); // do_calculation_in_native = new calcnative(); x=2; y=3; } @after public void teardown() throws exception { // code wish run after each test } @test public void testadd() { mmyadder.iterate_java(9999,9999,9999); // boolean x = do_calculation_in_native.perform_calculations(1234,1234,1234); int z = x+y; assertequals(z,5); // assertequals(true,x); } }
i don't errors if comment out lines containing class , test cases work ...
the error :
java.lang.unsatisfiedlinkerror: no calcnative in java.library.path @ java.lang.classloader.loadlibrary(classloader.java:1857) @ java.lang.runtime.loadlibrary0(runtime.java:870) @ java.lang.system.loadlibrary(system.java:1119) @ com.example.prabhu.mathapplication.mainactivity.<clinit>(mainactivity.java:118) @ myaddertest.setup(myaddertest.java:22) @ org.junit.internal.runners.junit38classrunner.run(junit38classrunner.java:86) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:78) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:212) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:68) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ com.intellij.rt.execution.application.appmain.main(appmain.java:140)
my project structure below :
the mainactivity.java follows :
package com.example.prabhu.mathapplication; import android.app.activitymanager; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.radiobutton; import android.widget.textview; public class mainactivity extends actionbaractivity { radiobutton jni_radio,java_radio; edittext number1,number2,iterate; int input_number1,input_number_2,iterator,increment; boolean check_operations; textview add_number,diff_between,product_of,divided_by; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); jni_radio = (radiobutton)findviewbyid(r.id.radiobutton); java_radio = (radiobutton)findviewbyid(r.id.radiobutton2); number1 =(edittext)findviewbyid(r.id.edittext); number2 = (edittext)findviewbyid(r.id.edittext2); iterate = (edittext)findviewbyid(r.id.edittext3); try { input_number1 = integer.parseint(number1.gettext()+""); input_number_2 = integer.parseint(number2.gettext()+""); iterator = integer.parseint(iterate.gettext()+""); }catch (exception e) { } if(java_radio.ischecked()) { iterate_java(input_number1,input_number_2,iterator); } else if(jni_radio.ischecked()) { check_operations = (boolean) calcnative.perform_calculations(input_number1,input_number_2,iterator); } } public static void iterate_java(int input_number1,int input_number_2,int iterator) { for(int increment = 0;increment<iterator;increment++) { int l = input_number1+input_number_2; int j = input_number1-input_number_2; int k = (input_number1*input_number_2); int = input_number1/input_number_2; } } static { system.loadlibrary("calcnative"); }
modify code below :
try { system.loadlibrary("calcnative"); } catch (unsatisfiedlinkerror e) { // todo: handle exception }
explanation : test framework not support yet loadlib functions hence apis jni etc not work android test framework. above try-catch block wont harm ur code in anyway escape when run test-cases.
Comments
Post a Comment