junit-rule

check errorcode with @rule in junit

坚强是说给别人听的谎言 提交于 2019-12-06 08:11:31
问题 I found @Rule annotation in jUnit for better handling of exception. Is there a way to check error code ? Currently my code looks like (without @Rule): @Test public void checkNullObject() { MyClass myClass= null; try { MyCustomClass.get(null); // it throws custom exception when null is passed } catch (CustomException e) { // error code is error.reason.null Assert.assertSame("error.reason.null", e.getInformationCode()); } } But with use of @Rule , I am doing following : @Rule public

CdiUnit test with Junit @Rule is impossible because of a public private field paradox

余生长醉 提交于 2019-12-06 01:45:50
问题 The following snippet is enough to reproduce my problem: Either I set the thrown attribute public and get the error org.jboss.weld.exceptions.DefinitionException: WELD-000075: Normal scoped managed bean implementation class has a public field Or I remove the public modifier and get the error org.junit.internal.runners.rules.ValidationError: The @Rule 'thrown' must be public. I also tried to let the public modifier in place and to add the @Dependent annotation scope on the class, but got error

Is it possible to mock a static method on a final class using a PowerMockRule instead of the PowerMockRunner?

女生的网名这么多〃 提交于 2019-12-04 20:59:33
问题 According to the PowerMock docs, I should be able to run using a PowerMockRule instead of @RunWith(PowerMockRunner.class) and get the same results. I seem to have found a case where this isn't true. The below sample runs fine: package com.test.powermockstatics; import static org.junit.Assert.assertEquals; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; import org.junit.Test; import org.junit.runner.RunWith; import org

Best way of logging exceptions when tests fail (e.g. using a junit rule)

放肆的年华 提交于 2019-12-01 03:32:44
问题 When I'm running a complete test suite, it would be helpful if exceptions that caused a test to fail would appear in my (SLF4J-)log. What is the best method to achieve this? What I would like is a junit4 rule that handles exception logging for me. The code @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; } } } of course does not work, since I can

How to continue test after JUnit ExpectedException if thrown?

末鹿安然 提交于 2019-11-30 22:08:28
问题 I have set up some JUnit (4.12) test with the ExpectedException feature, and I would like the test to continue after the expected exception. But I never see the log '3', as the execution seems to stop after the exception, event if catch? Is this actually possible, and how? @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testUserAlreadyExists() throws Exception { log.info("1"); // Create some users userService.createUser("toto1"); userService.createUser(

Why @Rule annotated fields in JUnit has to be public?

徘徊边缘 提交于 2019-11-28 13:15:38
In JUnit test case, a field annotated by @Rule must be public. It breaks a common Java coding convention (all class member variables should not be public). Why does JUnit require this? Documentation for @Rule : https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/Rule.java The JUnit runner will need to access the field reflectively to run the rule. If the field was private the access would throw IllegalAccessException . Another option would have been to have the runner modify the access from private to public before running the rule. However that could cause problems in case

Can I apply a time limit for all the tests in the suite

☆樱花仙子☆ 提交于 2019-11-28 08:12:01
问题 Is there a way in JUnit to define a global timeout/limit for all the tests included in the suite. Well, let me to explain this a little. I want to introduce a junit test suite into the build system of a legacy project that would be run on every build. The suite should consist of fast running unit tests, but the project has mixed set of integration and unit tests in it's tests source folder. So what I am aiming for is introducing a central test suite for unit tests that only contains those

Mockito verify after exception Junit 4.10

柔情痞子 提交于 2019-11-27 23:24:30
I am testing a method with an expected exception. I also need to verify that some cleanup code was called (on a mocked object) after the exception is thrown, but it looks like that verification is being ignored. Here is the code. I am using the Junit ExpectedException Rule to verify the expected exception. @Rule public ExpectedException expectedEx = ExpectedException.none(); @Test public void testExpectedException() { MockedObject mockObj = mock(MockedObj.class); MySubject subject = new MySubject(mockedObj); expectedEx.expect(MyException.class); expectedEx.expectMessage("My exception message."

Apply '@Rule' after each '@Test' and before each '@After' in JUnit

爱⌒轻易说出口 提交于 2019-11-27 22:50:57
I have a test suite where I am logging out of the system in @After and closing the browser in @AfterClass . I am trying to use @Rule to take failed test screenshot using Selenium for every test method. I checked manually that @Rule only runs before every @Before but I want to set it up after @Test and before @After . I couldn't find out simple solution. Any help will be appreciated. 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

Why @Rule annotated fields in JUnit has to be public?

余生长醉 提交于 2019-11-27 07:43:09
问题 In JUnit test case, a field annotated by @Rule must be public. It breaks a common Java coding convention (all class member variables should not be public). Why does JUnit require this? Documentation for @Rule : https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/Rule.java 回答1: The JUnit runner will need to access the field reflectively to run the rule. If the field was private the access would throw IllegalAccessException . Another option would have been to have the runner