Skip to content

Commit

Permalink
Use Mockito instead of EasyMock
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jan 14, 2018
1 parent 1322d90 commit c450df4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 79 deletions.
11 changes: 5 additions & 6 deletions javaparser-symbol-solver-testing/pom.xml
Expand Up @@ -92,12 +92,11 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-model</artifactId>
Expand Down
Expand Up @@ -27,13 +27,14 @@
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.google.common.collect.ImmutableList;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Federico Tomassetti
Expand All @@ -53,9 +54,9 @@ public void setup() {
string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
object = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
listOfString = listOf(string);
tpE = EasyMock.createMock(ResolvedTypeParameterDeclaration.class);
EasyMock.expect(tpE.getName()).andReturn("T").anyTimes();
EasyMock.replay(tpE);
tpE = mock(ResolvedTypeParameterDeclaration.class);
when(tpE.getName()).thenReturn("T");

listOfE = listOf(new ResolvedTypeVariable(tpE));
}

Expand Down
Expand Up @@ -45,8 +45,9 @@
import java.io.InputStream;
import java.util.Collections;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ContextTest extends AbstractTest {

Expand Down Expand Up @@ -111,22 +112,20 @@ public void resolveReferenceToImportedType() throws ParseException {
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(0);

ResolvedClassDeclaration compilationUnitDecl = createMock(ResolvedClassDeclaration.class);
expect(compilationUnitDecl.getName()).andReturn("CompilationUnit");
expect(compilationUnitDecl.getQualifiedName()).andReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = createMock(TypeSolver.class);
expect(typeSolver.getRoot()).andReturn(typeSolver);
expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
expect(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).andReturn(SymbolReference.solved(compilationUnitDecl));
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
replay(typeSolver, compilationUnitDecl);

SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param);

assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());

verify(typeSolver, compilationUnitDecl);
}

@Test
Expand All @@ -136,23 +135,21 @@ public void resolveReferenceUsingQualifiedName() throws ParseException {
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(0);

ResolvedClassDeclaration compilationUnitDecl = createMock(ResolvedClassDeclaration.class);
expect(compilationUnitDecl.getName()).andReturn("CompilationUnit");
expect(compilationUnitDecl.getQualifiedName()).andReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = createMock(TypeSolver.class);
//expect(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).andReturn(SymbolReference.unsolved(ClassDeclaration.class));
expect(typeSolver.getRoot()).andReturn(typeSolver);
expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
expect(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).andReturn(SymbolReference.solved(compilationUnitDecl));
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
//when(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.unsolved(ClassDeclaration.class));
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
replay(typeSolver, compilationUnitDecl);

SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("com.github.javaparser.ast.CompilationUnit", param);

assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());

verify(typeSolver, compilationUnitDecl);
}

@Test
Expand All @@ -162,22 +159,20 @@ public void resolveReferenceToClassesInTheSamePackage() throws ParseException {
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(0);

ResolvedClassDeclaration compilationUnitDecl = createMock(ResolvedClassDeclaration.class);
expect(compilationUnitDecl.getName()).andReturn("CompilationUnit");
expect(compilationUnitDecl.getQualifiedName()).andReturn("my.packagez.CompilationUnit");
TypeSolver typeSolver = createMock(TypeSolver.class);
expect(typeSolver.getRoot()).andReturn(typeSolver);
expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
expect(typeSolver.tryToSolveType("my.packagez.CompilationUnit")).andReturn(SymbolReference.solved(compilationUnitDecl));
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("my.packagez.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("my.packagez.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
replay(typeSolver, compilationUnitDecl);

SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param);

assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("my.packagez.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());

verify(typeSolver, compilationUnitDecl);
}

@Test
Expand All @@ -187,23 +182,21 @@ public void resolveReferenceToClassInJavaLang() throws ParseException {
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(1);

ResolvedClassDeclaration stringDecl = createMock(ResolvedClassDeclaration.class);
expect(stringDecl.getName()).andReturn("String");
expect(stringDecl.getQualifiedName()).andReturn("java.lang.String");
TypeSolver typeSolver = createMock(TypeSolver.class);
expect(typeSolver.tryToSolveType("me.tomassetti.symbolsolver.javaparser.String")).andReturn(SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class));
expect(typeSolver.getRoot()).andReturn(typeSolver);
expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
expect(typeSolver.tryToSolveType("java.lang.String")).andReturn(SymbolReference.solved(stringDecl));
ResolvedClassDeclaration stringDecl = mock(ResolvedClassDeclaration.class);
when(stringDecl.getName()).thenReturn("String");
when(stringDecl.getQualifiedName()).thenReturn("java.lang.String");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.tryToSolveType("me.tomassetti.symbolsolver.javaparser.String")).thenReturn(SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class));
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("java.lang.String")).thenReturn(SymbolReference.solved(stringDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
replay(typeSolver, stringDecl);

SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("String", param);

assertEquals(true, ref.isSolved());
assertEquals("String", ref.getCorrespondingDeclaration().getName());
assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getQualifiedName());

verify(typeSolver, stringDecl);
}

@Test
Expand Down Expand Up @@ -242,7 +235,7 @@ public void resolveCascadeOfReferencesToMethod() throws ParseException, IOExcept
}

@Test
public void resolveReferenceToMethodCalledOnArrayAccess() throws ParseException, IOException {
public void resolveReferenceToMethodCalledOnArrayAccess() throws ParseException {
CompilationUnit cu = parseSample("ArrayAccess");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ArrayAccess");
MethodDeclaration method = Navigator.demandMethod(clazz, "access");
Expand All @@ -258,7 +251,7 @@ public void resolveReferenceToMethodCalledOnArrayAccess() throws ParseException,
}

@Test
public void resolveReferenceToJreType() throws ParseException, IOException {
public void resolveReferenceToJreType() throws ParseException {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
Expand All @@ -271,7 +264,7 @@ public void resolveReferenceToJreType() throws ParseException, IOException {
}

@Test
public void resolveReferenceToMethodWithLambda() throws ParseException, IOException {
public void resolveReferenceToMethodWithLambda() throws ParseException {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
Expand All @@ -286,7 +279,7 @@ public void resolveReferenceToMethodWithLambda() throws ParseException, IOExcept
}

@Test
public void resolveReferenceToLambdaParamBase() throws ParseException, IOException {
public void resolveReferenceToLambdaParamBase() throws ParseException {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
Expand All @@ -300,7 +293,7 @@ public void resolveReferenceToLambdaParamBase() throws ParseException, IOExcepti
}

@Test
public void resolveReferenceToLambdaParamSimplified() throws ParseException, IOException {
public void resolveReferenceToLambdaParamSimplified() throws ParseException {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
Expand Down Expand Up @@ -421,7 +414,7 @@ public void resolveReferenceToCallOnLambdaParam() throws ParseException, IOExcep
}

@Test
public void resolveReferenceToOverloadMethodWithNullParam() throws ParseException, IOException {
public void resolveReferenceToOverloadMethodWithNullParam() throws ParseException {
CompilationUnit cu = parseSample("OverloadedMethods");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
MethodDeclaration method = Navigator.demandMethod(clazz, "m1");
Expand All @@ -436,7 +429,7 @@ public void resolveReferenceToOverloadMethodWithNullParam() throws ParseExceptio
}

@Test
public void resolveReferenceToOverloadMethodFindStricter() throws ParseException, IOException {
public void resolveReferenceToOverloadMethodFindStricter() throws ParseException {
CompilationUnit cu = parseSample("OverloadedMethods");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
MethodDeclaration method = Navigator.demandMethod(clazz, "m2");
Expand Down Expand Up @@ -465,7 +458,7 @@ public void resolveInheritedMethodFromInterface() throws ParseException {
}

@Test
public void resolveReferenceToOverloadMethodFindOnlyCompatible() throws ParseException, IOException {
public void resolveReferenceToOverloadMethodFindOnlyCompatible() throws ParseException {
CompilationUnit cu = parseSample("OverloadedMethods");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
MethodDeclaration method = Navigator.demandMethod(clazz, "m3");
Expand Down
Expand Up @@ -36,7 +36,6 @@
import com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.google.common.collect.ImmutableList;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -45,6 +44,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Federico Tomassetti
Expand Down Expand Up @@ -167,11 +168,10 @@ public void solveTypeInSamePackage() throws ParseException {
CompilationUnit cu = parseSample("CompilationUnitWithImports");
Context context = new CompilationUnitContext(cu, typeSolver);

ResolvedReferenceTypeDeclaration otherClass = EasyMock.createMock(ResolvedReferenceTypeDeclaration.class);
EasyMock.expect(otherClass.getQualifiedName()).andReturn("com.foo.OtherClassInSamePackage");
ResolvedReferenceTypeDeclaration otherClass = mock(ResolvedReferenceTypeDeclaration.class);
when(otherClass.getQualifiedName()).thenReturn("com.foo.OtherClassInSamePackage");
MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
memoryTypeSolver.addDeclaration("com.foo.OtherClassInSamePackage", otherClass);
EasyMock.replay(otherClass);

SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("OtherClassInSamePackage", memoryTypeSolver);
assertEquals(true, ref.isSolved());
Expand Down
Expand Up @@ -8,7 +8,6 @@
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.symbolsolver.resolution.typeinference.*;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.easymock.EasyMock;
import org.junit.Test;

import java.util.Arrays;
Expand All @@ -17,6 +16,7 @@

import static com.github.javaparser.symbolsolver.resolution.typeinference.TypeHelper.isProperType;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class SubtypeOfBoundTest {

Expand All @@ -29,7 +29,7 @@ public class SubtypeOfBoundTest {

@Test
public void recognizeProperLowerBound1() {
ResolvedTypeParameterDeclaration typeParameterDeclaration = EasyMock.createMock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration = mock(ResolvedTypeParameterDeclaration.class);

// { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α.

Expand All @@ -41,7 +41,7 @@ public void recognizeProperLowerBound1() {

@Test
public void recognizeProperLowerBound2() {
ResolvedTypeParameterDeclaration typeParameterDeclaration = EasyMock.createMock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration = mock(ResolvedTypeParameterDeclaration.class);

// { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α.

Expand All @@ -53,7 +53,7 @@ public void recognizeProperLowerBound2() {

@Test
public void recognizeProperUpperBound1() {
ResolvedTypeParameterDeclaration typeParameterDeclaration = EasyMock.createMock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration = mock(ResolvedTypeParameterDeclaration.class);

// { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α.

Expand All @@ -65,8 +65,8 @@ public void recognizeProperUpperBound1() {

@Test
public void recognizeProperUpperBound2() {
ResolvedTypeParameterDeclaration typeParameterDeclaration1 = EasyMock.createMock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration2 = EasyMock.createMock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration1 = mock(ResolvedTypeParameterDeclaration.class);
ResolvedTypeParameterDeclaration typeParameterDeclaration2 = mock(ResolvedTypeParameterDeclaration.class);
// { α <: Iterable<?>, β <: Object, α <: List<β> } describes a proper upper bound for each of α and β, along with a dependency between them.

InferenceVariable alpha = new InferenceVariable("α", typeParameterDeclaration1);
Expand Down

0 comments on commit c450df4

Please sign in to comment.