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
173
votes
3 answers

How to use ArgumentCaptor for stubbing?

In Mockito documentation and javadocs it says It is recommended to use ArgumentCaptor with verification but not with stubbing. but I don't understand how ArgumentCaptor can be used for stubbing. Can someone explain the above statement and show…
Can't Tell
  • 12,714
  • 9
  • 63
  • 91
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
167
votes
4 answers

Difference between setUp() and setUpBeforeClass()

When unit testing with JUnit, there are two similar methods, setUp() and setUpBeforeClass(). What is the difference between these methods? Also, what is the difference between tearDown() and tearDownAfterClass()? Here are the…
Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43
164
votes
5 answers

Checking that a List is not empty in Hamcrest

I was wondering if anyone knew of a way to check if a List is empty using assertThat() and Matchers? Best way I could see just use JUnit: assertFalse(list.isEmpty()); But I was hoping that there was some way to do this in Hamcrest.
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
159
votes
8 answers

java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail

I am using Java Mail API to read and parse emails. It is working fine with Servlet code. Now I am trying to write a JUnit test case for the same purpose. But on line Session session = Session.getDefaultInstance(properties); I am getting the…
user2215139
  • 1,805
  • 2
  • 18
  • 24
159
votes
6 answers

Python unittests in Jenkins?

How do you get Jenkins to execute python unittest cases? Is it possible to JUnit style XML output from the builtin unittest package?
erikbstack
  • 12,878
  • 21
  • 81
  • 115
156
votes
13 answers

Testing Private method using mockito

public class A { public void method(boolean b){ if (b == true) method1(); else method2(); } private void method1() {} private void method2() {} } public class TestA { @Test …
Nageswaran
  • 7,481
  • 14
  • 55
  • 74
156
votes
19 answers

Surefire is not picking up Junit 5 tests

I wrote a simple test method with JUnit 5: public class SimlpeTest { @Test @DisplayName("Some description") void methodName() { // Testing logic for subject under test } } But when I run mvn test, I…
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
155
votes
11 answers

Easy way of running the same junit test over and over?

Like the title says, I'm looking for some simple way to run JUnit 4.x tests several times in a row automatically using Eclipse. An example would be running the same test 10 times in a row and reporting back the result. We already have a complex way…
Stefan Thyberg
  • 3,445
  • 3
  • 23
  • 29
154
votes
8 answers

How to assert greater than using JUnit Assert?

I have these values coming from a test previousTokenValues[1] = "1378994409108" currentTokenValues[1] = "1378994416509" and I try // current timestamp is greater assertTrue(Long.parseLong(previousTokenValues[1]) >…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
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
148
votes
4 answers

What's the difference between src/androidtest and src/test folders?

In a project, in Android Studio, by default, there are two test folders. The first is src/androidTest. This folder already existed in the previous version of Android Studio. Nevertheless, there is now a new test folder, by default, src/test, and new…
lopez.mikhael
  • 9,943
  • 19
  • 67
  • 110
146
votes
6 answers

What order are the Junit @Before/@After called?

I have an Integration Test Suite. I have a IntegrationTestBase class for all my tests to extend. This base class has a @Before (public void setUp()) and @After (public void tearDown()) method to establish API and DB connections. What I've been doing…
Joel
  • 16,474
  • 17
  • 72
  • 93
146
votes
7 answers

Hamcrest compare collections

I'm trying to compare 2 lists: assertThat(actual.getList(), is(Matchers.containsInAnyOrder(expectedList))); But idea java: no suitable method found for assertThat(java.util.List,org.hamcrest.Matcher
xander27
  • 3,014
  • 8
  • 30
  • 42
144
votes
9 answers

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

What's the actual use of 'fail' in JUnit test case?
Sanju
  • 3,187
  • 10
  • 41
  • 55