Questions tagged [junit4]

Version 4 of the popular Junit Java Unit testing framework

Version 4 of the popular JUnit Java Unit testing framework.

New features are the introducing of annotations to use the framework.

Use this tag only for question related to features provided by version 4. Use for general questions.

4127 questions
2271
votes
35 answers

How do you assert that a certain exception is thrown in JUnit tests?

How can I use JUnit idiomatically to test that some code throws an exception? While I can certainly do something like this: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); …
SCdF
  • 57,260
  • 24
  • 77
  • 113
510
votes
7 answers

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

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…
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
470
votes
23 answers

How to run test methods in specific order in JUnit4?

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…
卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
432
votes
20 answers

How to test that no exception is thrown?

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…
Ankit Dhingra
  • 6,534
  • 6
  • 31
  • 34
387
votes
13 answers

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

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…
Cshah
  • 5,612
  • 10
  • 33
  • 37
265
votes
6 answers

differences between 2 JUnit Assert classes

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.
Dónal
  • 185,044
  • 174
  • 569
  • 824
245
votes
15 answers

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

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 { @Override protected boolean matchesSafely(String s) { /*…
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
236
votes
4 answers

How does Junit @Rule work?

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…
Dipak
  • 6,532
  • 8
  • 63
  • 87
225
votes
15 answers

Changing names of parameterized tests

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.
Epaga
  • 38,231
  • 58
  • 157
  • 245
218
votes
7 answers

Is Java's assertEquals method reliable?

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…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64
199
votes
7 answers

Mockito + PowerMock LinkageError while mocking system class

I've got such a code snippet: @RunWith(PowerMockRunner.class) @PrepareForTest({Thread.class}) public class AllMeasuresDataTest { @Before public void setUp() throws Exception { } @Test public void testGetMeasures() { AllMeasuresData measure =…
Wojciech Reszelewski
  • 2,656
  • 2
  • 18
  • 27
171
votes
9 answers

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

Assume I want to unit test a method with this signature: List 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, or any…
Kevin Pauli
  • 8,577
  • 15
  • 49
  • 70
168
votes
9 answers

Mockito: Mock private field initialization

How I can mock a field variable which is being initialized inline? class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock…
Arun
  • 2,360
  • 2
  • 17
  • 23
168
votes
7 answers

Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods

When I look at the examples in the Assert class JavaDoc assertThat("Help! Integers don't work", 0, is(1)); // fails: // failure message: // Help! Integers don't work // expected: is <1> // got value: <0> assertThat("Zero is one", 0, is(not(1))) //…
Peter Paul
149
votes
7 answers

"Assert in junit.framework has been deprecated" - what next to use?

I bump version of junit to 4.11 and get: [WARNING] [deprecation] Assert in junit.framework has been deprecated [WARNING] [deprecation] Assert in junit.framework has been deprecated .... How and to what migrate?
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
1
2 3
99 100