trocchietto
trocchietto

Reputation: 2617

Junit 4 does not recognize test

I cloned a repository and tried to run a test. Unfortunately, JUnit 4 does not recognize the directory that I added manually

The test GUI says after tries to load the tests:

No tests were found.

Tests ran to completion.

Empty test suite.

This is my directory with ExampleUnitTest that i copypasted by another project (this is a default file that is not questionable that does not work)

enter image description here

And this is my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        testApplicationId "com.commonsware.android.retrofit.test"
    }
}

dependencies {
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'de.greenrobot:eventbus:2.2.1'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
}

EDIT as requested here the content of ExampleUnitTest and Run/Debug Configurations.

package com.commonsware.android.retrofit;

/**
 * To work on unit tests, switch the Test Artifact in the Build Variants view.
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }
}

Run/Debug Configurations enter image description here

Upvotes: 3

Views: 2330

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83527

If you are creating a Run Configuration manually in the "Edit Configurations" dialog, you should select "JUnit" for local tests and "Android Tests" only for instrumented tests. The easiest way to do this for a single test class or method is to click any of the run icons on the left in editor window. You can also right-click on any class or package in the Project view then select "Run 'Tests in ...". Alternatively you can left-click on a class or package and push Ctrl+Shift+F10.

Upvotes: 1

Leszek Jasek
Leszek Jasek

Reputation: 2346

First, ensure Android Studio don't see any test in Run/Debug Configurations menu. To do that, click on Edit Configurations... option and then check JUnit section on the left.

enter image description here

If there are no tests, click enter image description here, select JUnit, select app module and ExampleUnitTest class. Don't forget to apply changes and then try to run test again.

enter image description here

If this does not help, please try switching from Release to Debug build variant and ensure there is no stacktrace of the exception in Android Monitor tab.

Upvotes: 3

Related Questions