junit

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 use JUnit to test asynchronous processes

天涯浪子 提交于 2019-12-17 06:58:18
问题 How do you test methods that fire asynchronous processes with JUnit? I don't know how to make my test wait for the process to end (it is not exactly a unit test, it is more like an integration test as it involves several classes and not just one). 回答1: IMHO it's bad practice to have unit tests create or wait on threads, etc. You'd like these tests to run in split seconds. That's why I'd like to propose a 2-step approach to testing async processes. Test that your async process is submitted

How to avoid multiple asserts in a JUnit test?

馋奶兔 提交于 2019-12-17 06:53:55
问题 I have a DTO which I'm populating from the request object, and the request object has many fields. I want to write a test to check if the populateDTO() method is putting values in the right places or not. If I follow the rule of one assert per test, I would have to write a large number of tests, to test each field. The other approach would be to write multiple asserts in a single test. Is it really recommended to follow one assert per test rule or can we relax in these cases. How do I

PowerMock ECLEmma coverage issue

女生的网名这么多〃 提交于 2019-12-17 06:47:19
问题 We are using EasyMock and PowerMock with JUnit. The coverage tool used is ECLEmma. With EasyMock, it shows the coverage properly in green (as covered). However, for the code that is unit tested with PowerMock, the coverage is shown in red (uncovered). Have read similar questions on the web. However, just wanted to check if there is a solution for this. Thanks Venkatesh 回答1: It's a known problem : https://github.com/jayway/powermock/issues/422 And it has been for a long time, it won't be fixed

How to set JVM parameters for Junit Unit Tests?

落花浮王杯 提交于 2019-12-17 06:25:09
问题 I have some Junit unit tests that require a large amount of heap-space to run - i.e. 1G. (They test memory-intensive functionality for a webstart app that will only run with sufficient heap-space, and will be run internally on Win 7 64-bit machines - so redesigning the tests isn't a practical suggestion.) I am developing in Intellij IDEA, so I know I can set the JVM parameters (e.g. -Xmx1024M) for the test class. However, this is only for running the whole test class - if I want to run an

annotation to make a private method public only for test classes [duplicate]

元气小坏坏 提交于 2019-12-17 05:52:07
问题 This question already has answers here : How do I test a private function or a class that has private methods, fields or inner classes? (51 answers) Closed 2 years ago . Who has a solution for that common need. I have a class in my application. some methods are public, as they are part of the api, and some are private, as they for internal use of making the internal flow more readable now, say I want to write a unit test, or more like an integration test, which will be located in a different

Configuring IntelliJ IDEA for unit testing with JUnit

大城市里の小女人 提交于 2019-12-17 05:33:52
问题 I decided to try out IntelliJ this morning via the trial version and installed the JUnit plugin. I made a new Java project and I want to write a test case for it. How do I add the junit.jar file to my project? (I actually want to add it to every java project, now and forever more - is there a way of doing that?). 回答1: Press Ctrl + Shift + T in the code editor. It will show you popup with suggestion to create a test. Mac OS: ⌘ Cmd + Shift + T 回答2: If you already have a test class, but missing

Java Maven 项目配置 JUnit

依然范特西╮ 提交于 2019-12-17 04:29:55
JUnit4:单元测试用 目录 1. IDEA 下载插件 2. 修改下载的插件的配置 3. maven 项目添加依赖 4. 生成测试文件 1. IDEA 下载插件 下载插件 2. 修改插件配置 (1)Output Path 修改为:${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME} (2)Default Template 修改为:JUnit 4 (3)把 JUnit 4 文件中的 package $entry.packageName; 前面的 test 去掉 3. maven 项目添加依赖 在模块的 pom.xml 文件中田间依赖: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> 4. 生成测试文件 在 src 目录下新建 test/java 目录,并将 java 文件夹设置为 Test Source Root 在要测试的文件按快捷键:command + shift + t 测试方法必须用@Test修饰 测试方法必须用 public void 修饰,不能带参数 每个测试方法必须可以单独测试,各方法间不要有依赖。 注解: @Test:讲一个普通方法修饰为测试方法

Rollback transaction after @Test

混江龙づ霸主 提交于 2019-12-17 04:21:36
问题 First of all, I've found a lot of threads on StackOverflow about this, but none of them really helped me, so sorry to ask possibly duplicate question. I'm running JUnit tests using spring-test, my code looks like this @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {}) public class StudentSystemTest { @Autowired private StudentSystem studentSystem; @Before public void initTest() { // set up the database, create basic structure for testing } @Test public void test1()

Easy way of running the same junit test over and over?

南笙酒味 提交于 2019-12-17 03:28:27
问题 Like the title says, I'm looking for some simple way to run JUnit 4.x tests several times in a row automatically using Eclipse. An example would be running the same test 10 times in a row and reporting back the result. We already have a complex way of doing this but I'm looking for a simple way of doing it so that I can be sorta sure that the flaky test I've been trying to fix stays fixed. An ideal solution would be an Eclipse plugin/setting/feature that I am unaware of. 回答1: The easiest (as