本文整理汇总了Java中org.junit.runners.model.TestClass类的典型用法代码示例。如果您正苦于以下问题:Java TestClass类的具体用法?Java TestClass怎么用?Java TestClass使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TestClass类属于org.junit.runners.model包,在下文中一共展示了TestClass类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldInitializeInjectDelayedField
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Test
public void shouldInitializeInjectDelayedField() throws Throwable {
// given - test class with initialized @Mock fields
DelayedInjectionRunnerIntegrationTest runnerTest = new DelayedInjectionRunnerIntegrationTest();
MockitoAnnotations.initMocks(runnerTest);
TestClass testClass = new TestClass(runnerTest.getClass());
Statement nextStatement = mock(Statement.class);
List<FrameworkField> injectDelayedFields = testClass.getAnnotatedFields(InjectDelayed.class);
assertThat(injectDelayedFields, hasSize(1)); // assumption
RunDelayedInjects runDelayedInjects =
new RunDelayedInjects(nextStatement, testClass, runnerTest, injectDelayedFields);
// when
runDelayedInjects.evaluate();
// then
assertThat(ReflectionUtils.getFieldValue(injectDelayedFields.get(0).getField(), runnerTest), not(nullValue()));
verify(nextStatement).evaluate();
}
示例2: shouldFindPossibleAssignmentsFromMethod
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Test
public void shouldFindPossibleAssignmentsFromMethod() throws Throwable {
this.assignments = new PotentialAssignments(new TestClass(AssignmentsFromMethod.class));
final List<Assignment> assignments = this.assignments.allPossible().collect(Collectors.toList());
assertThat(assignments).hasSize(2);
ArrayList<ParameterSignature> parameterSignature =
ParameterSignature.signatures(AssignmentsFromMethod.class.getMethod("a", String.class));
assertThat(assignments.get(0).isValidFor(parameterSignature.get(0)));
assertThat(assignments.get(0).parameterProducer().produceParamValue()).isEqualTo("42");
assertThat(assignments.get(1).isValidFor(parameterSignature.get(0)));
assertThat(assignments.get(1).parameterProducer().produceParamValue()).isEqualTo("27");
}
示例3: createRunners
import org.junit.runners.model.TestClass; //导入依赖的package包/类
private static List<Runner> createRunners(final Class<?> clazz) throws InitializationError {
final ValueConverter defaultValueConverter = getDefaultValueConverter(clazz);
final List<Runner> runners = new ArrayList<>();
final Table classWideTable = classWideTableOrNull(clazz);
if (classWideTable != null) {
for (final TableRow row : classWideTable) {
runners.add(new SingleRowMultiTestRunner(clazz, row, defaultValueConverter));
}
} else {
for (final FrameworkMethod testMethod : new TestClass(clazz).getAnnotatedMethods(Test.class)) {
final Spockito.UseValueConverter useValueConverter = testMethod.getAnnotation(Spockito.UseValueConverter.class);
final ValueConverter methodValueConverter = Spockito.getValueConverter(useValueConverter, defaultValueConverter);
runners.add(new SingleTestMultiRowRunner(clazz, testMethod, methodValueConverter));
}
}
return runners;
}
示例4: BytecoderUnitTestRunner
import org.junit.runners.model.TestClass; //导入依赖的package包/类
public BytecoderUnitTestRunner(Class aClass) throws InitializationError {
super(aClass);
testClass = new TestClass(aClass);
testMethods = new ArrayList<>();
Method[] classMethods = aClass.getDeclaredMethods();
for (Method classMethod : classMethods) {
Class retClass = classMethod.getReturnType();
int length = classMethod.getParameterTypes().length;
int modifiers = classMethod.getModifiers();
if (retClass == null || length != 0 || Modifier.isStatic(modifiers)
|| !Modifier.isPublic(modifiers) || Modifier.isInterface(modifiers)
|| Modifier.isAbstract(modifiers)) {
continue;
}
String methodName = classMethod.getName();
if (methodName.toUpperCase().startsWith("TEST")
|| classMethod.getAnnotation(Test.class) != null) {
testMethods.add(new FrameworkMethod(classMethod));
}
if (classMethod.getAnnotation(Ignore.class) != null) {
testMethods.remove(classMethod);
}
}
}
示例5: shouldProvideMock
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Test
public void shouldProvideMock() throws Exception {
// given
DelayedInjectionRunnerIntegrationTest runnerTest = new DelayedInjectionRunnerIntegrationTest();
MockitoAnnotations.initMocks(runnerTest);
TestClass testClass = new TestClass(runnerTest.getClass());
MockDependencyHandler mockDependencyHandler = new MockDependencyHandler(testClass, runnerTest);
AlphaService alphaService = mock(AlphaService.class);
given(injector.getIfAvailable(AlphaService.class)).willReturn(alphaService);
// when
Resolution<?> value = mockDependencyHandler.resolve(newContext(AlphaService.class));
// then
assertThat(unwrapFromSimpleResolution(value) == alphaService, equalTo(true));
verify(injector).register(eq(AlphaService.class), any(AlphaService.class));
verify(injector).register(eq(ClassWithAbstractDependency.AbstractDependency.class),
any(ClassWithAbstractDependency.AbstractDependency.class));
verify(injector).getIfAvailable(AlphaService.class);
}
示例6: shouldThrowForUnavailableMock
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Test
public void shouldThrowForUnavailableMock() {
// given
DelayedInjectionRunnerIntegrationTest runnerTest = new DelayedInjectionRunnerIntegrationTest();
MockitoAnnotations.initMocks(runnerTest);
TestClass testClass = new TestClass(runnerTest.getClass());
MockDependencyHandler mockDependencyHandler = new MockDependencyHandler(testClass, runnerTest);
given(injector.getIfAvailable(AlphaService.class)).willReturn(null);
// when
try {
mockDependencyHandler.resolve(newContext(AlphaService.class));
fail("Expected exception to be thrown");
} catch (InjectorException e) {
assertThat(e.getMessage(), containsString("dependencies of @InjectDelayed must be provided as @Mock"));
verify(injector, times(3)).register(any(Class.class), any(Object.class));
verify(injector).getIfAvailable(AlphaService.class);
}
}
示例7: shouldThrowForAlreadyInitializedField
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Test
public void shouldThrowForAlreadyInitializedField() throws Throwable {
// given - test class with initialized @Mock fields
DelayedInjectionRunnerIntegrationTest runnerTest = new DelayedInjectionRunnerIntegrationTest();
MockitoAnnotations.initMocks(runnerTest);
TestClass testClass = new TestClass(runnerTest.getClass());
Statement nextStatement = mock(Statement.class);
List<FrameworkField> injectDelayedFields = testClass.getAnnotatedFields(InjectDelayed.class);
assertThat(injectDelayedFields, hasSize(1)); // assumption
ReflectionUtils.setField(injectDelayedFields.get(0).getField(), runnerTest, mock(SampleInjectClass.class));
RunDelayedInjects runDelayedInjects =
new RunDelayedInjects(nextStatement, testClass, runnerTest, injectDelayedFields);
// when
try {
runDelayedInjects.evaluate();
fail("Expected exception to be thrown");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("Field with @InjectDelayed must be null"));
}
}
示例8: shouldValidateUnsuccessfullyForInjectMocksPresence
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Test
public void shouldValidateUnsuccessfullyForInjectMocksPresence() throws Exception {
// given
RunNotifier notifier = mock(RunNotifier.class);
TestClass testClass = new TestClass(getClass());
DelayedInjectionRunnerValidator validator = new DelayedInjectionRunnerValidator(notifier, testClass);
Description description = mock(Description.class);
// when
validator.testFinished(description);
// then
ArgumentCaptor<Failure> captor = ArgumentCaptor.forClass(Failure.class);
verify(notifier).fireTestFailure(captor.capture());
Failure failure = captor.getValue();
assertThat(failure.getMessage(), containsString("Do not use @InjectMocks"));
}
示例9: getServices
import org.junit.runners.model.TestClass; //导入依赖的package包/类
protected ServiceTest[] getServices()
{
TestClass test = getTestClass();
ServiceTests config
= test.getAnnotation(ServiceTests.class);
if (config != null)
return config.value();
Annotation[] annotations = test.getAnnotations();
List<ServiceTest> list = new ArrayList<>();
for (Annotation annotation : annotations) {
if (ServiceTest.class.isAssignableFrom(annotation.getClass()))
list.add((ServiceTest) annotation);
}
return list.toArray(new ServiceTest[list.size()]);
}
示例10: createTestUsingFieldInjection
import org.junit.runners.model.TestClass; //导入依赖的package包/类
/**
* @see BlockJUnit4ClassRunnerWithParameters#createTestUsingFieldInjection()
*/
private static Object createTestUsingFieldInjection(TestClass testClass, Object[] parameters) throws Exception {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter(testClass);
if (annotatedFieldsByParameter.size() != parameters.length) {
throw new Exception("Wrong number of parameters and @Parameter fields." + " @Parameter fields counted: " + annotatedFieldsByParameter.size()
+ ", available parameters: " + parameters.length + ".");
}
Object testClassInstance = testClass.getJavaClass().newInstance();
for (FrameworkField each : annotatedFieldsByParameter) {
Field field = each.getField();
Parameterized.Parameter annotation = field.getAnnotation(Parameterized.Parameter.class);
int index = annotation.value();
try {
field.set(testClassInstance, parameters[index]);
} catch (IllegalArgumentException iare) {
throw new Exception(
testClass.getName() + ": Trying to set " + field.getName() + " with the value " + parameters[index] + " that is not the right type ("
+ parameters[index].getClass().getSimpleName() + " instead of " + field.getType().getSimpleName() + ").",
iare);
}
}
return testClassInstance;
}
开发者ID:PeterWippermann,项目名称:parameterized-suite,代码行数:26,代码来源:BlockJUnit4ClassRunnerWithParametersUtil.java
示例11: buildStatementWithTestRules
import org.junit.runners.model.TestClass; //导入依赖的package包/类
/**
* Extends a given {@link Statement} for a {@link TestClass} with the evaluation of
* {@link TestRule}, {@link ClassRule}, {@link Before} and {@link After}.
* <p>
* Therefore the test class will be instantiated and parameters will be injected with the same
* mechanism as in {@link Parameterized}.
*
* Implementation has been extracted from BlockJUnit4ClassRunner#methodBlock(FrameworkMethod).
*
* @param baseStatementWithChildren - A {@link Statement} that includes execution of the test's
* children
* @param testClass - The {@link TestClass} of the test.
* @param description - The {@link Description} will be passed to the {@link Rule}s and
* {@link ClassRule}s.
* @param singleParameter - The parameters will be injected in attributes annotated with
* {@link Parameterized.Parameter} or passed to the constructor otherwise.
*
* @see BlockJUnit4ClassRunnerWithParameters#createTest()
* @see BlockJUnit4ClassRunner#methodBlock(FrameworkMethod)
*/
public static Statement buildStatementWithTestRules(Statement baseStatementWithChildren, final TestClass testClass, Description description,
final Object[] singleParameter) {
final Object test;
try {
test = new ReflectiveCallable() {
protected Object runReflectiveCall() throws Throwable {
return createInstanceOfParameterizedTest(testClass, singleParameter);
}
}.run();
} catch (Throwable e) {
return new Fail(e);
}
List<TestRule> testRules = BlockJUnit4ClassRunnerUtil.getTestRules(test, testClass);
Statement statement = BlockJUnit4ClassRunnerUtil.withTestRules(testRules, description, baseStatementWithChildren);
statement = ParentRunnerUtil.withBeforeClasses(statement, testClass);
statement = ParentRunnerUtil.withAfterClasses(statement, testClass);
statement = ParentRunnerUtil.withClassRules(statement, testClass, description);
return statement;
}
开发者ID:PeterWippermann,项目名称:parameterized-suite,代码行数:42,代码来源:BlockJUnit4ClassRunnerWithParametersUtil.java
示例12: getOrCreateInjectorProvider
import org.junit.runners.model.TestClass; //导入依赖的package包/类
public static IInjectorProvider getOrCreateInjectorProvider(TestClass testClass) {
InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
if (injectWith != null) {
Class<? extends IInjectorProvider> klass = injectWith.value();
IInjectorProvider injectorProvider = injectorProviderClassCache.get(klass);
if (injectorProvider == null) {
try {
injectorProvider = klass.newInstance();
injectorProviderClassCache.put(klass, injectorProvider);
} catch (Exception e) {
throwUncheckedException(e);
}
}
return injectorProvider;
}
return null;
}
示例13: runBootstrapTest
import org.junit.runners.model.TestClass; //导入依赖的package包/类
protected boolean runBootstrapTest(RunNotifier notifier, TestClass testClass) {
if (!runningBootstrapTest.get().booleanValue()) {
runningBootstrapTest.set(Boolean.TRUE);
try {
BootstrapTest bootstrapTest = getBootstrapTestAnnotation(testClass.getJavaClass());
if (bootstrapTest != null) {
Result result = JUnitCore.runClasses(bootstrapTest.value());
List<Failure> failures = result.getFailures();
for (Failure failure : failures) {
notifier.fireTestFailure(failure);
}
return result.getFailureCount() == 0;
} else {
throw new IllegalStateException("LoadTimeWeavableTestRunner, must be coupled with an @BootstrapTest annotation to define the bootstrap test to execute.");
}
} finally {
runningBootstrapTest.set(Boolean.FALSE);
}
}
return true;
}
示例14: getParameterizations
import org.junit.runners.model.TestClass; //导入依赖的package包/类
/**
* Gets the parameterization
* @return the parameterization collection
* @throws Throwable if the annotation requirements are not met, or if there's an error in invoking
* the class's "get parameterizations" method.
*/
private Collection<Parameterization> getParameterizations() throws Throwable
{
TestClass cls = getTestClass();
List<FrameworkMethod> methods = cls.getAnnotatedMethods(TestParameters.class);
if (methods.size() != 1)
{
throw new Exception("class " + cls.getName() + " must have exactly 1 method annotated with "
+ TestParameters.class.getSimpleName() +"; found " + methods.size());
}
FrameworkMethod method = methods.get(0);
checkParameterizationMethod(method);
@SuppressWarnings("unchecked")
Collection<Parameterization> ret = (Collection<Parameterization>) method.invokeExplosively(null);
checkParameterizations(ret);
return ret;
}
示例15: createTest
import org.junit.runners.model.TestClass; //导入依赖的package包/类
@Override
protected Object createTest()
throws Exception
{
final TestClass testClass = getTestClass();
final DefaultContainerConfiguration config = new DefaultContainerConfiguration();
config.setAutoWiring( true );
config.setClassPathScanning( PlexusConstants.SCANNING_ON );
config.setComponentVisibility( PlexusConstants.GLOBAL_VISIBILITY );
config.setName( testClass.getName() );
final DefaultPlexusContainer container = new DefaultPlexusContainer( config );
final ClassSpace cs = new URLClassSpace( Thread.currentThread()
.getContextClassLoader() );
container.addPlexusInjector( Arrays.<PlexusBeanModule> asList( new PlexusAnnotatedBeanModule(
cs,
Collections.emptyMap() ) ) );
return container.lookup( testClass.getJavaClass() );
}