junit4

How do I Dynamically create a Test Suite in JUnit 4?

坚强是说给别人听的谎言 提交于 2019-11-26 08:56:30
问题 I would like to create a junit test suite using JUnit 4 where the names of the test classes to be included are not known until the test suite is run. In JUnit 3 I could do this: public final class MasterTester extends TestCase { /** * Used by junit to specify what TestCases to run. * * @return a suite containing what TestCases to run */ public static TestSuite suite() { TestSuite suite = new TestSuite(); for(Class<?> klass : gatherTestClasses()) { suite.addTestSuite(klass); } return suite; }

Can&#39;t run JUnit 4 test case in Eclipse Android project

纵然是瞬间 提交于 2019-11-26 08:06:48
问题 I am new to Java and am trying to run a unit test on a class I am writing. Eclipse (3.5) created the unit test class for me and added Junit4 to my class path. My Class: public class DistanceUtil { public static double metersToMiles( double meters ) { return 0; } public static double metersToKilometers( double meters ) { return 0; } } My unit test: public class DistanceUtilTest { @Test public final void testMetersToMiles() { fail(\"Not yet implemented\"); // TODO } @Test public final void

How to run all tests belonging to a certain Category in JUnit 4

泪湿孤枕 提交于 2019-11-26 06:18:55
问题 JUnit 4.8 contains a nice new feature called \"Categories\" that allows you to group certain kinds of tests together. This is very useful, e.g. to have separate test runs for slow and fast tests. I know the stuff mentioned in JUnit 4.8 release notes, but would like to know how I can actually run all the tests annotated with certain category. The JUnit 4.8 release notes show an example suite definition, where SuiteClasses annotation selects the tests from certain category to run, like this:

No tests found with test runner &#39;JUnit 4&#39;

扶醉桌前 提交于 2019-11-26 06:05:51
问题 My Java test worked well from Eclipse. But now, when I relaunch test from the run menu, I get the following message: No tests found with test runner \'JUnit 4\' In the .classpath file I have all jar files, and at the end have: <classpathentry exported=\"true\" kind=\"con\" path=\"org.eclipse.jdt.junit.JUNIT_CONTAINER/4\"/> <classpathentry kind=\"output\" path=\"bin\"/> </classpath> How can I resolve this error and get tests running again? 回答1: this just happened to me. Rebuilding or

How to test that no exception is thrown?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 04:34:59
问题 I know that one way to do it would be: @Test public void foo(){ try{ //execute code that you expect not to throw Exceptions. } catch(Exception e){ fail(\"Should not have thrown any exception\"); } } Is there any cleaner way of doing this. (Probably using Junit\'s @Rule ?) 回答1: You're approaching this the wrong way. Just test your functionality: if an exception is thrown the test will automatically fail. If no exception is thrown, your tests will all turn up green. I have noticed this question

Getting “NoSuchMethodError: org.hamcrest.Matcher.describeMismatch” when running test in IntelliJ 10.5

ぐ巨炮叔叔 提交于 2019-11-26 04:10:02
问题 I\'m using JUnit-dep 4.10 and Hamcrest 1.3.RC2. I\'ve created a custom matcher that looks like the following: public static class MyMatcher extends TypeSafeMatcher<String> { @Override protected boolean matchesSafely(String s) { /* implementation */ } @Override public void describeTo(Description description) { /* implementation */ } @Override protected void describeMismatchSafely(String item, Description mismatchDescription) { /* implementation */ } } It works perfectly fine when run from the

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

偶尔善良 提交于 2019-11-26 03:24:10
问题 What is the main difference between @Before and @BeforeClass and in JUnit 5 @BeforeEach and @BeforeAll @After and @AfterClass According to the JUnit Api @Before is used in the following case: When writing tests, it is common to find that several tests need similar objects created before they can run. Whereas @BeforeClass can be used to establish a database connection. But couldn\'t @Before do the same? 回答1: The code marked @Before is executed before each test, while @BeforeClass runs once

How to run test methods in specific order in JUnit4?

大兔子大兔子 提交于 2019-11-26 03:13:42
问题 I want to execute test methods which are annotated by @Test in specific order. For example: public class MyTest { @Test public void test1(){} @Test public void test2(){} } I want to ensure to run test1() before test2() each time I run MyTest , but I couldn\'t find annotation like @Test(order=xx) . I think it\'s quite important feature for JUnit, if author of JUnit doesn\'t want the order feature , why? 回答1: I think it's quite important feature for JUnit, if author of JUnit doesn't want the

Changing names of parameterized tests

喜你入骨 提交于 2019-11-26 02:48:33
问题 Is there a way to set my own custom test case names when using parameterized tests in JUnit4? I\'d like to change the default — [Test class].runTest[n] — to something meaningful. 回答1: This feature has made it into JUnit 4.11. To use change the name of parameterized tests, you say: @Parameters(name="namestring") namestring is a string, which can have the following special placeholders: {index} - the index of this set of arguments. The default namestring is {index} . {0} - the first parameter

Maven does not find JUnit tests to run

删除回忆录丶 提交于 2019-11-26 00:35:43
问题 I have a maven program, it compiles fine. When I run mvn test it does not run any tests (under TESTs header says There are no tests to run. ). I\'ve recreated this problem with a super simple setup which I will include below as well as the output when run with -X . The unit tests run fine from eclipse (both with its default junit package and when I instead include the junit.jar downloaded by maven). Also mvn test-compile correctly creates the class under test-classes. I am running this on OSX