I am writing junit test cases for an android project. I wrote junit for activity classes but I dont know how to write test cases for other classes that doesn't inherit from activity. Also, how can I link these classes (activity and non activity classes)?
examples:
public class A extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.someLayout);
B objectB = new B();
pbjectB.getString();
}
}
public class B{
public b(){
}
public String getString(){
return anyString;
}
}
In this example I am able to write junit test cases for class A but I am confused for class B.
For non-Activity classes, you can use standard junit 3 test classes. So make your test for B extend from junit.framework.TestCase
import junit.framework.TestCase;
public class BTest extends TestCase {
public void testGetStringIsNotNull() {
B subject = new B();
assertNotNull(subject.getString());
}
}
Related
I have a BaseActivity which is an abstract activity and isn't registered in AndroidManifest. BaseActivity will call getPresenter in activity's lifecycle.
public abstract class BaseActivity extends AppCompatActivity{
public abstract Presenter getPresenter;
public abstract int getLayout();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
getPresenter().attachView(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
getPresenter().detachView();
}
}
I use ActivityTestRule to launch the BaseActivity, but the following error is shown.
java.lang.RuntimeException: Could not launch activity
How to test the getPresenter().attachView(this) and getPresenter().detachView() are called in correct activity's lifecycle?
I don't have quite big experience with Android Testing, especially unit testing, but I've already found this post, which may be useful for you:
Is it possible to test an Abstract activity with Robolectric
Also on Github page of Robolectric I'd found this: https://github.com/robolectric/robolectric/issues/1441
So all I can say according to your question, that yes you can test your abstract class, at least with Robolectric.
Read also: https://gualtierotesta.wordpress.com/2015/01/28/tutorial-java-abstract-classes-testing/
EDIT: Nowadays, Robolectric doesn't support directly API 23, but you can "downgrade" it in configuration of test class, like below:
#RunWith(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, sdk = 21)
public class MainActivityTest {
MainActivity_ activity = Robolectric.setupActivity(MainActivity.class);
}
The error is shown because the BaseActivity isn't registered in Android Manifest. It seems that there are some solutions to add an activity in test package.
However, I finally choose another solution, delegate the activity's lifecycle to others.
This idea is mentioned in Mosby playbook.
http://hannesdorfmann.com/android/mosby-playbook/
New BaseActivity:
public abstract class BaseActivity extends AppCompatActivity implements BaseMvpView, DelegateCallback{
private ActivityMvpDelegate activityDelegate;
protected ActivityMvpDelegate getActivityDelegate() {
if (activityDelegate == null) {
activityDelegate = createActivityDelegate();
}
return activityDelegate;
}
protected ActivityMvpDelegate createActivityDelegate() {
return new ActivityMvpDelegateImpl(this, this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityDelegate().onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
super.onDestroy();
getActivityDelegate().onDestroy();
}
}
Finally, I can test the delegate class without activity's lifecycle.
I try to create a Robolectric test (3.0-rc02) for the following Activity:
public class NotificationActivity extends ActionBarActivity {
private NotificationFragment fragment;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
}
}
The test is looking like this:
#Config(manifest = IConfig.MANIFEST_PATH, emulateSdk = IConfig.SDK_VERSION, reportSdk = IConfig.SDK_VERSION)
#RunWith(RobolectricTestRunner.class)
public class AbstractFragmentTest {
#Test
public void test() {
Robolectric.buildActivity(NotificationActivity.class).setup().visible();
}
where SDK_VERSION = 18.
When running the test, I get this error:
java.lang.NullPointerException
at org.robolectric.res.builder.DefaultPackageManager.getActivityInfo(DefaultPackageManager.java:164)
at org.robolectric.util.ActivityController.getActivityInfo(ActivityController.java:65)
at org.robolectric.util.ActivityController.attach(ActivityController.java:51)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:309)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:47)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at com.viae.common.view.AbstractFragmentTest.test(AbstractFragmentTest.java:31)
Anyone knows what I'm doing wrong over here?
Issue solved
Using FragmentActivity in stead of ActionBarActivity did the trick to me.
My main logic is in a fragment (DialogFragment) inside the activity. Both activities are supporting the DialogFragment, so for my functional tests it doesn't matter which of the 2 parent activities I use.
ActionBarActivity is deprecated. Use AppCompatActivity instead
First time I'm trying to make my own .jar file. It works, but now I want to give feedback to the MainActivity. So I want to call a function 'receiveSerial()' in the MainActivity.
So the MainActivity must always implement the function 'receiveSerial()' when including my .jar.
.jar file (part of the) code:
package com.hoeks.ma.bluetooth;
import java.util.Set;
import ...
public class Blauwe{
..
private Activity ma;
public Blauwe(Activity m){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ma = (Activity)m;
}
..
public void sendSerial(String s) {
ma.receiveSerial(s); // This line give Eclipse error "Add cast to ma"
// When I add the cast it is not working
}
MainActivity
import com.hoeks.ma.bluetooth.Blauwe;
....
public void receiveSerial(String s) {
javascr.setSerial(s);
}
Note: I do not post the whole code because the code is a big mess right now, its not good for the readability.
1) create interface
public interface ReceiveSerialCallback{
public void receiveSerial(String s);
}
2) add interface implementation in MainActivity
public class MainActivity implements ReceiveSerialCallback{
...
public void receiveSerial(String s) {
// serial received
}
}
3) update Blauwe class
private ReceiveSerialCallback callback;
...
public void setReceiveSerialCallback(ReceiveSerialCallback callback) {
this.callback = callback;
}
...
public void sendSerial(String s) {
callback.receiveSerial(s);
}
4) set inteface callback object to Blauwe class in MainActivity
Blauwe b = new Blauwe();
b.setReceiveSerialCallback(this);
You have to cast because receiveSerial(String) is not a method of Activity, but MainActivity. I would create an interface (with method sendSerial) that MainActivity should implement, and save a reference of this interface in Blauwe class, instead of an Activity instance.
I'm writing an application in which i have a set of code which i want to be available in all of my Activities and ActivityGroups. However, to achieve this, I have extended my activities as:
//custom Activity
public abstract class BaseActivity extends Activity
//custom ActivityGroup
public abstract class BaseActivityGroup extends ActivityGroup
//implemented activities in my app
public class PickUser extends BaseActivity
//and
public class Home extends BaseActivityGroup
Now the thing is, whatever the custom code i write in BaseActivity, I have to write the same in BaseActivityGroup too (as in current implementation). This is prone to code-sync problems and i believe not a good technique.
So, how can i make my extensions in such a way that I only write custom code in BaseActivity and my BaseActivityGroup extends ActivityGroup - which is conceived from BaseActivity class?
If i observe how android does this, so the ActivityGroup in android extends Activity class. And I also want to write my custom ActivityGroup class (known as BaseActivityGroup) that actually extends BaseActivity (which is an extended Activity).
Any ideas/suggestions?
First of all ActivityGroups are bad and should not be used. They are deprecated and it is preferred to use a single activity with multiple fragments.
If you must use an activitygroup you are probably best of by implementing a delegate pattern.
Create a delegate that handles all the common methods such as onCreate, onResume and use that in the bases. In this example I save a reference to the activity in the delegate. This circular referencing might not be the pretties. An alternative is to pass on the activity to the methods in the delegate.
public class ActivityDelegate() {
private Activity mActivity;
public ActivityDelegate(final Activity activity) {
mActivity = activity;
}
public void onCreate(final Bundle savedInstanceState) {
// Do stuff.
}
}
public abstract class BaseActivity extends Activity {
private ActivityDelegate mDelegate = new ActivityDelegate(this);
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
}
...
}
public abstract class BaseActivityGroup extends ActivityGroup {
private ActivityDelegate mDelegate = new ActivityDelegate(this);
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
}
...
}
Add an extra final class, called Base.
This one will only contain methods to be called by the other Base classes, such as for instance:
public static boolean createOptionsMenu(final Menu menu,
final MenuInflater inflater) {
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Then, in your BaseActivity and BaseActivityGroup classes, you would call:
#Override
public final boolean onCreateOptionsMenu(final Menu menu) {
return Base.createOptionsMenu(menu, getMenuInflater());
}
Hope it helps!
Just Extend everything to BaseActivity including BaseGroupActivity as everything is a child of Activity in android
you can put your login in a separate file under a method. now call the same method from both BaseActivity and BaseActivityGroup if you need activity instance in file . pass context through constructor
Is there a way to replace the default Activity class with own implementation extending from this class using roboguice?
For instance an activity like this:
public class MyActivity extends Activity
{...}
would replace the default Activity class and would become the base activity for all other derived activities.
Yes. The easiest way is to have your base activity extend from RoboActivity.
Eg.
class MyBaseActivity extends RoboActivity { ... }
And then have all of your activities extend from MyBaseActivity.
However, if for some reason you don't wish to extend from RoboActivity, you can easily add injection to your own activities by doing the following:
class MyBaseActivity extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
RoboGuice.getInjector(this).injectMembersWithoutViews(this);
}
public void onContentChanged() {
super.onContentChanged();
RoboGuice.getInjector(this).injectViewMembers(this);
}
}
Take a look at RoboActivity's source for more details. As long as you don't need events, the changes required are quite simple.