title: Creating / Mocking Android Context from a Java Application
body: <p>I am trying to write a java program that uses some functions in an Android project.
  I don't need an Android device or emulator, I just need to use some of the functions of the Android project.</p>
  
  <p>The problem I'm running into is this: I'm trying to run something like this FROM JAVA:</p>
  
  <pre><code>AndroidProject testService = new AndroidProject(_context);
  testService.start();
  </code></pre>
  
  <p>But "Context" is an Android thing, and I just can't figure out how to create one, or mock one, or get one from my Java class.</p>
  
  <p>I'm not actually interested in the context, I'm really just interested in the <code>testService.start()</code>, but there is no () constructor for AndroidProject, just the <code>AndroidProject(context)</code> constructor is available.</p>
  
  <p>I've tried</p>
  
  <pre><code>Context _context;
  _context = null;
  AndroidProject testService = new AndroidProject(_context);
  testService.start();
  </code></pre>
  
  <p>But I get a <code>Exception in thread "main" java.lang.RuntimeException: Stub!</code> error</p>
  
  <p>I've also tried</p>
  
  <pre><code>MainActivity myMainActivity = new MainActivity();
  Context _context;
  _context = myMainActivity.getBaseContext(); // also tried getApplicationContext()
  </code></pre>
  
  <p>I've started looking into Mocking, but don't know if I'm making it too complicated or not...</p>
  
  <ul>
  <li>"<strong><em>Mockito</em></strong>" only mocks interfaces, and I want to mock the
  <code>MainActivity</code> class, </li>
  <li>"<strong><em>Android Mock</em></strong>" was too complicated, and I
  couldn't get my project to build with   <code>MainActivity myMockObject =
  AndroidMock.createMock(MainActivity.class);</code> </li>
  <li>"<strong><em>Robotium</em></strong>" seems like it's an Android application that tests an Android application.</li>
  </ul>
  
  <p><strong>I just want to create some sort of Android dummy context and use it in my java application, to get to the other functions of that Android class...</strong> is this possible?</p>