Issue
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.
Solution
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'
}
}
}
Answered By — wogsland
I am following a little test script and providing it with the first piece of code to make it green. The code is java and the testing is gradle with java. Java is version «1.8.0_60» on Mac OSX «El Capitan». gradle
is version 2.8.
After using gradle build
, here is the error shown:
$ gradle build
:compileJava
/Users/rsalazar/exercism/java/etl/src/main/java/Etl.java:1: error: package com.google.common.collect does not exist
import com.google.common.collect.ImmutableMap;
^
/Users/rsalazar/exercism/java/etl/src/main/java/Etl.java:9: error: cannot find symbol
return ImmutableMap.of("a", 1);
^
symbol: variable ImmutableMap
location: class Etl
2 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.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.597 secs
Here is the build.gradle
file:
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"
repositories {
mavenCentral()
}
dependencies {
testCompile "junit:junit:4.10"
testCompile "org.easytesting:fest-assert-core:2.0M10"
testCompile "com.google.guava:guava:16+"
}
Here is the test: (EtlTest.java
)
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.fest.assertions.api.Assertions.assertThat;
public class EtlTest {
private final Etl etl = new Etl();
@Test
public void testTransformOneValue() {
Map<Integer, List<String>> old = ImmutableMap.of(1, Arrays.asList("A"));
Map<String, Integer> expected = ImmutableMap.of("a", 1);
assertThat(etl.transform(old)).isEqualTo(expected);
}
}
Here is the code under test: (Etl.java
)
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Etl {
public Map<String, Integer> transform(Map <Integer, List<String>> from) {
return ImmutableMap.of("a", 1);
}
}
I am not looking for help on making the test pass. Just help on making the testing compile with gradle. I am sorry to say I am stuck when compiling with the error message provided. I couldn’t find any help on the net. Thanks much!
Я пытаюсь построить проект Android
с Gradle
из командной строки, но обнаружил проблему, когда хочу изменить структуру каталогов.
В настоящее время выглядит так:
.
└── main
├── AndroidManifest.xml
├── ic_launcher-web.png
├── java
│ └── com
│ └── myproject
│ └── MainActivity.java
└── res
├── ...
├── layout
│ ├── activity_main.xml
│ └── fragment_main.xml
├── ...
...
Затем я выполняю:
./gradlew clean build
Это заканчивается на:
BUILD SUCCESSFUL
Ok. Все в порядке. Но теперь я хочу создать новый каталог, поэтому:
Я создаю каталог ui
и передвигаю MainActivity.java
там:
.
└── main
├── AndroidManifest.xml
├── ic_launcher-web.png
├── java
│ └── com
│ └── myproject
│ └── ui
│ └── MainActivity.java
└── res
├── ...
├── layout
│ ├── activity_main.xml
│ └── fragment_main.xml
├── ...
...
Измените свой пакет:
package com.myproject.ui;
// imports
public class MainActivity extends ActionBarActivity {
...
}
Измените его атрибут android:name
в AndroidManifest.xml
:
<activity
android:name=".ui.MainActivity"
android:label="@string/app_name" >
...
</activity>
И попробуйте снова скомпилировать его:
./gradlew clean build
Со следующими ошибками:
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:19: error: package R does not exist
setContentView(R.layout.activity_main);
^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:23: error: package R does not exist
.add(R.id.container, new PlaceholderFragment())
^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:33: error: package R does not exist
getMenuInflater().inflate(R.menu.main, menu);
^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:43: error: package R does not exist
if (id == R.id.action_settings) {
^
/home/birei/MyDummyProject/MyProject/src/main/java/com/myproject/ui/MainActivity.java:60: error: package R does not exist
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
BUILD FAILED
Что я делаю неправильно? Любые идеи?
Спасибо.