junit4

JUnit 4 @BeforeClass & @AfterClass when using Suites

依然范特西╮ 提交于 2019-11-27 11:55:46
When using this approach below, by setting up the jUnit with Suites. We got the problem when all @BeforeClass in every Testclass will be executed before any tests starts to execute. (For each n TestClass file the @BeforeClass runs, then after they have execute, it started to execute the first MyTest.class files @Test) This will cause that we allocate up much resources and memory. My thoughts was that it must be wrong, shouldn't each @BeforeClass run only before the actual testclass is executed, not when the Suite is started? @RunWith(Suite.class) @Suite.SuiteClasses({ MyTests.class, Mytests2

JUnit expected tag not working as expected

自古美人都是妖i 提交于 2019-11-27 11:32:30
问题 I have the following test case in eclipse, using JUnit 4 which is refusing to pass. What could be wrong? @Test(expected = IllegalArgumentException.class) public void testIAE() { throw new IllegalArgumentException(); } This exact testcase came about when trying to test my own code with the expected tag didn't work. I wanted to see if JUnit would pass the most basic test. It didn't. I've also tested with custom exceptions as expected without luck. Screenshot: 回答1: The problem is that your

Specifying an order to junit 4 tests at the Method level (not class level)

本秂侑毒 提交于 2019-11-27 11:07:13
I know this is bad practice, but it needs to be done, or I'll need to switch to testng . Is there a way, similar to JUnit 3's testSuite, to specify the order of the tests to be run in a class? Michael D If you're sure you really want to do this: There may be a better way, but this is all I could come up with... JUnit4 has an annotation: @RunWith which lets you override the default Runner for your tests. In your case you would want to create a special subclass of BlockJunit4ClassRunner , and override computeTestMethods() to return tests in the order you want them executed. For example, let's

setUp/tearDown (@Before/@After) why we need them in JUnit?

◇◆丶佛笑我妖孽 提交于 2019-11-27 11:05:24
I believe that we are all know that setUp (@Before) will execute before any test method and tearDown(@After) will execute after test method. Also we know that Junit will create one instance of Test per test method . my question is that can we just move setUp method content to class Constructor and remove setUp method? is there any specific reason to keep setUp method? This (old) JUnit best practices article puts it like this: Do not use the test-case constructor to set up a test case Setting up a test case in the constructor is not a good idea. Consider: public class SomeTest extends TestCase

What is the correct way to write to temp file during unit tests with Maven?

梦想与她 提交于 2019-11-27 10:41:18
问题 I have written a unit test that writes a file to the file-system, given no path it writes to the working directory; so if executed from the project directory it writes in the project root, if in the projects parent directory it writes to the parents root directory. So what is the correct way to write to the target directory? Quite possibly a directory inside the target directory? If I quite simply specify target/ with the file it will write to the parent projects target instead of the

How to execute JUnit and TestNG tests in same project using maven-surefire-plugin?

我的梦境 提交于 2019-11-27 10:29:03
问题 Right now I have both type of tests but when I say "mvn test" it only executes TestNG tests and not Junit. I want to execute both one after another. Any Idea ? 回答1: There is an open issue for this, so there's no elegant way to do this. It would be far simpler for you to pick a framework and stick with it. Edit: My previous answer doesn't work because you can't specify dependencies in the execution. I've tried a few approaches, but the best I can manage is to create a profile for the TestNG

How do I assert my exception message with JUnit Test annotation?

。_饼干妹妹 提交于 2019-11-27 08:57:26
问题 I have written a few JUnit tests with @Test annotation. If my test method throws a checked exception and if I want to assert the message along with the exception, is there a way to do so with JUnit @Test annotation? AFAIK, JUnit 4.7 doesn't provide this feature but does any future versions provide it? I know in .NET you can assert the message and the exception class. Looking for similar feature in the Java world. This is what I want: @Test (expected = RuntimeException.class, message =

Cleanup after all junit tests

时光怂恿深爱的人放手 提交于 2019-11-27 07:57:44
In my project I have to do some repository setup before all tests. This is done using some tricky static rules. However I've got no clue how to do clean up after all the tests. I don't want to keep some magic static number referring the number of all test methods, which I should maintain all the time. The most appreciated way is to add some listener which would be invoked after all the tests. Is there any interface for it already in JUnit4? edit: this has nothing to do with @BeforeClass and @AfterClass, cause I have to know if method annotated with @AfterClass is invoked for the last time.

Disable @EnableScheduling on Spring Tests

巧了我就是萌 提交于 2019-11-27 07:45:23
When I run my unit tests, it invokes my scheduled tasks. I want to prevent this behaviour, which is caused by the fact that I have @EnableScheduling on my main app configuration. How can I disable this on my unit tests? I have come across this question/answer which suggests setting up profiles? Not sure how I would go about that? or if its an overkill? I was thinking of having a separate AppConfiguration for my unit tests but it feels like im repeating code twice when I do that? @Configuration @EnableJpaRepositories(AppConfiguration.DAO_PACKAGE) @EnableTransactionManagement @EnableScheduling

How to run all JUnit tests in a category/suite with Ant?

丶灬走出姿态 提交于 2019-11-27 07:37:41
I'm using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer . To recap: public interface FastTests { } @RunWith(Categories.class) @Categories.IncludeCategory(FastTests.class) @Suite.SuiteClasses(AllTests.class) public class FastTestSuite { } @RunWith(ClasspathSuite.class) public class AllTests { } ...where AllTests makes use of the ClasspathSuite library. A test class that's part of the FastTests category would look like this: @Category(FastTests.class) public class StringUtilsTest { // ... } When I run "FastTestSuite" in my IDE, all tests with the