junit4

Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse

流过昼夜 提交于 2019-12-01 12:41:24
Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse It works fine when I am not extending from TestCase(jUnit), but my existing code extends from TestCase hence I would like to keep that as it is. Check that you are not using JUnit3 runner. Open 'Run Configuration' for you test and on Test tab make sure you have JUnit 4 selected for your test runner. IMHO, if you are retrofitting your test suite for JUnit4, just drop support for JUnit3. BTW, if you switch to 'JUnit 4' runner it will still execute old JUnit3 classes ( you don't have to add @Test

How to mock private methods using Whitebox(org.powermock.reflect)

久未见 提交于 2019-12-01 10:46:41
I want to mock a private method which has been called inside another method. Following is the sample code, I have written. Java code: package org.mockprivatemethods; public class AccountDeposit { // Instantiation of AccountDetails using some DI AccountDetails accountDetails; public long deposit(long accountNum, long amountDeposited){ long amount = 0; try{ amount = accountDetails.getAmount(accountNum); updateAccount(accountNum, amountDeposited); amount= amount + amountDeposited; } catch(Exception e){ // log exception } return amount; } private void updateAccount(long accountNum, long

Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse

我与影子孤独终老i 提交于 2019-12-01 09:51:44
问题 Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse It works fine when I am not extending from TestCase(jUnit), but my existing code extends from TestCase hence I would like to keep that as it is. 回答1: Check that you are not using JUnit3 runner. Open 'Run Configuration' for you test and on Test tab make sure you have JUnit 4 selected for your test runner. IMHO, if you are retrofitting your test suite for JUnit4, just drop support for JUnit3. BTW, if

How to parameterize junit Test Suite

廉价感情. 提交于 2019-12-01 08:59:19
Is it possible to parameterize a TestSuite in junit 4 ? For declaring a class as a test suite I need the annotation @RunWith(Suite.class) , but the same annotation is also needed to declare the test as parameterized: @RunWith(Parameterized.class) so I cannot add both to the same class. I found a similar question in this site that did not help much. So far, all the examples I have found explain how to parameterize simple unit tests, not a complete test tuite. mikemil I believe the basic answer is No, because as you said, the @RunsWith only take one parameter. I found a blog posting that got a

How to mock private methods using Whitebox(org.powermock.reflect)

穿精又带淫゛_ 提交于 2019-12-01 08:50:21
问题 I want to mock a private method which has been called inside another method. Following is the sample code, I have written. Java code: package org.mockprivatemethods; public class AccountDeposit { // Instantiation of AccountDetails using some DI AccountDetails accountDetails; public long deposit(long accountNum, long amountDeposited){ long amount = 0; try{ amount = accountDetails.getAmount(accountNum); updateAccount(accountNum, amountDeposited); amount= amount + amountDeposited; } catch

How to run entire JUnit Test Suite from within code

无人久伴 提交于 2019-12-01 06:53:05
In eclipse, with JUnit 4, you can right click a project or package and click Run as JUnit Test, and it will run all the JUnit tests within that grouping. Is there a way to do this same thing from within the code? You can use packages in junit such as JUnitCore like this: public static void main(String[] args){ List tests = new ArrayList(); tests.add(TestOne.class); tests.add(TestTwo.class); for (Class test : tests){ runTests(test); } } private static void runTests(Class test){ Result result = JUnitCore.runClasses(test); for (Failure failure : result.getFailures()){ System.out.println(failure

Using JUnit categories vs simply organizing tests in separate classes

不想你离开。 提交于 2019-12-01 06:28:58
I have two logical categories of tests: plain functional unit tests (pass/fail) and benchmark performance tests that are just for metrics/diagnostics. Currently, I have all test methods in a single class, call it MyTests : public class MyTests { @Test public void testUnit1() { ... assertTrue(someBool); } @Test public void testUnit2() { ... assertFalse(someBool); } @Test @Category(PerformanceTest.class) public void bmrkPerfTest1() { ... } @Test @Category(PerformanceTest.class) public void bmrkPerfTest2() { ... } } Then I have a UnitTestSuite defined as @RunWith(Categories.class) @Categories

unit-testing a ejb3.0 which has another ejb injected

梦想的初衷 提交于 2019-12-01 06:06:17
How can I unit test the ProcessorBean? Since I only wan't to test the ProcessorBean and not the Dao, I need to stub or mock the Dao, but I have no idea how I could do that with Junit. I'm using Junit4 and Ejb3.0 @Stateless public class ProcessorBean { @EJB private Dao dao; public void process() { //logic to be tested } } There's some support in OpenEJB you might find useful in combination with mocking. As an alternative to the EJB 3.0 Embedded EJBContainer API, you can simply build your app up in code. import junit.framework.TestCase; import org.apache.openejb.jee.EjbJar; import org.apache

JUnit 4.11 get test result in @After

我是研究僧i 提交于 2019-12-01 05:53:55
Is there any way I can get the test result in the teardown ( @After ) method? I'd like to do clean up after the tests depending on the result. Could not find much details about @After in the junit docs. The closest thing to what you're asking for would probably be the TestWatcher rule. That won't give you access to a returned result or anything, but you can use it (or create your own TestRule and combined with the Description object, you could annotate your methods differently to indicate what sort of clean-up is necessary. Why not set the result of a test in a class member and then act on it

Exclude individual JUnit Test methods without modifying the Test class?

落爺英雄遲暮 提交于 2019-12-01 04:50:24
I'm currently re-using JUnit 4 tests from another project against my code. I obtain them directly from the other project's repository as part of my automated Ant build. This is great, as it ensures I keep my code green against the very latest version of the tests. However, there is a subset of tests that I never expect to pass on my code. But if I start adding @Ignore annotations to those tests, I will have to maintain my own separate copy of the test implementation, which I really don't want to do. Is there a way of excluding individual tests without modifying the Test source? Here's what I