2017-03-08 22 views
8

Próbuję zaimplementować prosty test junit użyciem Gradle i działa w następującej kwestii gdy biegnę gradle test:Gradle compileJava błąd: pakiet org.junit nie istnieje

: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. 

Więc mam to build.gradle file:

apply plugin: 'java' 

dependencies { 
    testCompile 'junit:junit:4.12' 
} 

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

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

Ale nie mogę zrozumieć, dlaczego nie znajduje się razem, gdy mam ją w zależnościach.

Odpowiedz

14

Więc widocznie musiałem dodać zależność compile a następnie również zadeklarować repositories. Mój nowy build.gradle, który z powodzeniem wykonuje test:

apply plugin: 'java' 

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

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

Zobacz https://issuetracker.google.com/issues/37098629: jeśli zmieniłeś 'sourceSets', gradle jest zepsuty dla testów jednostkowych i musisz skompilować junit do produkcyjnego pliku APK. – WillC

0

spróbuj dodać

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

bezpośrednio pod swoim buildscript {w build.gradle

+0

A więc brakuje "buildscriptu"? – wogsland

+0

Powyższy 'build.gradle' jest dosłownie wszystkim, co mam. – wogsland

Powiązane problemy