Skip to content

Commit

Permalink
migrate parameterized tests to junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
signed committed Dec 26, 2018
1 parent 10e4d87 commit 625bc06
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 42 deletions.
4 changes: 4 additions & 0 deletions javaparser-symbol-solver-testing/pom.xml
Expand Up @@ -117,6 +117,10 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-logic</artifactId>
Expand Down
Expand Up @@ -10,24 +10,16 @@
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class Issue235 extends AbstractResolutionTest{
private final String method;
class Issue235 extends AbstractResolutionTest{

public Issue235(String method) {
this.method = method;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<String> data() throws Exception {
static Collection<String> data() {
return Arrays.asList(
"new_Bar_Baz_direct",
"new_Bar_Baz",
Expand All @@ -36,15 +28,16 @@ public static Collection<String> data() throws Exception {
);
}

@Test
public void issue235() {
@ParameterizedTest
@MethodSource("data")
void issue235(String method) {
CompilationUnit cu = parseSample("Issue235");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Foo");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration m = Navigator.demandMethod(cls, this.method);
MethodDeclaration m = Navigator.demandMethod(cls, method);
ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(0);
ObjectCreationExpr expression = (ObjectCreationExpr) stmt.getExpression();
Assert.assertNotNull(javaParserFacade.convertToUsage(expression.getType()));
Assertions.assertNotNull(javaParserFacade.convertToUsage(expression.getType()));
}
}
Expand Up @@ -16,29 +16,24 @@

package com.github.javaparser.symbolsolver.resolution.typesolvers;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver.ExceptionHandlers;

@RunWith(Parameterized.class)
public class CombinedTypeSolverTest {
class CombinedTypeSolverTest {

@Parameters
public static List<Object[]> parameters() {
static List<Object[]> parameters() {
// Why these classes? NFE is a subclass, IOOBE is a superclass and ISE is a class without children (by default)
Predicate<Exception> whitelistTestFilter = ExceptionHandlers.getTypeBasedWhitelist(NumberFormatException.class,
IndexOutOfBoundsException.class, IllegalStateException.class);
Expand Down Expand Up @@ -70,17 +65,9 @@ public static List<Object[]> parameters() {
});
}

@Parameter(0)
public Exception toBeThrownException;

@Parameter(1)
public Predicate<Exception> filter;

@Parameter(2)
public boolean expectForward;

@Test
public void testExceptionFilter() {
@ParameterizedTest
@MethodSource("parameters")
void testExceptionFilter(Exception toBeThrownException, Predicate<Exception> filter, boolean expectForward) {
TypeSolver erroringTypeSolver = mock(TypeSolver.class);
doThrow(toBeThrownException).when(erroringTypeSolver).tryToSolveType(any(String.class));

Expand All @@ -91,11 +78,11 @@ public void testExceptionFilter() {
try {
new CombinedTypeSolver(filter, erroringTypeSolver, secondaryTypeSolver)
.tryToSolveType("an uninteresting string");
assertTrue("Forwarded, but we expected an exception", expectForward);
assertTrue(expectForward, "Forwarded, but we expected an exception");
} catch (Exception e) {
assertFalse("Exception, but we expected forwarding", expectForward); // if we expected the error to be
// forwarded there shouldn't be an
// exception
assertFalse(expectForward, "Exception, but we expected forwarding"); // if we expected the error to be
// forwarded there shouldn't be an
// exception
}

verify(secondaryTypeSolver, times(expectForward ? 1 : 0)).tryToSolveType(any(String.class));
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -322,6 +322,12 @@
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand Down

0 comments on commit 625bc06

Please sign in to comment.