gradle compileJava错误:软件包org.junit不存在 [英] gradle compileJava error: package org.junit does not exist

查看:195
本文介绍了gradle compileJava错误:软件包org.junit不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用gradle实现一个简单的junit测试,并运行 gradle test 时遇到以下问题:

 :compileJava 
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1:错误:程序包org.junit不存在
import static org.junit.Assert.assertEquals;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1:错误:静态仅从类和接口导入
导入静态org.junit.Assert.assertEquals ;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:2:error:package org.junit does not exist
import org.junit.Test;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:5:错误:找不到符号
@Test
^
符号: class Test
location:class CalculatorTest
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:9:错误:找不到符号
assertEquals(6,sum);
$
符号:方法assertEquals(int,int)
位置:类CalculatorTest
5错误
:compileJava失败

失败:构建例外失败。

*出错:
任务'compileJava'的执行失败。
编译失败;详细信息请参阅编译器错误输出。

所以我得到了这个 build.gradle file:

  apply plugin:'java'

dependencies {
testCompile'junit :junit:4.12'
}

sourceSets {
main {
java {
srcDir'src'
}
}

CalculatorTest.java

  import static org.junit.Assert.assertEquals; 
import org.junit.Test;

public class CalculatorTest {
@Test
public void evaluatesExpression(){
Calculator calculator = new Calculator();
int sum = calculator.evaluate(1 + 2 + 3);
assertEquals(6,sum);


$ / code $ / pre

但我找不出为什么找不到junit当我将它包含在依赖关系中。

解决方案

显然,我需要添加一个编译依赖项,然后声明 repositories 。我的新版 build.gradle 成功运行测试:

  apply plugin :'java'

repositories {jcenter()}
dependencies {
testCompile'junit:junit:4.12'
compile'junit:junit:4.12'


sourceSets {
main {
java {
srcDir'src'
}
}
}


I'm trying to implement a simple junit test using gradle and running into the following issue when I run gradle test:

:compileJava
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
                       ^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:2: error: package org.junit does not exist
import org.junit.Test;
                ^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:5: error: cannot find symbol
  @Test
   ^
  symbol:   class Test
  location: class CalculatorTest
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:9: error: cannot find symbol
    assertEquals(6, sum);
    ^
  symbol:   method assertEquals(int,int)
  location: class CalculatorTest
5 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details.

So I've got this build.gradle file:

apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.12'
}

sourceSets {
  main {
    java {
      srcDir 'src'
    }
  }
}

And CalculatorTest.java:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression() {
    Calculator calculator = new Calculator();
    int sum = calculator.evaluate("1+2+3");
    assertEquals(6, sum);
  }
}

But I can't figure out why it's not finding junit when I've got it included in the dependencies.

解决方案

So apparently I needed to add a compile dependency and then also declare repositories. My new build.gradle that successfully runs the test:

apply plugin: 'java'

repositories { jcenter() }
dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

这篇关于gradle compileJava错误:软件包org.junit不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆