- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一些名为 SqlCounter 的功能(测试监听器)。
它的目的是在测试执行期间对真实 SQL 查询进行计数。
如果此计数较大,则特殊环境属性 — 测试失败。
问题是:我的 @Before 方法中有一些逻辑,它也运行很多查询。我需要的是在所有“before” Hook 之后(就在测试方法执行开始之前)实际清除我的“SQL 计数器”。
但是我知道的所有方法(org.springframework.test.context.support.AbstractTestExecutionListener:beforeTestMethod、org.junit.rules.TestWatcher:starting、org.junit.rules.TestRule:apply)都在 JUnit 的 @Before 之前执行: (
请帮助我;)
更新:
我想不是显式地清除这个 SQL 计数器(在每个 @Before 中),而是在某个监听器中,它必须在 @Before 和 @Test 带注释的方法之间调用
最佳答案
JUnit 中 @Rule
/@Before
/@Test
注释序列的执行取决于 Runner 实现。假设 SpringJUnit4ClassRunner.methodBlock
或 BlockJUnit4ClassRunner.methodBlock
如下所示:
Statement statement = methodInvoker(frameworkMethod, testInstance);
statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement);
statement = withBefores(frameworkMethod, testInstance, statement);
...
statement = withRules...
基于此,我可以通过重写 methodInvoker
并添加新的 @RightBeforeTest
注释来提出以下实现
package info.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static org.junit.Assert.assertEquals;
@RunWith(JUnit4AnnotationsSequenceTest.CustomSpringJUnit4ClassRunner.class)
public class JUnit4AnnotationsSequenceTest
{
private String value = null;
@Before
public void setUp()
{
value = "@Before.setUp";
}
@RightBeforeTest
public void latestChance()
{
value = "@RightBeforeTest.latestChance";
}
@Test
public void rightBeforeTestAnnotationExecutesAfterBeforeAnnotation()
{
assertEquals("@RightBeforeTest.latestChance", value);
}
public static class CustomSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner
{
public CustomSpringJUnit4ClassRunner(final Class<?> clazz) throws InitializationError
{
super(clazz);
}
protected Statement methodInvoker(final FrameworkMethod method, final Object test)
{
return new RunBefores(
super.methodInvoker(method, test),
getTestClass().getAnnotatedMethods(RightBeforeTest.class),
test);
}
}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
public @interface RightBeforeTest {}
}
关于java - 使用 Spring 和 JUnit 在 @Before 和 @Test 执行之间注入(inject)逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019687/
我是一名优秀的程序员,十分优秀!