org.springframework.test.context.TestContext.getTestMethod()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.2k)|赞(0)|评价(0)|浏览(89)

本文整理了Java中org.springframework.test.context.TestContext.getTestMethod()方法的一些代码示例,展示了TestContext.getTestMethod()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TestContext.getTestMethod()方法的具体详情如下:
包路径:org.springframework.test.context.TestContext
类名称:TestContext
方法名:getTestMethod

TestContext.getTestMethod介绍

[英]Get the current Method for this test context.

Note: this is a mutable property.
[中]获取此测试上下文的当前方法。
注意:这是一个可变属性。

代码示例

代码示例来源:origin: spring-projects/spring-framework

public TestContextTransactionAttribute(TransactionAttribute targetAttribute, TestContext testContext) {
  super(targetAttribute);
  this.name = ClassUtils.getQualifiedMethodName(testContext.getTestMethod(), testContext.getTestClass());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
  String name = testContext.getTestMethod().getName();
  actualMethods.add(name);
  testContext.setAttribute("method", name);
  this.methodName.set(name);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void missingValueAndScriptsAndStatementsAtMethodLevel() throws Exception {
  Class<?> clazz = MissingValueAndScriptsAndStatementsAtMethodLevel.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
  assertExceptionContains(clazz.getSimpleName() + ".foo" + ".sql");
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void missingValueAndScriptsAndStatementsAtClassLevel() throws Exception {
  Class<?> clazz = MissingValueAndScriptsAndStatementsAtClassLevel.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
  assertExceptionContains(clazz.getSimpleName() + ".sql");
}

代码示例来源:origin: spring-projects/spring-framework

private void assertIsRollback(Class<?> clazz, boolean rollback) throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("test"));
  assertEquals(rollback, listener.isRollback(testContext));
}

代码示例来源:origin: spring-projects/spring-framework

private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  Invocable instance = BeanUtils.instantiateClass(clazz);
  given(testContext.getTestInstance()).willReturn(instance);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
  assertFalse("callback should not have been invoked", instance.invoked());
  TransactionContextHolder.removeCurrentTransactionContext();
  listener.beforeTestMethod(testContext);
  assertFalse("callback should not have been invoked", instance.invoked());
}

代码示例来源:origin: spring-projects/spring-framework

private void assertBeforeTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz, boolean invokedInTx)
    throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  Invocable instance = BeanUtils.instantiateClass(clazz);
  given(testContext.getTestInstance()).willReturn(instance);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
  assertFalse("callback should not have been invoked", instance.invoked());
  TransactionContextHolder.removeCurrentTransactionContext();
  listener.beforeTestMethod(testContext);
  assertEquals(invokedInTx, instance.invoked());
}

代码示例来源:origin: spring-projects/spring-framework

private void assertAfterTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  Invocable instance = BeanUtils.instantiateClass(clazz);
  given(testContext.getTestInstance()).willReturn(instance);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest"));
  assertFalse("callback should not have been invoked", instance.invoked());
  TransactionContextHolder.removeCurrentTransactionContext();
  listener.beforeTestMethod(testContext);
  listener.afterTestMethod(testContext);
  assertFalse("callback should not have been invoked", instance.invoked());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnMethodWithBeforeMethodMode() throws Exception {
  Class<?> clazz = getClass();
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(
    clazz.getDeclaredMethod("dirtiesContextDeclaredLocallyWithBeforeMethodMode"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnMethodWithAfterMethodMode() throws Exception {
  Class<?> clazz = getClass();
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(
    clazz.getDeclaredMethod("dirtiesContextDeclaredLocallyWithAfterMethodMode"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnClassAfterClass() throws Exception {
  Class<?> clazz = DirtiesContextDeclaredLocallyAfterClass.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnClassBeforeClass() throws Exception {
  Class<?> clazz = DirtiesContextDeclaredLocallyBeforeClass.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterClass() throws Exception {
  Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void missingDataSourceAndTxMgr() throws Exception {
  ApplicationContext ctx = mock(ApplicationContext.class);
  given(ctx.getResource(anyString())).willReturn(mock(Resource.class));
  given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class));
  Class<?> clazz = MissingDataSourceAndTxMgr.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
  given(testContext.getApplicationContext()).willReturn(ctx);
  assertExceptionContains("supply at least a DataSource or PlatformTransactionManager");
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void isolatedTxModeDeclaredWithoutTxMgr() throws Exception {
  ApplicationContext ctx = mock(ApplicationContext.class);
  given(ctx.getResource(anyString())).willReturn(mock(Resource.class));
  given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class));
  Class<?> clazz = IsolatedWithoutTxMgr.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
  given(testContext.getApplicationContext()).willReturn(ctx);
  assertExceptionContains("cannot execute SQL scripts using Transaction Mode [ISOLATED] without a PlatformTransactionManager");
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnClassBeforeEachTestMethod() throws Exception {
  Class<?> clazz = DirtiesContextDeclaredLocallyBeforeEachTestMethod.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextViaMetaAnnotationWithOverrides() throws Exception {
  Class<?> clazz = DirtiesContextViaMetaAnnotationWithOverrides.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(CURRENT_LEVEL);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredLocallyOnClassAfterEachTestMethod() throws Exception {
  Class<?> clazz = DirtiesContextDeclaredLocallyAfterEachTestMethod.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}

代码示例来源:origin: spring-projects/spring-framework

private void assertAfterTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception {
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  Invocable instance = BeanUtils.instantiateClass(clazz);
  given(testContext.getTestInstance()).willReturn(instance);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
  given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus());
  assertFalse("callback should not have been invoked", instance.invoked());
  TransactionContextHolder.removeCurrentTransactionContext();
  listener.beforeTestMethod(testContext);
  assertFalse("callback should not have been invoked", instance.invoked());
  listener.afterTestMethod(testContext);
  assertTrue("callback should have been invoked", instance.invoked());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void beforeAndAfterTestMethodForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterEachTestMethod()
    throws Exception {
  Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterEachTestMethod.class;
  BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
  given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
  beforeListener.beforeTestMethod(testContext);
  afterListener.beforeTestMethod(testContext);
  verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
  afterListener.afterTestMethod(testContext);
  beforeListener.afterTestMethod(testContext);
  verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}

相关文章