Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is anyone out there using Robolectric without Maven on IntelliJ? [closed]

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.

like image 632
Chris Danson Avatar asked Jun 03 '13 22:06

Chris Danson


2 Answers

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!

like image 154
levibostian Avatar answered Sep 18 '22 18:09

levibostian


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.

like image 28
GreyBeardedGeek Avatar answered Sep 18 '22 18:09

GreyBeardedGeek