本文整理汇总了Java中org.junit.Rule类的典型用法代码示例。如果您正苦于以下问题:Java Rule类的具体用法?Java Rule怎么用?Java Rule使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Rule类属于org.junit包,在下文中一共展示了Rule类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getObjectRule
import org.junit.Rule; //导入依赖的package包/类
@Override
public org.jsonschema2pojo.rules.Rule<JPackage, JType> getObjectRule() {
final org.jsonschema2pojo.rules.Rule<JPackage, JType> workingRule = super.getObjectRule();
return new org.jsonschema2pojo.rules.Rule<JPackage, JType>() {
@Override
public JType apply(String nodeName, JsonNode node, JPackage generatableType, Schema currentSchema) {
JType objectType = workingRule.apply(nodeName, node, generatableType, currentSchema);
if( objectType instanceof JDefinedClass ) {
JDefinedClass jclass = (JDefinedClass)objectType;
jclass.method(JMod.PUBLIC, jclass.owner().BOOLEAN, "brokenMethod").body();
}
return objectType;
}
};
}
示例2: class_has_source_of_import
import org.junit.Rule; //导入依赖的package包/类
@Test
public void class_has_source_of_import() throws Exception {
ArchConfiguration.get().setMd5InClassSourcesEnabled(true);
JavaClass clazzFromFile = new ClassFileImporter().importClass(ClassToImportOne.class);
Source source = clazzFromFile.getSource().get();
assertThat(source.getUri()).isEqualTo(urlOf(ClassToImportOne.class).toURI());
assertThat(source.getMd5sum()).isEqualTo(md5sumOf(bytesAt(urlOf(ClassToImportOne.class))));
JavaClass clazzFromJar = new ClassFileImporter().importClass(Rule.class);
source = clazzFromJar.getSource().get();
assertThat(source.getUri()).isEqualTo(urlOf(Rule.class).toURI());
assertThat(source.getMd5sum()).isEqualTo(md5sumOf(bytesAt(urlOf(Rule.class))));
ArchConfiguration.get().setMd5InClassSourcesEnabled(false);
source = new ClassFileImporter().importClass(ClassToImportOne.class).getSource().get();
assertThat(source.getMd5sum()).isEqualTo(MD5_SUM_DISABLED);
}
示例3: matches_annotation_by_type
import org.junit.Rule; //导入依赖的package包/类
@Test
public void matches_annotation_by_type() {
assertThat(annotatedWith(RuntimeRetentionAnnotation.class).apply(importClassWithContext(AnnotatedClass.class)))
.as("annotated class matches").isTrue();
assertThat(annotatedWith(RuntimeRetentionAnnotation.class.getName()).apply(importClassWithContext(AnnotatedClass.class)))
.as("annotated class matches").isTrue();
assertThat(annotatedWith(RuntimeRetentionAnnotation.class).apply(importClassWithContext(Object.class)))
.as("annotated class matches").isFalse();
assertThat(annotatedWith(RuntimeRetentionAnnotation.class.getName()).apply(importClassWithContext(Object.class)))
.as("annotated class matches").isFalse();
assertThat(annotatedWith(Rule.class).getDescription())
.isEqualTo("annotated with @Rule");
assertThat(annotatedWith(Rule.class.getName()).getDescription())
.isEqualTo("annotated with @Rule");
}
示例4: validateSpringMethodRuleConfiguration
import org.junit.Rule; //导入依赖的package包/类
/**
* Throw an {@link IllegalStateException} if the supplied {@code testClass}
* does not declare a {@code public SpringMethodRule} field that is
* annotated with {@code @Rule}.
*/
private static void validateSpringMethodRuleConfiguration(Class<?> testClass) {
Field ruleField = null;
for (Field field : testClass.getFields()) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) &&
SpringMethodRule.class.isAssignableFrom(field.getType())) {
ruleField = field;
break;
}
}
if (ruleField == null) {
throw new IllegalStateException(String.format(
"Failed to find 'public SpringMethodRule' field in test class [%s]. " +
"Consult the javadoc for SpringClassRule for details.", testClass.getName()));
}
if (!ruleField.isAnnotationPresent(Rule.class)) {
throw new IllegalStateException(String.format(
"SpringMethodRule field [%s] must be annotated with JUnit's @Rule annotation. " +
"Consult the javadoc for SpringClassRule for details.", ruleField));
}
}
示例5: testClassWithPersistenceContextWithKonfiguredUnitNameSpecified
import org.junit.Rule; //导入依赖的package包/类
@Test
public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class);
jAnnotation.param("unitName", "test-unit-1");
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut);
final RunListener listener = mock(RunListener.class);
final RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
// WHEN
runner.run(notifier);
// THEN
final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class);
verify(listener).testStarted(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
verify(listener).testFinished(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
}
示例6: initClassFields
import org.junit.Rule; //导入依赖的package包/类
private void initClassFields(Object target, Class<?> targetClass) {
Field[] targetFields = targetClass.getDeclaredFields();
for (Field field : targetFields) {
if (field.getAnnotation(Rule.class) == null) {
if (!Modifier.isStatic(field.getModifiers())) {
field.setAccessible(true);
try {
final Object value = field.get(target);
if (value != null) {
fields.put(new ObjectId(field), new Provider() {
@Override
public Object get() {
return value;
}
});
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error accessing field " + field, e);
}
}
}
}
}
示例7: openAndCloseDriver
import org.junit.Rule; //导入依赖的package包/类
@Rule
public final TestRule openAndCloseDriver() {
return (base, description) -> new Statement() {
@Override
public void evaluate() throws Throwable {
driver = createDriver();
try {
base.evaluate();
} finally {
if (closeDriver)
try {
driver.close();
} catch (Throwable t) {
// swallow
}
}
}
};
}
示例8: visitEnd
import org.junit.Rule; //导入依赖的package包/类
@Override
public void visitEnd() {
if (transformationParameters.isJUnit4RuleInjectionRequired) {
FieldVisitor fv = super.visitField(Opcodes.ACC_PUBLIC, "scottReportingRule", Type.getDescriptor(ScottReportingRule.class), null, null);
fv.visitAnnotation(Type.getDescriptor(Rule.class), true).visitEnd();
}
if (transformationParameters.isJUnit5ExtensionInjectionRequired) {
AnnotationVisitor av0 = super.visitAnnotation("Lorg/junit/jupiter/api/extension/ExtendWith;", true);
AnnotationVisitor av1 = av0.visitArray("value");
av1.visit(null, Type.getType("Lhu/advancedweb/scott/runtime/ScottJUnit5Extension;"));
av1.visitEnd();
av0.visitEnd();
}
super.visitEnd();
}
示例9: hasTimeoutRule
import org.junit.Rule; //导入依赖的package包/类
/**
* @return {@code true} if the test class has any fields annotated with {@code Rule} whose type
* is {@link Timeout}.
*/
static boolean hasTimeoutRule(TestClass testClass) {
// Many protected convenience methods in BlockJUnit4ClassRunner that are available in JUnit 4.11
// such as getTestRules(Object) were not public until
// https://github.com/junit-team/junit/commit/8782efa08abf5d47afdc16740678661443706740,
// which appears to be JUnit 4.9. Because we allow users to use JUnit 4.7, we need to include a
// custom implementation that is backwards compatible to JUnit 4.7.
List<FrameworkField> fields = testClass.getAnnotatedFields(Rule.class);
for (FrameworkField field : fields) {
if (field.getField().getType().equals(Timeout.class)) {
return true;
}
}
return false;
}
示例10: init
import org.junit.Rule; //导入依赖的package包/类
@Before
public void init() throws Exception {
sonarIssue = new DefaultIssue()
.setKey("ABCD")
.setMessage("The Cyclomatic Complexity of this method is 14 which is greater than 10 authorized.")
.setSeverity("MINOR")
.setRuleKey(RuleKey.of("squid", "CycleBetweenPackages"));
ruleFinder = mock(RuleFinder.class);
when(ruleFinder.findByKey(RuleKey.of("squid", "CycleBetweenPackages"))).thenReturn(org.sonar.api.rules.Rule.create().setName("Avoid cycle between java packages"));
settings = new Settings(new PropertyDefinitions(JiraIssueCreator.class, JiraPlugin.class));
settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://my.sonar.com");
settings.setProperty(JiraConstants.SERVER_URL_PROPERTY, "http://my.jira.com");
settings.setProperty(JiraConstants.USERNAME_PROPERTY, "foo");
settings.setProperty(JiraConstants.PASSWORD_PROPERTY, "bar");
settings.setProperty(JiraConstants.JIRA_PROJECT_KEY_PROPERTY, "TEST");
jiraIssueCreator = new JiraIssueCreator(ruleFinder);
}
示例11: shouldInitRemoteIssueWithoutName
import org.junit.Rule; //导入依赖的package包/类
@Test
public void shouldInitRemoteIssueWithoutName() throws Exception {
// Given that
when(ruleFinder.findByKey(RuleKey.of("squid", "CycleBetweenPackages"))).thenReturn(org.sonar.api.rules.Rule.create().setName(null));
RemoteIssue expectedIssue = new RemoteIssue();
expectedIssue.setProject("TEST");
expectedIssue.setType("3");
expectedIssue.setPriority("4");
expectedIssue.setSummary("Sonar Issue - CycleBetweenPackages");
//expectedIssue.setSummary("Sonar Issue #ABCD");
expectedIssue.setDescription("Issue detail:\n{quote}\nThe Cyclomatic Complexity of this method is 14 which is greater than 10 authorized.\n" +
"{quote}\n\n\nCheck it on Sonar: http://my.sonar.com/issue/show/ABCD");
// Verify
RemoteIssue returnedIssue = jiraIssueCreator.initRemoteIssue(sonarIssue, settings, "");
assertThat(returnedIssue.getSummary()).isEqualTo(expectedIssue.getSummary());
assertThat(returnedIssue.getDescription()).isEqualTo(expectedIssue.getDescription());
assertThat(returnedIssue).isEqualTo(expectedIssue);
}
示例12: hasTimeoutRule
import org.junit.Rule; //导入依赖的package包/类
/**
* @return {@code true} if the test class has any fields annotated with {@code Rule} whose type is
* {@link Timeout}.
*/
static boolean hasTimeoutRule(TestClass testClass) {
// Many protected convenience methods in BlockJUnit4ClassRunner that are available in JUnit 4.11
// such as getTestRules(Object) were not public until
// https://github.com/junit-team/junit/commit/8782efa08abf5d47afdc16740678661443706740,
// which appears to be JUnit 4.9. Because we allow users to use JUnit 4.7, we need to include a
// custom implementation that is backwards compatible to JUnit 4.7.
List<FrameworkField> fields = testClass.getAnnotatedFields(Rule.class);
for (FrameworkField field : fields) {
if (field.getField().getType().equals(Timeout.class)) {
return true;
}
}
return false;
}
示例13: get_all_classes_by_LocationProvider
import org.junit.Rule; //导入依赖的package包/类
@Test
public void get_all_classes_by_LocationProvider() {
JavaClasses classes = cache.getClassesToAnalyzeFor(TestClassWithLocationProviders.class);
assertThatClasses(classes).contain(String.class, Rule.class, getClass());
classes = cache.getClassesToAnalyzeFor(TestClassWithLocationProviderUsingTestClass.class);
assertThatClasses(classes).contain(String.class);
assertThatClasses(classes).dontContain(getClass());
}
示例14: imports_the_classpath
import org.junit.Rule; //导入依赖的package包/类
@Test
public void imports_the_classpath() {
JavaClasses classes = new ClassFileImporter().importClasspath();
assertThatClasses(classes).contain(ClassFileImporter.class, getClass());
assertThatClasses(classes).dontContain(Rule.class); // Default doesn't import jars
classes = new ClassFileImporter().importClasspath(new ImportOptions());
assertThatClasses(classes).contain(ClassFileImporter.class, getClass(), Rule.class);
}
示例15: imports_packages
import org.junit.Rule; //导入依赖的package包/类
@Test
public void imports_packages() {
JavaClasses classes = new ClassFileImporter().importPackages(
getClass().getPackage().getName(), Rule.class.getPackage().getName());
assertThatClasses(classes).contain(ImmutableSet.of(getClass(), Rule.class));
classes = new ClassFileImporter().importPackages(
ImmutableSet.of(getClass().getPackage().getName(), Rule.class.getPackage().getName()));
assertThatClasses(classes).contain(ImmutableSet.of(getClass(), Rule.class));
}