8

All the examples of using Robolectric I can find seem to be Maven based. Is anyone not using Maven? If so I'd really like to understand your IntelliJ project setup.

Having read this post

android-unit-test-approaches

it seems sensible to have a tiered approach to unit testig android projects with a combination of pure junit, robolectric & android test framework tests. If anyone who is doing this with or without Maven I'd love to understand a little bit about how you configured your projects in IntelliJ.

I'm guessing I will need multiple projects / modules. Any wisdom on this gratefully received.

Community
  • 1
  • 1
Chris Danson
  • 247
  • 4
  • 13

3 Answers3

6

Maven is a great tool to make things more convenient. If you are still looking for a way to install Robolectric inside of intellij WITHOUT Maven, follow along:

  1. Download Robolectric: https://oss.sonatype.org/index.html#nexus-search;quick~robolectric download the latest jar-with-dependencies.jar file. Download it and put it in your libs/ directory of your app project

  2. set up your app source with the following file structure (this is a widely used method of setting your directories up for Robolectric testing)

    ProjectName/  
        src/  
             main/  
                 java/  
                      com/  
                          example/  
                                  packageName/  
                                             javaClassesHere.java
             test/
                 java/
                      com/
                          example/
                                  packageName/
                                             javaTestClassesHere.java
    
  3. In Intellij, go to File > Project Structure. Then on the left column, select "Modules".
    In the middle column, select your app module.
    Then in the right column, click the "Sources" tab.
    Now in the file browser, find the "src" directory and expand it. Then expand both your "main" and "test" directories inside of src. Click on the "java" directory under "main" then towards the top, click the blue "Sources" button. Then for the "java" directory under "test", towards the top click the "Test Sources" button.
    Also, click the main "src" directory and DEselect the blue sources button to make it not blue anymore.

  4. Now still in the project structure window, go to the left hand column and click "Libraries".
    If you see an entry for robolectric in the middle column already, click on it then click the red "-" sign in the top of the middle column.
    Now click the green "+" sign above the middle column, and select "Java". Browse to your robolectric jar you downloaded in step 1. (should be in your libs/ directory of your project.
    A window will now come up asking you what modules you want to use robolectric on. Select all the modules that are going to be using robolectric and click ok. Now click apply and ok to get out of Project Structure.

You are now all set up for testing with robolectric!

levibostian
  • 753
  • 1
  • 7
  • 21
  • Maven is an over-complex tool for Mobile development. It might be very good and useful for server side development but not for simple Mobile apps development. Ant works out the best and supported out of the box by Android SDK. – Sileria Aug 21 '13 at 18:10
  • 1
    @Mobistry I've had the opposite experience. Maven (along with Nexus) has helped my team tremendously. Dependency management is important for any software project, especially when it starts to become complex. – Christopher Perry Nov 07 '13 at 07:14
  • Is there any way to run Robolectric tests on Eclipse (Juno) **without** Maven? I haven't had any luck with the quick start steps on the robolectric website. – JaKXz Jan 07 '14 at 18:01
3

I'm guessing that the reason that most people wo are using Roboelectric are using Maven is that most people who bother to use Roboelectric are serious about their testing.

Given that they are motivated to put a serious amount of effort into testing, they are highly likely to want to be able to include their tests in an automated build, typically in a Continuous Integration (CI) server, such as Jenkins or Hudson.

When you want to do automated testing, you need a good command-line build. Gradle is the brand-new official command-line build tool for Android, but being brand-new, is still a work in progress, and not yet widely adopted. The tried-and-true tool is Maven.

Lots of people love to hate Maven, but it gets the job done, and beats Ant by a mile.

So, it may not be a bad idea to use Maven (or Gradle) - it will give you a lot more bang for your testing buck than just running your tests in your IDE.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Thanks for that. I do want to setup CI for my project and you've helped me settle on Maven for now. I've been listening to a Maven detractor who quite reasonably sites long build times & complicated dependency issues as reasons not to use it. I'm no expert in building Java though and it does seem that Maven can eventually be coerced into doing what you want. – Chris Danson Jun 04 '13 at 00:38
  • Maven is not entirely at fault for long build times and complicated dependency issues - the developers of the software who chose to use a complicated web of inter-dependent libraries are responsible for that. Maven is simply a tool that can be used to manage those dependencies. But Maven haters 'gonna hate... Anyway, the new Gradle build tool will be able to manage those same dependencies, and hopefully will do an even better job. – GreyBeardedGeek Jun 04 '13 at 01:14
2

I am an early bird in the Robolectric sky and started with not going the Maven way. My main motivation was to try out the framework and hence never gave a second thought for using Maven. I have documented my learnings at :
http://dasherize.blogspot.in/2013/04/robolectric-beginning.html
and you can find setup instructions here.

Note: It is with respect to Robolectric 1.x. But it should not vary much with change in version.

As for this answer I completely agree with @GreyBeardedGeek. You have to settle down with Maven if you wish to setup CI.

Community
  • 1
  • 1
Jigish Chawda
  • 2,148
  • 1
  • 22
  • 29