简体   繁体   English

Android SDK中的Junit测试

[英]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 当我尝试在最近的android studio中使用Junit测试来测试一个简单的类时,出现2个错误

Error:Gradle: Failure:Build failed with an exception *What went wrong: Execution failed for task ':Name:CompileDebug' 错误:等级:失败:构建失败,并带有异常*出了什么问题:任务':Name:CompileDebug'的执行失败

Compilation failed 编译失败

Error:Could not execute build using Gradle distribution. 错误:无法使用Gradle分发执行构建。

package com.example.name;

public class MyClass {

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

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


} 

My TestCode is: 我的TestCode是:

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. 1)您的系统上仅安装了一个JRE。 Gradle needs a JDK (JRE doesn't have the javac compiler, which gradle needs). Gradle需要一个JDK(JRE没有gradle需要的javac编译器)。 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. 2)您同时安装了JRE和JDK,但是gradle找不到JDK,因为它仅在“标准”位置查找,只能找到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. 如果情况是1),则需要安装JDK。 (Then you might end up in case 2) (然后您可能会遇到情况2)

If you are in case 2) please take a look at this issue: https://issuetracker.springsource.com/browse/STS-2276 如果您遇到第2种情况,请查看以下问题: 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. 简而言之,只有最近才有可能从STS内告诉Gradle使用哪个JAVA_HOME。 You will need to install a nightly snapshot of the gradle-sts tools to get this option. 您将需要安装gradle-sts工具的夜间快照才能获得此选项。 Or you can try some of the workarounds suggested in the issue. 或者,您可以尝试问题中建议的一些解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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