0

While looking up for unit testing for Android applications, I came across a lot of voices over how it is difficult to test-unit an Android app.

For example:

Dependency issue - why is this a problem for unit-testing in Android?

What would be "dependent" in this case that testing is impossible?

For example:

public String getName()
{
    mName = "Android";
    return mName;
}

Would this be hard to unit-test? If so, why? Is it because it "depends" on String class? But why can't it depend on String class as far as unit-testing is concerned? If my sample above and my understanding is off, please correct me.

Another issue I heard is:

"the android.jar is not on the machine! It's on the emulator, that's why it makes unit-testing on Android a living hell!"

But, why would it be a living hell if it is installed on the emulator?

Another issue I hear the most is this:

The Activity class. Some say it is a view, some say it is a controller because it handles creation of objects, too. That's why this is an issue. But I don't get it at all. Why would this cause problem?

Another thing I am trying to get my head around is:

Why in both Android and Java they need to "mock" a class to unit-test? Why? In Objective-C for example, I have never heard of a need such as this.

Thank you.

1 Answer 1

1

This is a lot of questions that you might consider splitting up, but here goes:

  • Dependency issue - why is this a problem for unit-testing in Android?

Really, this is as much of a problem for Android as it is for anything else. Depending on how you architect your application, isolating from dependencies can easy or hard. There's nothing in your getName example that makes it impossible or even difficult because that example is not actually dependent on anything besides the base libraries (the String class).

  • But, why would it be a living hell if it is installed on the emulator?

It's not really, it's just different than a lot of other platforms. You have to run tests on a device or emulator which is a little strange.

  • The Activity class.

I think this is somewhat people creating their own problems coupled with a platform that doesn't try to prevent them from creating these problems. Like I mentioned earlier, depending on how you architect your code, it's either going to be easy or hard to isolate it for unit testing. I think a lot of difficulty comes into Android development because the Activity class is used for more than what it should be used for. In most code I've seen it's used as a view, controller, model, and whatever else you'd want.

  • Why in both Android and Java they need to "mock" a class to unit-test? Why? In Objective-C for example, I have never heard of a need such as this.

In some ways mocking is the core of unit testing. It's not specific to Android or Java. The point of unit testing is testing a single piece of code in isolation. If you've seen tests before that aren't mocking dependencies they are not unit tests. It sounds like you might want to read up on what exactly unit testing is - the wikipedia page is a good introduction: https://en.wikipedia.org/wiki/Unit_testing

I just searched for iOS mocking framework, and this was the first result: http://ocmock.org/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.