junit4

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

我怕爱的太早我们不能终老 提交于 2019-11-26 15:06:59
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 command line using Ant. But when run from IntelliJ, it fails with: java.lang.NoSuchMethodError: org

Disable @EnableScheduling on Spring Tests

醉酒当歌 提交于 2019-11-26 13:28:41
问题 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?

Changing names of parameterized tests

跟風遠走 提交于 2019-11-26 12:47:12
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. 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 value from this invocation of the test. {1} - the second parameter value and so on The final name of the test

differences between 2 JUnit Assert classes

空扰寡人 提交于 2019-11-26 11:48:47
问题 The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is? The classes I\'m referring to are: junit.framework.Assert and org.junit.Assert. 回答1: The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase . That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way. Since version 4 of JUnit, the

Has JUnit4 begun supporting ordering of test? Is it intentional?

萝らか妹 提交于 2019-11-26 11:33:16
问题 A newbie to JUnit (in fact JUnit 4) and came across suite way of executing test @RunWith(Suite.class) @Suite.SuiteClasses( { CreateNewProfile.class, EditProfile.class, }) public class ProfileTestSuite { } This is the code sample I came across while browsing through the test code base at my new employer. During execution I fund that - CreateNewProfile tests are executed first and then EditProfile, which does make sense but then it introduces dependency among tests. I have been following non

How to define a JUnit method rule in a test suite?

ⅰ亾dé卋堺 提交于 2019-11-26 11:18:17
问题 I have a class that is a JUnit suite of JUnit test classes. I would like to define a rule on the suite to do something to the database before and after each unit test is run if a certain annotation is present on that test method. I\'ve been able to create a @ClassRule in the suites and test classes that will do this before each and every class (which is not good enough) and I have been able to define the test rules with the test classes themselves, but this is repetitive and does not seem

@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

≡放荡痞女 提交于 2019-11-26 11:11:18
While writing a new jUnit4 test, I'm wondering whether to use @RunWith(MockitoJUnitRunner.class) or MockitoAnnotations.initMocks(this) . I created a new test & the wizard automatically generated a test with the Runner. Javadocs for MockitoJUnitRunner state the following: Compatible with JUnit 4.4 and higher, this runner adds following behavior: Initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before each test method. validates framework usage after each test method. It's not clear to me whether using

Is Java&#39;s assertEquals method reliable?

那年仲夏 提交于 2019-11-26 10:17:49
问题 I know that == has some issues when comparing two Strings . It seems that String.equals() is a better approach. Well, I\'m doing JUnit testing and my inclination is to use assertEquals(str1, str2) . Is this a reliable way to assert two Strings contain the same content? I would use assertTrue(str1.equals(str2)) , but then you don\'t get the benefit of seeing what the expected and actual values are on failure. On a related note, does anyone have a link to a page or thread that plainly explains

Basic JUnit test for JavaFX 8

旧城冷巷雨未停 提交于 2019-11-26 09:39:52
问题 I want to create basic JUnit test for JavaFX 8 application. I have this simple code sample: public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(\"Tabs\"); Group root = new Group(); Scene scene = new Scene(root, 400, 250, Color.WHITE); TabPane tabPane = new TabPane(); BorderPane borderPane = new BorderPane(); for (int i = 0; i < 5; i++) { Tab tab = new Tab(); tab

How do I assert an Iterable contains elements with a certain property?

萝らか妹 提交于 2019-11-26 09:29:40
问题 Assume I want to unit test a method with this signature: List<MyItem> getMyItems(); Assume MyItem is a Pojo that has many properties, one of which is \"name\" , accessed via getName() . All I care about verifying is that the List<MyItem> , or any Iterable , contains two MyItem instances, whose \"name\" properties have the values \"foo\" and \"bar\" . If any other properties don\'t match, I don\'t really care for the purposes of this test. If the names match, it\'s a successful test. I would