Reputation: 2071
I have a test that perfectly works in Eclipse but fails in Gradle. I am not sure what is wrong. I am using Java 8 in Eclipse.
prompt:
E:\Files\Source\Workspace-Eclipse2\project\src\test\java\com\project\core\domain\TeamUnitTest.java:31: error: no sui
table method found for assertThat(List<User>,Matcher<Collection<Object>>)
assertThat(team.getUsers(), empty());
^
method Assert.<T#1>assertThat(T#1,Matcher<? super T#1>) is not applicable
(actual argument Matcher<Collection<Object>> cannot be converted to Matcher<? super List<User>> by method invocati
on conversion)
method Assert.<T#2>assertThat(String,T#2,Matcher<? super T#2>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>assertThat(T#1,Matcher<? super T#1>)
T#2 extends Object declared in method <T#2>assertThat(String,T#2,Matcher<? super T#2>)
E:\Files\Source\Workspace-Eclipse2\project\src\test\java\com\project\core\domain\TeamUnitTest.java:51: error: no sui
table method found for assertThat(List<User>,Matcher<Collection<Object>>)
assertThat(team.getUsers(), empty());
^
method Assert.<T#1>assertThat(T#1,Matcher<? super T#1>) is not applicable
(actual argument Matcher<Collection<Object>> cannot be converted to Matcher<? super List<User>> by method invocati
on conversion)
method Assert.<T#2>assertThat(String,T#2,Matcher<? super T#2>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>assertThat(T#1,Matcher<? super T#1>)
T#2 extends Object declared in method <T#2>assertThat(String,T#2,Matcher<? super T#2>)
2 errors
:compileTestJava FAILED
test code:
assertThat(team.getUsers(), empty());
Upvotes: 3
Views: 3547
Reputation: 48326
It would seem that your gradle build isn't pulling in the same junit or hamcrest that Eclipse is, but instead an older one. You should set explicit dependencies.
If you put this class into src/test/java/SOTest.java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import java.util.Collections;
import java.util.List;
public class SOTest {
@Test
public void doTest() throws Exception {
// Regular JUnit assert
assertEquals(true, true);
// Hamcrest assertThat
List<Object> teamUsers = Collections.emptyList();
assertThat(teamUsers, empty());
}
}
Then create a build.gradle
file with this in:
apply plugin: 'java'
sourceCompatibility = 1.6
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
}
And build, the test of assertThat(Collection,empty())
will run and pass.
The difference here is we're explicitly depending on a new version of junit, and on the hamcrest 1.3 version with those matches in it.
Upvotes: 3