Tag: junit rule

由于公共私人领域的矛盾,使用Junit @Rule的CdiUnit测试是不可能的

以下代码段足以重现我的问题: 我将thrown属性设置为public并获取错误org.jboss.weld.exceptions.DefinitionException: WELD-000075: Normal scoped managed bean implementation class has a public field 或者我删除public修饰符并获取错误org.junit.internal.runners.rules.ValidationError: The @Rule ‘thrown’ must be public. 我还试图让public修饰符到位,并在类上添加@Dependent注释范围,但得到错误org.jboss.weld.exceptions.DefinitionException: WELD-000046: At most one scope may be specified on [EnhancedAnnotatedTypeImpl] public @Dependent @ApplicationScoped @RunWith 我删除了所有不必要的代码,但这是一个非常复杂的unit testing,通过CDI进行模拟,服务注入,并且一些测试方法预计会引发exception。 import org.jglue.cdiunit.CdiRunner; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; @RunWith(CdiRunner.class) public class FooBarTest { @Rule public ExpectedException […]

测试失败时记录exception的最佳方法(例如使用junit规则)

当我运行一个完整的测试套件时,如果导致测试失败的exception会出现在我的(SLF4J-)日志中,将会很有帮助。 实现这一目标的最佳方法是什么? 我想要什么 是一个junit4规则,为我处理exception日志记录。 代码 @Rule public TestRule logException = new TestWatcher() { @Override public void failed(Description d) { catch (Exception e) { logger.error(“Test ({}) failed because of exception {}”, d, e); throw e; } } } 当然不起作用,因为我只能从try块中捕获exception。 是否有一种解决方法以某种方式以类似的简单和一般方式实现这一目标? 顺便说一下,我现在在做什么 在创建exception时记录exception。 但是在调用者和库之间的接口上记录exception会更好,所以在我的情况下在测试用例中。 在创建例外时不记录也可以保证在调用者决定记录它们时它们不会多次显示。

如果抛出JUnit ExpectedException后如何继续测试?

我已经使用ExpectedExceptionfunction设置了一些JUnit(4.12)测试,我希望测试在预期的exception之后继续。 但我从来没有看到日志’3’,因为执行似乎在exception后停止,如果捕获事件? 这实际上是可能的,怎么样? @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testUserAlreadyExists() throws Exception { log.info(“1”); // Create some users userService.createUser(“toto1”); userService.createUser(“toto2”); userService.createUser(“toto3”); Assert.assertTrue( userService.userExists(“toto1”) ); Assert.assertTrue( userService.userExists(“toto2”) ); Assert.assertTrue( userService.userExists(“toto3”) ); log.info(“2”); // Try to create an existing user exception.expect(AlreadyExistsException.class); userService.createUser(“toto1”); log.info(“3”); }

我可以为套件中的所有测试应用时间限制吗?

在JUnit中是否有一种方法可以为套件中包含的所有测试定义全局超时/限制。 好吧,让我来解释一下。 我想将junit测试套件引入到将在每个构建上运行的遗留项目的构建系统中。 该套件应包含快速运行的unit testing,但该项目在其测试源文件夹中具有混合的集成和unit testing集。 所以我的目标是为unit testing引入一个中央测试套件,它只包含那些能够在一定时限内运行的测试(每个测试单独进行)。 我想通过引入@Rule注释来强制执行该规则。 我试过这个: @RunWith(Suite.class) @SuiteClasses({…}) public class FastTestSuite{ @Rule Timeout timeout = new Timeout(1); } 但这似乎并没有成功。 我知道有一个事实,即测试的速度远低于1毫秒,但没有一个测试失败。

为什么JUnit中的@Rule注释字段必须是公共的?

在JUnit测试用例中, @Rule注释的@Rule必须是公共的。 它打破了常见的Java编码约定(所有类成员变量都不应该是公共的)。 为什么JUnit需要这个? @Rule文档: https : //github.com/junit-team/junit/blob/master/src/main/java/org/junit/Rule.java

在每个’@Test’之后和JUnit中的每个’@After’之前应用’@Rule’

我有一个测试套件,我在@After退出系统并在@AfterClass关闭浏览器。 我正在尝试使用@Rule为每种测试方法使用Selenium进行失败的测试截图。 我手动检查@Rule只在每个@Before之前运行,但是我想在@Test之后和@After之前设置它。 我找不到简单的解决方案。 任何帮助将不胜感激。 public class MorgatgeCalculatorTest { @Before public void before(){ System.out.println(“I am before”); } @BeforeClass public static void beforeclass(){ System.out.println(“I am beforeclass”); } @Test public void test(){ System.out.println(“I am Test”); } @Test public void test2(){ System.out.println(“I am Test2”); } @After public void after(){ System.out.println(“I am after”); } @AfterClass public static void afterclass(){ […]