Questions tagged [junit]

Popular unit testing framework for Java and Scala. The latest version, JUnit 5, supports rich annotation-based and parameterized tests. Consider using in conjunction with the Java or Scala tag to indicate your use case.

JUnit is a popular unit testing framework for JVM languages (Java, Scala, Groovy, Kotlin, and others). The current version, JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage. Consider using in conjunction with the appropriate language tag (, , , and others) tag to indicate your use case. It is one of the most popular Java frameworks and is used in circa 30% of all projects.

The latest iteration, JUnit 5 (use tag ) adds Java 8 idioms, parameterized tests and comprehensive support for custom extensions.

There are JUnit implementations for other programming languages, which are called xUnit.

Kent Beck, Erich Gamma and David Saff created the unit testing framework influencing Java development heavily.

Grab JUnit's source code at GitHub or even fork it.

Related Links:

27795 questions
250
votes
12 answers

AssertContains on strings in jUnit

Is there a nicer way to write in jUnit String x = "foo bar"; Assert.assertTrue(x.contains("foo"));
ripper234
  • 222,824
  • 274
  • 634
  • 905
247
votes
7 answers

When do Java generics require instead of and is there any downside of switching?

Given the following example (using JUnit with Hamcrest matchers): Map> expected = null; Map> result = null; assertThat(result, is(expected)); This does not compile with the JUnit…
Yishai
  • 90,445
  • 31
  • 189
  • 263
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
239
votes
6 answers

Assert an object is a specific type

Is it possible in JUnit to assert an object is an instance of a class? For various reasons I have an object in my test that I want to check the type of. Is it a type of Object1 or a type of Object2? Currently I have: assertTrue(myObject instanceof…
RNJ
  • 15,272
  • 18
  • 86
  • 131
236
votes
19 answers

How to test methods that call System.exit()?

I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just…
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
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
234
votes
16 answers

Unable to find a @SpringBootConfiguration when doing a JpaTest

I'm trying to run a simple Junit test to see if my CrudRepositories are indeed working. The error I keep getting is: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your…
Thomas Billet
  • 2,381
  • 2
  • 13
  • 13
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
219
votes
27 answers

IntelliJ IDEA with Junit 4.7 "!!! JUnit version 3.8 or later expected:"

When I attempt to run the following test in IntelliJ IDEA I get the message: "!!! JUnit version 3.8 or later expected:" It should be noted that this is an Android project I am working on in IntelliJ IDEA 9. public class GameScoreUtilTest { …
benstpierre
  • 32,833
  • 51
  • 177
  • 288
218
votes
8 answers

Comparing arrays in JUnit assertions, concise built-in way?

Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself. EG, doesn't work: int[] expectedResult = new int[] { 116800,…
mBria
  • 2,412
  • 2
  • 15
  • 11
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
216
votes
8 answers

Meaning of delta or epsilon argument of assertEquals for double values

I have a question about JUnit assertEquals to test double values. Reading the API doc I can see: @Deprecated public static void assertEquals(double expected, double actual) Deprecated. Use assertEquals(double expected, double actual, double delta)…
Édipo Féderle
  • 4,169
  • 5
  • 31
  • 35
215
votes
7 answers

Example of Mockito's argumentCaptor

Can anyone please provide me an example showing how to use the org.mockito.ArgumentCaptor class and how it is different from simple matchers that are provided with mockito. I read the provided mockito documents but those doesn't illustrate it…
Ujjwal
  • 2,365
  • 2
  • 11
  • 7
211
votes
8 answers

What is the JUnit XML format specification that Hudson supports?

I have Hudson as continuous integration server and I want to use option 'Publish JUnit test result report'. But I don't use xUnit tools for testing, instead of that i have shell scripts which run tests and return results in simple format. I am…
Vlad Krylov
  • 2,684
  • 3
  • 21
  • 23
211
votes
14 answers

Assert equals between 2 Lists in Junit

How can I make an equality assertion between lists in a JUnit test case? Equality should be between the content of the list. For example: List numbers = Arrays.asList("one", "two", "three"); List numbers2 = Arrays.asList("one",…
Kamal
  • 3,878
  • 4
  • 21
  • 24