本文整理汇总了Java中org.springframework.test.context.TestContext.getTestClass方法的典型用法代码示例。如果您正苦于以下问题:Java TestContext.getTestClass方法的具体用法?Java TestContext.getTestClass怎么用?Java TestContext.getTestClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.test.context.TestContext
的用法示例。
在下文中一共展示了TestContext.getTestClass方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initDatabase
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* 初始化数据源
*
* @param testContext
*/
private void initDatabase(TestContext testContext) {
SqlConfig.Database database = null;
Class<?> testClass = testContext.getTestClass();
SqlConfig sqlConfigInClass = testClass.getAnnotation(SqlConfig.class);
if (sqlConfigInClass != null) {
database = sqlConfigInClass.database();
}
Method method = testContext.getTestMethod();
SqlConfig sqlConfigInMethod = method.getAnnotation(SqlConfig.class);
if (sqlConfigInMethod != null) {
database = sqlConfigInMethod.database();
}
if (database != null) {
UnitTestDataSource.selectDataSource(database);
}
}
示例2: initPath
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
private void initPath(TestContext testContext) {
if (System.getProperties().keySet().contains(ParamsHome.JVM_PARAM_PATH)) {
return;
}
Class<?> clazz = testContext.getTestClass();
ParamsPath paramsPath;
if (clazz.isAnnotationPresent(ParamsPath.class)) {
paramsPath = clazz.getAnnotation(ParamsPath.class);
} else {
return;
}
String path = paramsPath.value();
System.setProperty(ParamsHome.JVM_PARAM_PATH, path);
}
示例3: isDefaultRollback
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* Determine whether or not to rollback transactions by default for the
* supplied {@linkplain TestContext test context}.
* <p>Supports {@link Rollback @Rollback} or
* {@link TransactionConfiguration @TransactionConfiguration} at the
* class-level.
* @param testContext the test context for which the default rollback flag
* should be retrieved
* @return the <em>default rollback</em> flag for the supplied test context
* @throws Exception if an error occurs while determining the default rollback flag
*/
@SuppressWarnings("deprecation")
protected final boolean isDefaultRollback(TestContext testContext) throws Exception {
Class<?> testClass = testContext.getTestClass();
Rollback rollback = findAnnotation(testClass, Rollback.class);
boolean rollbackPresent = (rollback != null);
TransactionConfigurationAttributes txConfigAttributes = retrieveConfigurationAttributes(testContext);
if (rollbackPresent && txConfigAttributes != defaultTxConfigAttributes) {
throw new IllegalStateException(String.format("Test class [%s] is annotated with both @Rollback "
+ "and @TransactionConfiguration, but only one is permitted.", testClass.getName()));
}
if (rollbackPresent) {
boolean defaultRollback = rollback.value();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Retrieved default @Rollback(%s) for test class [%s].", defaultRollback,
testClass.getName()));
}
return defaultRollback;
}
// else
return txConfigAttributes.isDefaultRollback();
}
示例4: retrieveConfigurationAttributes
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* Retrieve the {@link TransactionConfigurationAttributes} for the
* supplied {@link TestContext} whose {@linkplain Class test class}
* may optionally declare or inherit
* {@link TransactionConfiguration @TransactionConfiguration}.
* <p>If {@code @TransactionConfiguration} is not present for the
* supplied {@code TestContext}, a default instance of
* {@code TransactionConfigurationAttributes} will be used instead.
* @param testContext the test context for which the configuration
* attributes should be retrieved
* @return the TransactionConfigurationAttributes instance for this listener,
* potentially cached
* @see TransactionConfigurationAttributes#TransactionConfigurationAttributes()
*/
@SuppressWarnings("deprecation")
TransactionConfigurationAttributes retrieveConfigurationAttributes(TestContext testContext) {
if (this.configurationAttributes == null) {
Class<?> clazz = testContext.getTestClass();
TransactionConfiguration txConfig = AnnotatedElementUtils.findMergedAnnotation(clazz,
TransactionConfiguration.class);
if (logger.isDebugEnabled()) {
logger.debug(String.format("Retrieved @TransactionConfiguration [%s] for test class [%s].",
txConfig, clazz.getName()));
}
TransactionConfigurationAttributes configAttributes = (txConfig == null ? defaultTxConfigAttributes
: new TransactionConfigurationAttributes(txConfig.transactionManager(), txConfig.defaultRollback()));
if (logger.isDebugEnabled()) {
logger.debug(String.format("Using TransactionConfigurationAttributes %s for test class [%s].",
configAttributes, clazz.getName()));
}
this.configurationAttributes = configAttributes;
}
return this.configurationAttributes;
}
示例5: beforeOrAfterTestClass
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* Perform the actual work for {@link #beforeTestClass} and {@link #afterTestClass}
* by dirtying the context if appropriate (i.e., according to the required mode).
* @param testContext the test context whose application context should
* potentially be marked as dirty; never {@code null}
* @param requiredClassMode the class mode required for a context to
* be marked dirty in the current phase; never {@code null}
* @throws Exception allows any exception to propagate
* @since 4.2
* @see #dirtyContext
*/
protected void beforeOrAfterTestClass(TestContext testContext, ClassMode requiredClassMode) throws Exception {
Assert.notNull(testContext, "TestContext must not be null");
Assert.notNull(requiredClassMode, "requiredClassMode must not be null");
Class<?> testClass = testContext.getTestClass();
Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");
DirtiesContext dirtiesContext = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class);
boolean classAnnotated = (dirtiesContext != null);
ClassMode classMode = (classAnnotated ? dirtiesContext.classMode() : null);
if (logger.isDebugEnabled()) {
String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After");
logger.debug(String.format(
"%s test class: context %s, class annotated with @DirtiesContext [%s] with mode [%s].", phase,
testContext, classAnnotated, classMode));
}
if (classMode == requiredClassMode) {
dirtyContext(testContext, dirtiesContext.hierarchyMode());
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:34,代码来源:AbstractDirtiesContextTestExecutionListener.java
示例6: beforeTestMethod
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
final String uuid = testCases.get();
final Class<?> testClass = testContext.getTestClass();
final Method testMethod = testContext.getTestMethod();
final String id = getHistoryId(testClass, testMethod);
final TestResult result = new TestResult()
.withUuid(uuid)
.withHistoryId(id)
.withName(testMethod.getName())
.withFullName(String.format("%s.%s", testClass.getCanonicalName(), testMethod.getName()))
.withLinks(getLinks(testClass, testMethod))
.withLabels(
new Label().withName("package").withValue(testClass.getCanonicalName()),
new Label().withName("testClass").withValue(testClass.getCanonicalName()),
new Label().withName("testMethod").withValue(testMethod.getName()),
new Label().withName("suite").withValue(testClass.getName()),
new Label().withName("host").withValue(getHostName()),
new Label().withName("thread").withValue(getThreadName())
);
result.getLabels().addAll(getLabels(testClass, testMethod));
getDisplayName(testMethod).ifPresent(result::setName);
getLifecycle().scheduleTestCase(result);
getLifecycle().startTestCase(uuid);
}
示例7: initTestData
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
private void initTestData(TestContext testContext) {
List<String> sqlFiles = new ArrayList<String>();
/**
* 读取测试类指定的sql文件
*/
Class<?> testClass = testContext.getTestClass();
SqlConfig sqlConfigInClass = testClass.getAnnotation(SqlConfig.class);
if (sqlConfigInClass != null) {
String[] sqlFilesInClass = sqlConfigInClass.sqlFiles();
if (ArrayUtils.isNotEmpty(sqlFilesInClass)) {
sqlFiles.addAll(Arrays.asList(sqlFilesInClass));
}
}
/**
* 读取测试方法指定的sql文件
*/
Method method = testContext.getTestMethod();
SqlConfig sqlConfigInMethod = method.getAnnotation(SqlConfig.class);
if (sqlConfigInMethod != null) {
String[] sqlFilesInMethod = sqlConfigInMethod.sqlFiles();
if (ArrayUtils.isNotEmpty(sqlFilesInMethod)) {
sqlFiles.addAll(Arrays.asList(sqlFilesInMethod));
}
}
/**
* 执行sql
*/
for (String sqlFile : sqlFiles) {
LOGGER.info(String.format("execute sql file [%s]", sqlFile));
this.executeSqlScript(testContext, sqlFile, false);
}
}
示例8: beforeTestMethod
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* If the test method of the supplied {@linkplain TestContext test context}
* is configured to run within a transaction, this method will run
* {@link BeforeTransaction @BeforeTransaction} methods and start a new
* transaction.
* <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
* {@code @BeforeTransaction} methods will not be invoked, and a transaction
* will not be started.
* @see org.springframework.transaction.annotation.Transactional
* @see #getTransactionManager(TestContext, String)
*/
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
final Method testMethod = testContext.getTestMethod();
final Class<?> testClass = testContext.getTestClass();
Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");
TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
if (txContext != null) {
throw new IllegalStateException("Cannot start a new transaction without ending the existing transaction.");
}
PlatformTransactionManager tm = null;
TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);
if (transactionAttribute != null) {
transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
transactionAttribute);
if (logger.isDebugEnabled()) {
logger.debug("Explicit transaction definition [" + transactionAttribute + "] found for test context "
+ testContext);
}
if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
return;
}
tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
}
if (tm != null) {
txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
runBeforeTransactionMethods(testContext);
txContext.startTransaction();
TransactionContextHolder.setCurrentTransactionContext(txContext);
}
}
示例9: detectDefaultScript
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* Detect a default SQL script by implementing the algorithm defined in
* {@link Sql#scripts}.
*/
private String detectDefaultScript(TestContext testContext, boolean classLevel) {
Class<?> clazz = testContext.getTestClass();
Method method = testContext.getTestMethod();
String elementType = (classLevel ? "class" : "method");
String elementName = (classLevel ? clazz.getName() : method.toString());
String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName());
if (!classLevel) {
resourcePath += "." + method.getName();
}
resourcePath += ".sql";
String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
ClassPathResource classPathResource = new ClassPathResource(resourcePath);
if (classPathResource.exists()) {
if (logger.isInfoEnabled()) {
logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]", prefixedResourcePath,
elementType, elementName));
}
return prefixedResourcePath;
}
else {
String msg = String.format("Could not detect default SQL script for test %s [%s]: "
+ "%s does not exist. Either declare statements or scripts via @Sql or make the "
+ "default SQL script available.", elementType, elementName, classPathResource);
logger.error(msg);
throw new IllegalStateException(msg);
}
}
示例10: beforeOrAfterTestMethod
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod}
* by dirtying the context if appropriate (i.e., according to the required modes).
* @param testContext the test context whose application context should
* potentially be marked as dirty; never {@code null}
* @param requiredMethodMode the method mode required for a context to
* be marked dirty in the current phase; never {@code null}
* @param requiredClassMode the class mode required for a context to
* be marked dirty in the current phase; never {@code null}
* @throws Exception allows any exception to propagate
* @since 4.2
* @see #dirtyContext
*/
protected void beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode,
ClassMode requiredClassMode) throws Exception {
Assert.notNull(testContext, "TestContext must not be null");
Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null");
Assert.notNull(requiredClassMode, "requiredClassMode must not be null");
Class<?> testClass = testContext.getTestClass();
Method testMethod = testContext.getTestMethod();
Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");
Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");
DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class);
DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class);
boolean methodAnnotated = (methodAnn != null);
boolean classAnnotated = (classAnn != null);
MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null);
ClassMode classMode = (classAnnotated ? classAnn.classMode() : null);
if (logger.isDebugEnabled()) {
String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After");
logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] "
+ "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext,
classAnnotated, classMode, methodAnnotated, methodMode));
}
if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) {
HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode());
dirtyContext(testContext, hierarchyMode);
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:45,代码来源:AbstractDirtiesContextTestExecutionListener.java
示例11: beforeTestClass
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
Class testClass = testContext.getTestClass();
Logger logger = LoggerFactory.getLogger(testClass);
logger.info("******** Starting test_class=" + testClass.getName());
super.beforeTestClass(testContext);
}
示例12: afterTestClass
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void afterTestClass(TestContext testContext) throws Exception {
Class testClass = testContext.getTestClass();
Logger logger = LoggerFactory.getLogger(testClass);
logger.info("******** Shutting down test_class=" + testClass.getName());
super.afterTestClass(testContext);
}
示例13: prepareTestInstance
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
Class<?> testClass = testContext.getTestClass();
AnnotationAttributes annotationAttributes = AnnotatedElementUtils
.getMergedAnnotationAttributes(testClass,
IntegrationTest.class.getName());
if (annotationAttributes != null) {
addPropertySourceProperties(testContext,
annotationAttributes.getStringArray("value"));
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:12,代码来源:IntegrationTestPropertiesListener.java
示例14: beforeTestMethod
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
/**
* Build a list of mock/test users for the provided TestContext if not present. If
* present, validate that the user set in the SecurityContext matches the expected User based
* on the current iteration of the TestContext. If the actual user does not match expected,
* then a test failure is raised.
* @param testContext The TestContext being executed
* @throws Exception if an Exception occurs
*/
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
testClass = testContext.getTestClass();
String methodMapKey = testClass.getName() + ":" + testContext.getTestMethod().getName();
TestMethodContext context = methodContextMap.get(methodMapKey);
if (context == null) {
context = new TestMethodContext(testContext.getTestMethod().getName(), findUserAnnotations(testContext),testContext.getApplicationContext());
methodContextMap.put(methodMapKey, context);
}
boolean pop = false;
User expectedUser = null;
User user = null;
if (SecurityContextHolder.getContext().getAuthentication() != null) {
user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
if (context.getExecutionStack().size() > 0) {
expectedUser = context.getExecutionStack().pop();
}
if (user != null || expectedUser != null) {
if (user != null && !user.equals(expectedUser)) {
throw new AssertionFailedError("Invalid SecurityUser, actual=" + user + ", expected=" + expectedUser);
}
}
}
开发者ID:Mastercard,项目名称:java8-spring-security-test,代码行数:36,代码来源:WatchWithUserTestExecutionListener.java
示例15: prepareTestInstance
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
LOG.info("@RunWith(CamelSpringBootJUnit4ClassRunner.class) preparing: {}", testContext.getTestClass());
Class<?> testClass = testContext.getTestClass();
ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext();
// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
CamelAnnotationsHandler.handleMockEndpointsAndSkip(context, testClass);
CamelAnnotationsHandler.handleUseOverridePropertiesWithPropertiesComponent(context, testClass);
}