How to use ParameterSignature class of org.junit.experimental.theories package

Best junit code snippet using org.junit.experimental.theories.ParameterSignature

Source:AllMembersSupplier.java Github

copy

Full Screen

...5import java.util.ArrayList;6import java.util.List;7import org.junit.experimental.theories.DataPoint;8import org.junit.experimental.theories.DataPoints;9import org.junit.experimental.theories.ParameterSignature;10import org.junit.experimental.theories.ParameterSupplier;11import org.junit.experimental.theories.PotentialAssignment;12import org.junit.runners.model.FrameworkMethod;13import org.junit.runners.model.TestClass;14/**15 * Supplies Theory parameters based on all public members of the target class.16 */17public class AllMembersSupplier extends ParameterSupplier {18 static class MethodParameterValue extends PotentialAssignment {19 private final FrameworkMethod fMethod;20 private MethodParameterValue(FrameworkMethod dataPointMethod) {21 fMethod = dataPointMethod;22 }23 @Override24 public Object getValue() throws CouldNotGenerateValueException {25 try {26 return fMethod.invokeExplosively(null);27 } catch (IllegalArgumentException e) {28 throw new RuntimeException(29 "unexpected: argument length is checked");30 } catch (IllegalAccessException e) {31 throw new RuntimeException(32 "unexpected: getMethods returned an inaccessible method");33 } catch (Throwable e) {34 throw new CouldNotGenerateValueException();35 // do nothing, just look for more values36 }37 }38 @Override39 public String getDescription() throws CouldNotGenerateValueException {40 return fMethod.getName();41 }42 }43 private final TestClass fClass;44 /**45 * Constructs a new supplier for {@code type}46 */47 public AllMembersSupplier(TestClass type) {48 fClass = type;49 }50 @Override51 public List<PotentialAssignment> getValueSources(ParameterSignature sig) {52 List<PotentialAssignment> list = new ArrayList<PotentialAssignment>();53 addFields(sig, list);54 addSinglePointMethods(sig, list);55 addMultiPointMethods(sig, list);56 return list;57 }58 private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) {59 for (FrameworkMethod dataPointsMethod : fClass60 .getAnnotatedMethods(DataPoints.class)) {61 try {62 addMultiPointArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));63 } catch (Throwable e) {64 // ignore and move on65 }66 }67 }68 private void addSinglePointMethods(ParameterSignature sig,69 List<PotentialAssignment> list) {70 for (FrameworkMethod dataPointMethod : fClass71 .getAnnotatedMethods(DataPoint.class)) {72 if (isCorrectlyTyped(sig, dataPointMethod.getType())) {73 list.add(new MethodParameterValue(dataPointMethod));74 }75 }76 }77 private void addFields(ParameterSignature sig,78 List<PotentialAssignment> list) {79 for (final Field field : fClass.getJavaClass().getFields()) {80 if (Modifier.isStatic(field.getModifiers())) {81 Class<?> type = field.getType();82 if (sig.canAcceptArrayType(type)83 && field.getAnnotation(DataPoints.class) != null) {84 try {85 addArrayValues(field.getName(), list, getStaticFieldValue(field));86 } catch (Throwable e) {87 // ignore and move on88 }89 } else if (sig.canAcceptType(type)90 && field.getAnnotation(DataPoint.class) != null) {91 list.add(PotentialAssignment92 .forValue(field.getName(), getStaticFieldValue(field)));93 }94 }95 }96 }97 private void addArrayValues(String name, List<PotentialAssignment> list, Object array) {98 for (int i = 0; i < Array.getLength(array); i++) {99 list.add(PotentialAssignment.forValue(name + "[" + i + "]", Array.get(array, i)));100 }101 }102 private void addMultiPointArrayValues(ParameterSignature sig, String name, List<PotentialAssignment> list,103 Object array) throws Throwable {104 for (int i = 0; i < Array.getLength(array); i++) {105 if (!isCorrectlyTyped(sig, Array.get(array, i).getClass())) {106 return;107 }108 list.add(PotentialAssignment.forValue(name + "[" + i + "]", Array.get(array, i)));109 }110 }111 @SuppressWarnings("deprecation")112 private boolean isCorrectlyTyped(ParameterSignature parameterSignature, Class<?> type) {113 return parameterSignature.canAcceptType(type);114 }115 private Object getStaticFieldValue(final Field field) {116 try {117 return field.get(null);118 } catch (IllegalArgumentException e) {119 throw new RuntimeException(120 "unexpected: field from getClass doesn't exist on object");121 } catch (IllegalAccessException e) {122 throw new RuntimeException(123 "unexpected: getFields returned an inaccessible field");124 }125 }126}...

Full Screen

Full Screen

Source:Assignments.java Github

copy

Full Screen

1package org.junit.experimental.theories.internal;2import java.lang.reflect.Method;3import java.util.ArrayList;4import java.util.List;5import org.junit.experimental.theories.ParameterSignature;6import org.junit.experimental.theories.ParameterSupplier;7import org.junit.experimental.theories.ParametersSuppliedBy;8import org.junit.experimental.theories.PotentialAssignment;9import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;10import org.junit.runners.model.TestClass;11/**12 * A potentially incomplete list of value assignments for a method's formal13 * parameters14 */15public class Assignments {16 private List<PotentialAssignment> fAssigned;17 private final List<ParameterSignature> fUnassigned;18 private final TestClass fClass;19 private Assignments(List<PotentialAssignment> assigned,20 List<ParameterSignature> unassigned, TestClass testClass) {21 fUnassigned = unassigned;22 fAssigned = assigned;23 fClass = testClass;24 }25 /**26 * Returns a new assignment list for {@code testMethod}, with no params27 * assigned.28 */29 public static Assignments allUnassigned(Method testMethod,30 TestClass testClass) throws Exception {31 List<ParameterSignature> signatures;32 signatures = ParameterSignature.signatures(testClass33 .getOnlyConstructor());34 signatures.addAll(ParameterSignature.signatures(testMethod));35 return new Assignments(new ArrayList<PotentialAssignment>(),36 signatures, testClass);37 }38 public boolean isComplete() {39 return fUnassigned.size() == 0;40 }41 public ParameterSignature nextUnassigned() {42 return fUnassigned.get(0);43 }44 public Assignments assignNext(PotentialAssignment source) {45 List<PotentialAssignment> assigned = new ArrayList<PotentialAssignment>(46 fAssigned);47 assigned.add(source);48 return new Assignments(assigned, fUnassigned.subList(1, fUnassigned49 .size()), fClass);50 }51 public Object[] getActualValues(int start, int stop, boolean nullsOk)52 throws CouldNotGenerateValueException {53 Object[] values = new Object[stop - start];54 for (int i = start; i < stop; i++) {55 Object value = fAssigned.get(i).getValue();56 if (value == null && !nullsOk) {57 throw new CouldNotGenerateValueException();58 }59 values[i - start] = value;60 }61 return values;62 }63 public List<PotentialAssignment> potentialsForNextUnassigned()64 throws InstantiationException, IllegalAccessException {65 ParameterSignature unassigned = nextUnassigned();66 return getSupplier(unassigned).getValueSources(unassigned);67 }68 public ParameterSupplier getSupplier(ParameterSignature unassigned)69 throws InstantiationException, IllegalAccessException {70 ParameterSupplier supplier = getAnnotatedSupplier(unassigned);71 if (supplier != null) {72 return supplier;73 }74 return new AllMembersSupplier(fClass);75 }76 public ParameterSupplier getAnnotatedSupplier(ParameterSignature unassigned)77 throws InstantiationException, IllegalAccessException {78 ParametersSuppliedBy annotation = unassigned79 .findDeepAnnotation(ParametersSuppliedBy.class);80 if (annotation == null) {81 return null;82 }83 return annotation.value().newInstance();84 }85 public Object[] getConstructorArguments(boolean nullsOk)86 throws CouldNotGenerateValueException {87 return getActualValues(0, getConstructorParameterCount(), nullsOk);88 }89 public Object[] getMethodArguments(boolean nullsOk)90 throws CouldNotGenerateValueException {91 return getActualValues(getConstructorParameterCount(),92 fAssigned.size(), nullsOk);93 }94 public Object[] getAllArguments(boolean nullsOk)95 throws CouldNotGenerateValueException {96 return getActualValues(0, fAssigned.size(), nullsOk);97 }98 private int getConstructorParameterCount() {99 List<ParameterSignature> signatures = ParameterSignature100 .signatures(fClass.getOnlyConstructor());101 int constructorParameterCount = signatures.size();102 return constructorParameterCount;103 }104 public Object[] getArgumentStrings(boolean nullsOk)105 throws CouldNotGenerateValueException {106 Object[] values = new Object[fAssigned.size()];107 for (int i = 0; i < values.length; i++) {108 values[i] = fAssigned.get(i).getDescription();109 }110 return values;111 }112}...

Full Screen

Full Screen

Source:SpecificDataPointsSupplier.java Github

copy

Full Screen

...5import java.util.Collection;6import org.junit.experimental.theories.DataPoint;7import org.junit.experimental.theories.DataPoints;8import org.junit.experimental.theories.FromDataPoints;9import org.junit.experimental.theories.ParameterSignature;10import org.junit.runners.model.FrameworkMethod;11import org.junit.runners.model.TestClass;12public class SpecificDataPointsSupplier extends AllMembersSupplier {13 public SpecificDataPointsSupplier(TestClass testClass) {14 super(testClass);15 }16 /* access modifiers changed from: protected */17 public Collection<Field> getDataPointsFields(ParameterSignature parameterSignature) {18 Collection<Field> dataPointsFields = super.getDataPointsFields(parameterSignature);19 String value = ((FromDataPoints) parameterSignature.getAnnotation(FromDataPoints.class)).value();20 ArrayList arrayList = new ArrayList();21 for (Field next : dataPointsFields) {22 if (Arrays.asList(((DataPoints) next.getAnnotation(DataPoints.class)).value()).contains(value)) {23 arrayList.add(next);24 }25 }26 return arrayList;27 }28 /* access modifiers changed from: protected */29 public Collection<FrameworkMethod> getDataPointsMethods(ParameterSignature parameterSignature) {30 Collection<FrameworkMethod> dataPointsMethods = super.getDataPointsMethods(parameterSignature);31 String value = ((FromDataPoints) parameterSignature.getAnnotation(FromDataPoints.class)).value();32 ArrayList arrayList = new ArrayList();33 for (FrameworkMethod next : dataPointsMethods) {34 if (Arrays.asList(((DataPoints) next.getAnnotation(DataPoints.class)).value()).contains(value)) {35 arrayList.add(next);36 }37 }38 return arrayList;39 }40 /* access modifiers changed from: protected */41 public Collection<Field> getSingleDataPointFields(ParameterSignature parameterSignature) {42 Collection<Field> singleDataPointFields = super.getSingleDataPointFields(parameterSignature);43 String value = ((FromDataPoints) parameterSignature.getAnnotation(FromDataPoints.class)).value();44 ArrayList arrayList = new ArrayList();45 for (Field next : singleDataPointFields) {46 if (Arrays.asList(((DataPoint) next.getAnnotation(DataPoint.class)).value()).contains(value)) {47 arrayList.add(next);48 }49 }50 return arrayList;51 }52 /* access modifiers changed from: protected */53 public Collection<FrameworkMethod> getSingleDataPointMethods(ParameterSignature parameterSignature) {54 Collection<FrameworkMethod> singleDataPointMethods = super.getSingleDataPointMethods(parameterSignature);55 String value = ((FromDataPoints) parameterSignature.getAnnotation(FromDataPoints.class)).value();56 ArrayList arrayList = new ArrayList();57 for (FrameworkMethod next : singleDataPointMethods) {58 if (Arrays.asList(((DataPoint) next.getAnnotation(DataPoint.class)).value()).contains(value)) {59 arrayList.add(next);60 }61 }62 return arrayList;63 }64}...

Full Screen

Full Screen

Source:ParameterSignatureTest.java Github

copy

Full Screen

...9import java.util.List;10import org.hamcrest.CoreMatchers;11import org.junit.Test;12import org.junit.experimental.theories.DataPoint;13import org.junit.experimental.theories.ParameterSignature;14import org.junit.experimental.theories.Theories;15import org.junit.experimental.theories.Theory;16import org.junit.experimental.theories.suppliers.TestedOn;17import org.junit.runner.RunWith;18@RunWith(Theories.class)19public class ParameterSignatureTest {20 @DataPoint21 public static Method getType() throws SecurityException,22 NoSuchMethodException {23 return ParameterSignatureTest.class.getMethod("getType", Method.class,24 int.class);25 }26 @DataPoint27 public static int ZERO = 0;28 @DataPoint29 public static int ONE = 1;30 @Theory31 public void getType(Method method, int index) {32 assumeTrue(index < method.getParameterTypes().length);33 assertEquals(method.getParameterTypes()[index], ParameterSignature34 .signatures(method).get(index).getType());35 }36 public void foo(@TestedOn(ints = {1, 2, 3}) int x) {37 }38 @Test39 public void getAnnotations() throws SecurityException,40 NoSuchMethodException {41 Method method = getClass().getMethod("foo", int.class);42 List<Annotation> annotations = ParameterSignature.signatures(method)43 .get(0).getAnnotations();44 assertThat(annotations,45 CoreMatchers.<TestedOn>hasItem(isA(TestedOn.class)));46 }47 48 public void intMethod(int param) {49 }50 51 public void integerMethod(Integer param) {52 }53 54 public void numberMethod(Number param) {55 }56 @Test57 public void primitiveTypesShouldBeAcceptedAsWrapperTypes() throws Exception {58 List<ParameterSignature> signatures = ParameterSignature59 .signatures(getClass().getMethod("integerMethod", Integer.class));60 ParameterSignature integerSignature = signatures.get(0);61 assertTrue(integerSignature.canAcceptType(int.class));62 }63 64 @Test65 public void primitiveTypesShouldBeAcceptedAsWrapperTypeAssignables() throws Exception {66 List<ParameterSignature> signatures = ParameterSignature67 .signatures(getClass().getMethod("numberMethod", Number.class));68 ParameterSignature numberSignature = signatures.get(0);69 assertTrue(numberSignature.canAcceptType(int.class));70 }71 72 @Test73 public void wrapperTypesShouldBeAcceptedAsPrimitiveTypes() throws Exception {74 List<ParameterSignature> signatures = ParameterSignature75 .signatures(getClass().getMethod("intMethod", int.class));76 ParameterSignature intSignature = signatures.get(0);77 assertTrue(intSignature.canAcceptType(Integer.class));78 }79}...

Full Screen

Full Screen

Source:WebDriverSuppliers.java Github

copy

Full Screen

1package amazon.framework.core;2import org.junit.experimental.theories.ParameterSignature;3import org.junit.experimental.theories.ParameterSupplier;4import org.junit.experimental.theories.ParametersSuppliedBy;5import org.junit.experimental.theories.PotentialAssignment;6import java.lang.annotation.Retention;7import java.lang.annotation.RetentionPolicy;8import java.util.ArrayList;9import java.util.List;10public class WebDriverSuppliers {11 /**12 * Execute the test with multiple browsers while using the JUnit Theories runtime13 *14 * @author Nicolas Rémond (nre)15 */16 @Retention(RetentionPolicy.RUNTIME)17 @ParametersSuppliedBy(AllWebDriversSupplier.class)18 public @interface AllWebDrivers {19 }20 public static class AllWebDriversSupplier extends ParameterSupplier {21 @Override22 public List<PotentialAssignment> getValueSources(final ParameterSignature sig) {23 final List<PotentialAssignment> assignments = new ArrayList<PotentialAssignment>();24 assignments.add(PotentialAssignment.forValue(AbstractWebDriverTestCase.WebDriverKind.Chrome.toString(), AbstractWebDriverTestCase.WebDriverKind.Chrome));25 return assignments;26 }27 }28 @Retention(RetentionPolicy.RUNTIME)29 @ParametersSuppliedBy(SingleWebDriverSupplier.class)30 public @interface SingleWebDriver {31 }32 public static class SingleWebDriverSupplier extends ParameterSupplier {33 @Override34 public List<PotentialAssignment> getValueSources(final ParameterSignature sig) {35 final List<PotentialAssignment> assignments = new ArrayList<PotentialAssignment>();36 assignments.add(PotentialAssignment.forValue(AbstractWebDriverTestCase.WebDriverKind.Chrome.toString(), AbstractWebDriverTestCase.WebDriverKind.Chrome));37 return assignments;38 }39 }40}...

Full Screen

Full Screen

Source:TestedOnBoolSupplier.java Github

copy

Full Screen

1package org.wikipedia.test.theories;2import org.junit.experimental.theories.ParameterSignature;3import org.junit.experimental.theories.ParameterSupplier;4import org.junit.experimental.theories.PotentialAssignment;5import java.util.Arrays;6import java.util.List;7public class TestedOnBoolSupplier extends ParameterSupplier {8 private static final String NAME = "bools";9 @Override public List<PotentialAssignment> getValueSources(ParameterSignature sig) {10 return Arrays.asList(PotentialAssignment.forValue(NAME, false),11 PotentialAssignment.forValue(NAME, true));12 }13}...

Full Screen

Full Screen

ParameterSignature

Using AI Code Generation

copy

Full Screen

1import org.junit.experimental.theories.ParameterSignature;2import org.junit.experimental.theories.ParameterSupplier;3import org.junit.experimental.theories.Theories;4import org.junit.experimental.theories.Theory;5import org.junit.runner.RunWith;6import java.util.ArrayList;7import java.util.Arrays;8import java.util.List;9@RunWith(Theories.class)10public class TestTheories {11 public void test(@MyParameterSupplierAnnotation int[] array) {12 System.out.println(Arrays.toString(array));13 }14}15@Retention(RetentionPolicy.RUNTIME)16@interface MyParameterSupplierAnnotation {17}18public class MyParameterSupplier extends ParameterSupplier {19 public List<Integer[]> getValueSources(ParameterSignature sig) {20 List<Integer[]> list = new ArrayList<>();21 list.add(new Integer[]{1, 2, 3});22 list.add(new Integer[]{4, 5, 6});23 return list;24 }25}

Full Screen

Full Screen

ParameterSignature

Using AI Code Generation

copy

Full Screen

1import org.junit.experimental.theories.*;2import org.junit.runner.*;3@RunWith(Theories.class)4public class ParameterSignatureTest {5 public void test1(@FromDataPoints("data1") String s) {6 System.out.println(s);7 }8 public static String[] data1 = {"abc", "def", "ghi"};9}

Full Screen

Full Screen

ParameterSignature

Using AI Code Generation

copy

Full Screen

1public class ParameterSignatureTest {2 public void testParameterSignature() {3 ParameterSignature parameterSignature = new ParameterSignature("name");4 assertThat(parameterSignature.getName(), is("name"));5 }6}7public class ParametersSuppliedByTest {8 public void testParametersSuppliedBy() {9 ParametersSuppliedBy parametersSuppliedBy = new ParametersSuppliedBy(Object.class);10 assertThat(parametersSuppliedBy.value(), is(Object.class));11 }12}13public class PotentialAssignmentTest {14 public void testPotentialAssignment() {15 PotentialAssignment potentialAssignment = PotentialAssignment.forValue("name", "value");16 assertThat(potentialAssignment.getDescription(), is("name"));17 assertThat(potentialAssignment.getValue(), is("value"));18 }19}20public class ParameterSignatureTest {21 public void testParameterSignature() {22 ParameterSignature parameterSignature = new ParameterSignature("name");23 assertThat(parameterSignature.getName(), is("name"));24 }25}26public class ParametersSuppliedByTest {27 public void testParametersSuppliedBy() {28 ParametersSuppliedBy parametersSuppliedBy = new ParametersSuppliedBy(Object.class);29 assertThat(parametersSuppliedBy.value(), is(Object.class));30 }31}32public class PotentialAssignmentTest {33 public void testPotentialAssignment() {34 PotentialAssignment potentialAssignment = PotentialAssignment.forValue("name", "value");35 assertThat(potentialAssignment.getDescription(), is("name"));36 assertThat(potentialAssignment.getValue(), is("value"));37 }38}39public class ParameterSignatureTest {40 public void testParameterSignature() {41 ParameterSignature parameterSignature = new ParameterSignature("name");42 assertThat(parameterSignature.getName(), is("name"));43 }44}45public class ParametersSuppliedByTest {46 public void testParametersSuppliedBy() {

Full Screen

Full Screen

ParameterSignature

Using AI Code Generation

copy

Full Screen

1package com.zetcode;2import org.junit.experimental.theories.ParameterSignature;3import org.junit.experimental.theories.ParameterSupplier;4import org.junit.experimental.theories.PotentialAssignment;5import org.junit.experimental.theories.Theories;6import org.junit.experimental.theories.Theory;7import org.junit.runner.RunWith;8import java.util.Arrays;9import java.util.List;10@RunWith(Theories.class)11public class JunitTheoriesTest {12 public void testString(@StringSource String s) {13 System.out.println(s);14 }15 public static class StringSource extends ParameterSupplier {16 public List<PotentialAssignment> getValueSources(ParameterSignature sig) {17 return Arrays.asList(18 PotentialAssignment.forValue("one", "one"),19 PotentialAssignment.forValue("two", "two"),20 PotentialAssignment.forValue("three", "three")21 );22 }23 }24}25import static org.junit.Assert.*;26import org.junit.Test;27public class JunitTheoriesTest {28 public void testString() {29 fail("Not yet implemented");30 }31}

Full Screen

Full Screen

JUnit Tutorial:

LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.

JUnit Tutorial Chapters:

Here are the detailed JUnit testing chapters to help you get started:

  • Importance of Unit testing - Learn why Unit testing is essential during the development phase to identify bugs and errors.
  • Top Java Unit testing frameworks - Here are the upcoming JUnit automation testing frameworks that you can use in 2023 to boost your unit testing.
  • What is the JUnit framework
  • Why is JUnit testing important - Learn the importance and numerous benefits of using the JUnit testing framework.
  • Features of JUnit - Learn about the numerous features of JUnit and why developers prefer it.
  • JUnit 5 vs. JUnit 4: Differences - Here is a complete comparison between JUnit 5 and JUnit 4 testing frameworks.
  • Setting up the JUnit environment - Learn how to set up your JUnit testing environment.
  • Getting started with JUnit testing - After successfully setting up your JUnit environment, this chapter will help you get started with JUnit testing in no time.
  • Parallel testing with JUnit - Parallel Testing can be used to reduce test execution time and improve test efficiency. Learn how to perform parallel testing with JUnit.
  • Annotations in JUnit - When writing automation scripts with JUnit, we can use JUnit annotations to specify the type of methods in our test code. This helps us identify those methods when we run JUnit tests using Selenium WebDriver. Learn in detail what annotations are in JUnit.
  • Assertions in JUnit - Assertions are used to validate or test that the result of an action/functionality is the same as expected. Learn in detail what assertions are and how to use them while performing JUnit testing.
  • Parameterization in JUnit - Parameterized Test enables you to run the same automated test scripts with different variables. By collecting data on each method's test parameters, you can minimize time spent on writing tests. Learn how to use parameterization in JUnit.
  • Nested Tests In JUnit 5 - A nested class is a non-static class contained within another class in a hierarchical structure. It can share the state and setup of the outer class. Learn about nested annotations in JUnit 5 with examples.
  • Best practices for JUnit testing - Learn about the best practices, such as always testing key methods and classes, integrating JUnit tests with your build, and more to get the best possible results.
  • Advanced Use Cases for JUnit testing - Take a deep dive into the advanced use cases, such as how to run JUnit tests in Jupiter, how to use JUnit 5 Mockito for Unit testing, and more for JUnit testing.

JUnit Certification:

You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful