ошибка gradle compileJava: пакет org.junit не существует

Я пытаюсь реализовать простой тест junit с помощью gradle и столкнулся со следующей проблемой при запуске 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.

Я :

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);
  }
}

но я не могу понять, почему он не находит junit, когда я включил его в зависимости.

2 ответов


поэтому, по-видимому, мне нужно было добавить compile зависимость, а затем и объявить repositories. Мой новый build.gradle который успешно выполняет тест:

apply plugin: 'java'

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

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

попробуйте добавить

 repositories {
    maven { url 'http://repo1.maven.org/maven2' }

непосредственно под вашим buildscript{ в сборке.Gradle в