Rectangle 27 32

(Option 1) - Running JUnit tests with Android Studio using a Java modu...

Note that at the time of writing robolectric 2.4 is the latest version and has no support for appcompat v7 library. Support wil be added in robolectric 3.0 release (no ETA yet). Also ActionBar Sherlock can cause issues with robolectric.

To use Robolectric within Android Studio you have 2 options:

This technique uses a java module for all your tests with a dependency to your android module and a custom test runner with some magic:

Also check the link at the end of that post for running the tests from android studio.

I encountered a few issues setting up junit tests to run from gradle in Android Studio.

This is a very basic sample project for running junit tests from a gradle based project in Android Studio: https://github.com/hanscappelle/android-studio-junit-robolectric This was tested with Android Studio 0.8.14, JUnit 4.10, robolectric gradle plugin 0.13+ and robolectric 2.3

The build script is the build.gradle file in the root of you project. There I had to add robolectric gradle plugin to classpath

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.13.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+'

    }
}

allprojects {
    repositories {
        jcenter()
    }
}

In the build script of your app module use the robolectric plugin, add robolectric config and add androidTestCompile dependencies.

apply plugin: 'com.android.application'
apply plugin: 'robolectric'

android {
    // like any other project
}

robolectric {
    // configure the set of classes for JUnit tests
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'

    // configure max heap size of the test JVM
    maxHeapSize = '2048m'

    // configure the test JVM arguments
    jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'

    // configure whether failing tests should fail the build
    ignoreFailures true

    // use afterTest to listen to the test execution results
    afterTest { descriptor, result ->
        println "Executing test for {$descriptor.name} with result: ${result.resultType}"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile 'org.robolectric:robolectric:2.3'
    androidTestCompile 'junit:junit:4.10'
}

Now put the test classes in the default location (or update gradle config)

app/src/androidTest/java

And name your tests classes ending with Test (or again update config), extending junit.framework.TestCase and annotate test methods with @Test.

package be.hcpl.android.mytestedapplication;

import junit.framework.TestCase;
import org.junit.Test;

public class MainActivityTest extends TestCase {

    @Test
    public void testThatSucceeds(){
        // all OK
        assert true;
    }

    @Test
    public void testThatFails(){
        // all NOK
        assert false;
    }
}

Next execute the tests using gradlew from command line (make it executable using chmod +x if needed)

./gradlew clean test
Executing test for {testThatSucceeds} with result: SUCCESS
Executing test for {testThatFails} with result: FAILURE

android.hcpl.be.mytestedapplication.MainActivityTest > testThatFails FAILED
    java.lang.AssertionError at MainActivityTest.java:21

2 tests completed, 1 failed                                  
There were failing tests. See the report at: file:///Users/hcpl/Development/git/MyTestedApplication/app/build/test-report/debug/index.html
:app:test                      

BUILD SUCCESSFUL

Just like you can have your java source files somewhere else you can move your test source files. Just update gradle sourceSets config.

sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        androidTest {
            setRoot('tests')
        }
    }

You forgot to add the junit test dependency in the app build script

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile 'org.robolectric:robolectric:2.3'
    androidTestCompile 'junit:junit:4.10'
}

You're running this test with the run configurations from Android Studio instead of command line (Terminal tab in Android Studio). To run it from Android Studio you'll have to update the app.iml file to have the jdk entry listed at the bottom. See deckard-gradle example for details.

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
    at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
    at junit.textui.TestRunner.<init>(TestRunner.java:54)
    at junit.textui.TestRunner.<init>(TestRunner.java:48)
    at junit.textui.TestRunner.<init>(TestRunner.java:41)
    at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:190)
    at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:173)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

See this SO question for a solution. Add the below export to you bash profile:

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
ERROR: JAVA_HOME is set to an invalid directory: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

If you want to run your tests from Android Studio Junit Test runner instead you'll have to expand the build.gradle file a little more so that android studio can find your compiled test classes:

sourceSets {
    testLocal {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')
    }
}

android {

    // tell Android studio that the instrumentTest source set is located in the unit test source set
    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

dependencies {

    // Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
    testLocalCompile 'junit:junit:4.11'
    testLocalCompile 'com.google.android:android:4.1.1.4'
    testLocalCompile 'org.robolectric:robolectric:2.3'

    // Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
    // which is Android Studio's test task
    androidTestCompile 'junit:junit:4.11'
    androidTestCompile 'com.google.android:android:4.1.1.4'
    androidTestCompile 'org.robolectric:robolectric:2.3'

}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir

    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }

    classpath = sourceSets.testLocal.runtimeClasspath
}

check.dependsOn localTest

Running simple JUnit tests on Android Studio (IntelliJ) when using a G...

android junit intellij-idea gradle android-studio
Rectangle 27 83

I have found Embedded MongoDB library which looks quite promising and does what you have asked for.

Currently supports MongoDB versions: 1.6.5 to 3.1.6, provided the binaries are still available from the configured mirror.

Here is short example of use, which I have just tried and it works perfectly:

public class EmbeddedMongoTest {
    private static final String DATABASE_NAME = "embedded";

    private MongodExecutable mongodExe;
    private MongodProcess mongod;
    private Mongo mongo;

    @Before
    public void beforeEach() throws Exception {
        MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
        mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, 12345, Network.localhostIsIPv6()));
        mongod = mongodExe.start();
        mongo = new Mongo("localhost", 12345);
    }

    @After
    public void afterEach() throws Exception {
        if (this.mongod != null) {
            this.mongod.stop();
            this.mongodExe.stop();
        }
    }

    @Test
    public void shouldCreateNewObjectInEmbeddedMongoDb() {
        // given
        DB db = mongo.getDB(DATABASE_NAME);
        DBCollection col = db.createCollection("testCollection", new BasicDBObject());

        // when
        col.save(new BasicDBObject("testDoc", new Date()));

        // then
        assertThat(col.getCount(), Matchers.is(1L));
    }
}

Just used this library and it worked perfectly JUnit testing a Mongo API on a Mac. Recommended.

+1 excellent find! When I first started using mongodb a year ago, not having a programmatic way to test against a database was one of the downsides. We got around this by having a test instance on every environment, configured through a Java properties file but of course that needed to have mongo installed on every environment. This looks like it will solve all that.

Nice! deleted my answer since it's no longer accurate. Anyone any idea how mature this is? I can imagine it having to simulate MongoDB on a very low level would be quite complicated and judging from the source it looks pretty high level.

Finally got to play with this in my project and can report that is was incredibly easy to set-up and run. The low-level calls are all part of the official com.mongodb Java API so it is no more complicated than using the regular API.

Be careful with this solution. It just gathers information about the current operating system and downloads the appropriate platform-specific MongoDB binaries from the internet, runs the daemon and does some other configuration stuff. As an enterprise solution, this is not. Mocking may be the only real option.

java - Embedded MongoDB when running integration tests - Stack Overflo...

java mongodb junit integration-testing embedded-database
Rectangle 27 49

The problem was that the wrong hamcrest.Matcher, not hamcrest.MatcherAssert, class was being used. That was being pulled in from a junit-4.8 dependency one of my dependencies was specifying.

To see what dependencies (and versions) are included from what source while testing, run:

mvn dependency:tree -Dscope=test

I had the same issue. I was using JUnit-dep and Hamcrest-core but I had Powermock listed earlier in the pom which was resulting in JUnit being included before JUnit-dep and Hamcrest.

Also mockito-all includes some Hamcrest classes. It's better to use mockito-core and exclude the hamcrest dependency.

Just stumbled upon the exact same problem. Solution was upping junit version to 4.11 which is compatible (i.e. "contains classes from") with hamcrest 1.3

java - Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismat...

java junit intellij-idea junit4 hamcrest
Rectangle 27 48

The problem was that the wrong hamcrest.Matcher, not hamcrest.MatcherAssert, class was being used. That was being pulled in from a junit-4.8 dependency one of my dependencies was specifying.

To see what dependencies (and versions) are included from what source while testing, run:

mvn dependency:tree -Dscope=test

I had the same issue. I was using JUnit-dep and Hamcrest-core but I had Powermock listed earlier in the pom which was resulting in JUnit being included before JUnit-dep and Hamcrest.

Also mockito-all includes some Hamcrest classes. It's better to use mockito-core and exclude the hamcrest dependency.

Just stumbled upon the exact same problem. Solution was upping junit version to 4.11 which is compatible (i.e. "contains classes from") with hamcrest 1.3

java - Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismat...

java junit intellij-idea junit4 hamcrest
Rectangle 27 1

This is a result of how Java dependencies work. When running JUnit tests the src/test/java directory is used as the project to be run, src/main/java is treated as a dependency. When java is trying to resolve an import path it has no idea of knowing if the path is from an internal file or a dependency. Therefore it will first try to resolve it within the project and then look at the dependencies. If it finds a match in the project it will not look any further. In the same way, when running the 'production' code src/main/java is used as the main project with no dependency to src/test/java, therefore it will always get the original implementation of MyObject.

Overwriting classes in this manner is technically safe but not a good practice. It would make it unclear if the original implementation is used.

If you want to keep most of the logic of MyObject but override some of it you should create a stub class in src/test/java, ie. MyObjectStub.

Another way of replacing logic logic for testing is to use a mocking library such as Mockito.

Thank you, exactly the answer i was looking for.A small followup question-is the src/test directory the first class lookup even for the project dependencies(i.e,if MyObject was not in the same project/dependency as the test class, but in a parallel project that also have src/MyObject and test/MyObject). Will it always look first in test folders and only then go to the src one, regardless of 'distance' to the original project?). In other words-will unit test runs always look for src/test first, and 'production' runs always look for 'src/main'-regardless of them being in any jar/dependency path?

src/test will not be seen from any other project so there is no risk of those classes/resources being used in the production code. They will also never be seen if an external project has a dependency to your project. As long as your unit tests are located in src/test(which they should be) they will always prioritize resources from src/test.

Overriding src clases with test classes for java unit testing - Stack ...

java unit-testing junit
Rectangle 27 1

Despite the fact that this is a very old question and probably many of the beforementioned ideas solved many problems, I still want to share the solution with the community that fixed my problem.

I found that the problem was a function called "hasItem" which I was using to check whether or not a JSON-Array contains a specific item. In my case I checked for a value of type Long.

Somehow, the Matchers have problems with values of type Long. (I do not use JUnit or Rest-Assured so much so idk. exactly why, but I guess that the returned JSON-data does just contain Integers.)

So what I did to actually fix the problem was the following. Instead of using:

long ID = ...;

...
.then().assertThat()
  .body("myArray", hasItem((int) ID));

That's probably not the best solution, but I just wanted to mention that the exception can also be thrown because of wrong/unknown data types.

java - Getting "NoSuchMethodError: org.hamcrest.Matcher.describeMismat...

java junit intellij-idea junit4 hamcrest
Rectangle 27 1

Unit test has to be Unit. If you are trying to make some sort of unit tests which are dependent on each other then its a bad idea.

Although you can use @BeforeClass or @AfterClass Annotation for setting up your configuration for test or cleanup code respectively.

java - How to make a Junit test class run last when running test packa...

java unit-testing junit4
Rectangle 27 0

I was also facing a similar issue ("no runnable methods..") on running the simplest of simple piece of code (Using @Test, @Before etc.) and found the solution nowhere. I was using Junit4 and Eclipse SDK version 4.1.2. Resolved my problem by using the latest Eclipse SDK 4.2.2. I hope this helps people who are struggling with a somewhat similar issue.

java - JUnit: how to avoid "no runnable methods" in test utils classes...

java ant junit testing
Rectangle 27 0

We plan to add it in IDEA 10, but the priority would depend on the number of votes.

Can you offer a source for this?

IDEA 10 will be released by the end of 2010.

java - Running JUnit Tests in Parallel in IntelliJ IDEA - Stack Overfl...

java unit-testing junit intellij-idea
Rectangle 27 0

You have two options:

Another option using maven-android-plugin https://code.google.com/p/maven-android-plugin/ and use appropriate dependencies. I prefer second.

java - Problems with JUnit testing in IntelliJ IDEA - Class not found ...

java android junit intellij-idea
Rectangle 27 0

can't run only a specific test in the class

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA. User: nir Date: 1/30/13 Time: 4:46 PM To change
 * this template use File | Settings | File Templates.
 */
public class AutoTestNGShellRunner {
    static Class<?> autotestClass;



    public static void main(final String[] args) {
        int rc;
        if (args.length != 2) {
            System.err
                    .println("Usage: AutoTestNGShellRunner  <TEST_SCENARIO_CLASS_NAME> <TEST_CASE>");
            System.exit(-1);
        }

        final String testsuite = args[0];
        final String testcase = args[1];

        try {
            autotestClass = Class.forName(testsuite);

        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("class" + testsuite + " is not found ",
                    e);
        }

        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { autotestClass });
        testng.setTestNames(Arrays.asList(testcase));
        testng.addListener(tla);

        testng.run();
        tla.getPassedTests();
        List<ITestResult> failedTestsResults = tla.getFailedTests();
        List<ITestResult> successTestsResults = tla.getPassedTests();

        final int failureCount = failedTestsResults.size();

        if (failureCount == 0) {
            System.out
                    .println(String.format("Test case %s passed", testcase));
            rc = 0;
        } else {
            System.out
                    .println(String.format("Test case %s failed", testcase));
            rc = -1;
        }

        System.exit(rc);
    }
}

it seems like when you pass the class name it runs all the TestNG methods inside the class, is there any way to avoid it?

java - Change JUnitCore ( JUnit ) to TestNG - Stack Overflow

java junit testng
Rectangle 27 0

The answer is to create a test suite that contains only those tests underneath the unit test folder and run that instead. There is a junit-addon which does just this called DirectorySuiteBuilder but I only found this after I had pretty much re-invented the wheel.

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;

import java.io.File;
import java.io.IOException;

public class DirectoryTestSuite {
    static final String rootPath = "proj\\src\\test\\java\\";
    static final ClassLoader classLoader = DirectoryTestSuite.class.getClassLoader();

    public static TestSuite suite() throws IOException, ClassNotFoundException {
	final TestSuite testSuite = new TestSuite();
	findTests(testSuite, new File(rootPath));
	return testSuite;
    }

    private static void findTests(final TestSuite testSuite, final File folder) throws IOException, ClassNotFoundException {
	for (final String fileName : folder.list()) {
	    final File file = new File( folder.getPath() + "/" +fileName);
	    if (file.isDirectory()) {
		findTests(testSuite, file);
	    } else if (isTest(file)) {
		addTest(testSuite, file);
	    }
	}
    }

    private static boolean isTest(final File f) {
	return f.isFile() && f.getName().endsWith("Test.java");
    }

    private static void addTest(final TestSuite testSuite, final File f) throws ClassNotFoundException {
	final String className = makeClassName(f);
	final Class testClass = makeClass(className);
	testSuite.addTest(new JUnit4TestAdapter(testClass));
    }

    private static Class makeClass(final String className) throws ClassNotFoundException {
	return (classLoader.loadClass(className));
    }

    private static String makeClassName(final File f) {
	return f.getPath().replace(rootPath, "").replace("\\", ".").replace(".java", "");
    }
}

java - Run all tests in a source tree, not a package - Stack Overflow

java unit-testing junit intellij-idea
Rectangle 27 0

Sven's link (http://youtrack.jetbrains.com/issue/SCL-5152) led me to the answer to compile / run unit tests in Intellij 12 with Play framework 2.1.1 [sorry, don't have Play 1.x installed anymore].

From the menu bar [OSX -- likely similar for other platforms] navigate to: IntelliJ -> Preferences -> Compiler

Uncheck the "use external build" and voila, my unit tests were able to run.

java - Play Framework 2 running JUnit tests in Intellij IDEA - Stack O...

java junit intellij-idea playframework-2.0
Rectangle 27 0

The only reasonable way that I've found to write unit tests for classes that don't depend on the Android libraries (Activities, Context, etc.) is to install Robolectric.

Then, since Robolectric is based on JUnit, in addition to Robolectric tests, which do typically involve Context and friends, you have the infrastructure in place to support simple, non-Android JUnit tests.

Can't you create functions to test whether certain data can be accepted utilising(extending) TestCases. Or have I got the wrong end of the stick?

java - How can I JUnit test a model within Android? - Stack Overflow

java android unit-testing android-activity
Rectangle 27 0

Earlier in this case I always do mvn eclipse:eclipse and restart my Eclipse and it worked. After migrating to GIT it stop working for me which is somewhat wired.

Basic problem here is Mr Eclipse does not found the compiled class. Then I set the output folder as Project/target/test-classes which is by default generated by mvn clean install without skipping the test and proceed with following workaround

Option 2: Create classpath variable and include it in classpath for all the test cases

Eclipse ->Windows ->Classpath Variables ->New->[Name : Junit_test_cases_cp | path : ]->ok Then go to Eclipse->Run ->Run Configurations ->JUnit->select mytest ->under classpath tab ->Select User Entries->Advanced->Add classpath variables->Select Junit_test_cases_cp->ok->Apply->Run

This is the only thing currently working for me after trying all the suggestions on-line.

java - Class Not Found Exception when running Junit test - Stack Overf...

java junit
Rectangle 27 0

  • Delete the content of local Maven repository.
  • run mvn clean install in the command line. (cd to the pom directory).
  • Build Project in Eclipse.

java - Class Not Found Exception when running Junit test - Stack Overf...

java junit
Rectangle 27 0

Might be you forgotten to place the Main class and Test Case class in /src/test/java. Check it Once.

java - Class Not Found Exception when running Junit test - Stack Overf...

java junit
Rectangle 27 0

Earlier, in this case, I always did mvn eclipse:eclipse and restarted my Eclipse and it worked. After migrating to GIT, it stopped working for me which is somewhat weird.

Basic problem here is Mr Eclipse does not find the compiled class. Then, I set the output folder as Project/target/test-classes which is by default generated by mvn clean install without skipping the test and proceeded with following workaround:

Option 2: Create classpath variable and include it in classpath for all the test cases

Eclipse ->Windows ->Classpath Variables ->New->[Name : Junit_test_cases_cp | path : ]->ok Then go to Eclipse->Run ->Run Configurations ->JUnit->select mytest ->under classpath tab ->Select User Entries->Advanced->Add classpath variables->Select Junit_test_cases_cp->ok->Apply->Run

This is the only thing currently working for me after trying all the suggestions online.

java - Class Not Found Exception when running JUnit test - Stack Overf...

java eclipse junit
Rectangle 27 0

It can also be due to "[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?"

java - Class Not Found Exception when running JUnit test - Stack Overf...

java eclipse junit
Rectangle 27 0

I found the solution to my problem - it may help others ;) When I was increasing the heap size I was increasing the heap size of eclipse application, not of my program (which I executed through eclipse) What I had to do is modify the execution commands before running my program.

java - Why does heap space run out only when running JUnit tests? - St...

java eclipse junit out-of-memory