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.

3828 questions
2100
votes
34 answers

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

How can I use JUnit4 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(); } catch…
SCdF
  • 51,261
  • 23
  • 74
  • 108
461
votes
6 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
  • 16,046
  • 33
  • 87
  • 157
438
votes
21 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
  • 29,208
  • 21
  • 78
  • 123
346
votes
12 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
  • 4,966
  • 9
  • 29
  • 31
304
votes
17 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
  • 5,084
  • 6
  • 25
  • 33
260
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
  • 176,670
  • 166
  • 541
  • 787
238
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
  • 15,499
  • 17
  • 77
  • 123
212
votes
13 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
  • 35,261
  • 53
  • 143
  • 239
206
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
  • 5,512
  • 6
  • 50
  • 78
205
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
  • 18,215
  • 23
  • 55
  • 63
180
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,416
  • 2
  • 16
  • 23
155
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
136
votes
6 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
  • 37,355
  • 13
  • 206
  • 248
131
votes
8 answers

What's the actual use of 'fail' in JUnit test case?

What's the actual use of 'fail' in JUnit test case?
Sanju
  • 2,847
  • 10
  • 39
  • 54
131
votes
2 answers

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

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…
OceanBlue
  • 8,812
  • 20
  • 58
  • 80
1
2 3
99 100