简体   繁体   中英

Junit testing in Android SDK

When I try to test a simple class using Junit testing in the recent android studio, I am getting 2 errors

Error:Gradle: Failure:Build failed with an exception *What went wrong: Execution failed for task ':Name:CompileDebug'

Compilation failed

Error:Could not execute build using Gradle distribution.

package com.example.name;

public class MyClass {

    public String sayHello() {
        return "Hello";
    }

    public String sayGoodbye() {
        return "Goodbye!";
    }


} 

My TestCode is:

package com.example.name;

import android.test.AndroidTestCase;
import org.junit.Before;
import org.junit.Test;

public class MyClassTest extends AndroidTestCase {
    private MyClass myClass;

    @Before
    public void setUp() throws Exception {
        myClass = new MyClass();
    }

    @Test
    public void testSayHello() throws Exception {
        assertEquals("Hello", myClass.sayHello());
    }
}

Please help me with these errors. Thanks in advance

I got the same problem. I solved it by following this guideline:

This could be either one of two problems:

1) you have only a JRE installed on your system. Gradle needs a JDK (JRE doesn't have the javac compiler, which gradle needs). 2) you have both a JRE and a JDK installed, but gradle doesn't find the JDK because it only looks in the 'standard' location where it just finds the JRE.

Depending on what you have installed on your system you may be able to decide which of the two cases you are in.

If you are in case 1) you will need to install a JDK. (Then you might end up in case 2)

If you are in case 2) please take a look at this issue: https://issuetracker.springsource.com/browse/STS-2276

In short, only recently has it become possible to tell Gradle from within STS which JAVA_HOME to use. You will need to install a nightly snapshot of the gradle-sts tools to get this option. Or you can try some of the workarounds suggested in the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM