junit-rule

Log4j appender for debug output if test fails

╄→гoц情女王★ 提交于 2021-02-19 01:35:15
问题 I would like to set up my test-logging so that log output is mainly suppressed - unless a test fails. then I would like to have the debug output. I know the manual solution of just modifying the log4j.properties in the test classpath or just having debug logging always active for tests - but I don't want to spam the console with unnessesary output for green tests. For failing tests I want the automatic build to give me the debug output in case there are some weired environment issues going on

Use Spring Data random (embedded) Mongo port with NoSQL JUnit @Rule

天涯浪子 提交于 2021-01-28 04:15:59
问题 I'm currently trying to write an Integration test class which uses Spring Data Mongo repositories. I use an embedded Mongo instance provided by the de.flapdoodle.embed.mongo dependency. Spring Data documentation specifies that we only have to put this dependency in the project and the EmbedMongoAutoConfiguration takes care of the rest. For now, that's ok, and setting the port to 0 makes the auto configuration process to find a free port to launch the mongo instance on. This feature is

How to replace @Rule annotation in Junit 5?

你离开我真会死。 提交于 2020-01-24 02:01:06
问题 I'm using wiremock in my tests and have such a line of code: @Rule public WireMockRule wireMockRule = new WireMockRule(8080); I want to switch to Junit 5. So I added the next dependency (using gradle): testCompile('org.junit.jupiter:junit-jupiter-engine:5.1.1') But there no suggestions when I'm trying to import @Rule annotation. Do I need to add another module of junit dependency? Or rules are not supported in Junit5? If not how can I replace @Rule annotation to make tests work again? Thanks.

Mockito verify after exception Junit 4.10

徘徊边缘 提交于 2019-12-17 16:24:15
问题 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

How does Junit @Rule work?

只谈情不闲聊 提交于 2019-12-17 07:00:10
问题 I want to write test cases for a bulk of code, I would like to know details of JUnit @Rule annotation feature, so that I can use it for writing test cases. Please provide some good answers or links, which give detailed description of its functionality through a simple example. 回答1: Rules are used to add additional functionality which applies to all tests within a test class, but in a more generic way. For instance, ExternalResource executes code before and after a test method, without having

How to apply a Hamcrest matcher to the property of a class under test?

随声附和 提交于 2019-12-12 18:33:05
问题 Is there a way to build a combined Hamcrest matcher which tests an object and the property of this object? - pseudo code: both( instanceof(MultipleFailureException.class) ).and( // pseudo code starts adapt( new Adapter<MultipleFailureException, Iterable<Throwable>() { public Iterable<Throwable> getAdapter(MultipleFailureException item) { return item.getFailures(); } }, // pseudo code ends everyItem(instanceOf(IllegalArgumentException.class)) ) ) Background: I have a JUnit test, which iterates

JUnit Rule not being executed when test fails

大憨熊 提交于 2019-12-11 06:46:20
问题 Following is my test file : package test; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestWatcher; import org.junit.runner.Description; public class TestWatcherTest { @Rule public TestWatcher testWatcher = new TestWatcher() { protected void failed(Throwable e, Description description) { System.out.println("in failed()"); } }; @Test public void shouldFail() { Assert.assertTrue("Test failed", false); } } Output is : <failure message="Test failed

What is the logic of @Rule(JUnit) declaration and assignment in a Groovy class

穿精又带淫゛_ 提交于 2019-12-11 06:32:36
问题 Trying some variants of rules creation in a groovy file, I have come to the thought, that @Rule doesn't describe DECLARATION, but ASSIGNMENT. So, the runner, when loading the test, tries every rule for the correct assignment. //Correct variants: @Rule public ErrorCollector collector1= new ErrorCollector(); public ErrorCollector collector2= null; @Rule collector2= new ErrorCollector(); public ErrorCollector collector3; @Rule collector3= new ErrorCollector(); // incorrect variants: @Rule public

How can I automatically skip certain JUnit tests based on a condition?

吃可爱长大的小学妹 提交于 2019-12-10 14:13:19
问题 I would like a simple way to assign a priority value to my JUnit tests such that I can say 'run only priority 1 tests', 'run priority 1, 2 and 3 tests' etc. I am aware that I could just include a line like Assume.assumeTrue("Test skipped for priority " + priority, priority <= 2); at the start of each test (where priority is the highest value priority tests I want to run and 2 is the priority value of this particular test), however copy-pasting a line at the start of each test doesn't seem a

Test the error code of a custom exception with JUnit 4

非 Y 不嫁゛ 提交于 2019-12-06 23:24:01
问题 I would like to test the return code of an exception. Here is my production code: class A { try { something... } catch (Exception e) { throw new MyExceptionClass(INTERNAL_ERROR_CODE, e); } } And the corresponding exception: class MyExceptionClass extends ... { private errorCode; public MyExceptionClass(int errorCode){ this.errorCode = errorCode; } public getErrorCode(){ return this.errorCode; } } My unit test: public class AUnitTests{ @Rule public ExpectedException thrown= ExpectedException