0

So let's say I have a simple activity as follows. Assume all of the the lifecycle events etc. are correctly created there's no exception in creating an activity.

public class ButtonClickActivity extends Activity implements OnClickListener {

... onCreate etc. ...

  @Override
  public void onClick(View v) {
      logInfo();
  }

  protected void logInfo() {
     Log log = new Logger();
     log.log('foo');
     ...
  }

}

How exactly would you test that the onClickMethod calls on callMyOtherMethod? How also would you test that logInfo makes a new Logger and .log('foo')? Let's assume you have all frameworks at your disposal.

I've tried to use Robolectric (which is fine for high level testing, but really doesn't seem to let you get down into detail like to test if a method calls another method).

It seems that you have to use that in conjunction with a mocking framework / injection in order to verify at least injected object behavior. It doesn't to be able to test 'new Construction' so you're forced to use a factory always for every single object, even for objects you use from other frameworks etc.

I've given a shot with JMockIt (which allows you to verify every single new construction etc.) but it seems there is a problem there also with ByteCode manipulation and it basically will fail if it tries to rewrite bytecode for Android Activities and Views especially, along with other problems.

So my question for folks is what strategy have they been going with other than 'it's a implementation detail' or 'doesn't need to be tested'? I'm sure of course you don't want to test every little detail method call but letting important method calls go unverified doesn't seem like a good solution either.

adrian
  • 2,326
  • 2
  • 32
  • 48
  • Just a note: JMockit requires a JVM, it won't work on Dalvik as it doesn't include all the standard Java APIs. – Rogério Jul 11 '14 at 15:50
  • Yea I know I'm running these using JDK 1.8 on the CLI. In my final conclusion it appears JMockIt cannot rewrite the native Android objects. Byte code exceptions are thrown unless you have any experience with it. – adrian Jul 11 '14 at 16:14
  • I thought it was not possible to run code using Android APIs on a JVM, except through a tool like Robolectric. Are you saying it *is* possible? Is there any recent info on that on the web (I can't seem to find any)? – Rogério Jul 11 '14 at 16:45
  • You're right it isn't. All the methods are stubbed with exceptions if you don't use Robolectric with exceptions. It was my impression however JMockIt would mock all methods and constructors so the expect ions in all constructors etc would be avoided and only mocks of these objects would be called therefore allowing you to mock all Android objects. This appears not possible though... – adrian Jul 11 '14 at 17:40
  • Sorry all exceptions would be avoided because JMockIt stubs these methods and constructors... – adrian Jul 11 '14 at 17:40

0 Answers0