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
127
votes
9 answers

Multiple RunWith Statements in jUnit

I write unit test and want to use JUnitParamsRunner and MockitoJUnitRunner for one test class. Unfortunately, the following does not work: @RunWith(MockitoJUnitRunner.class) @RunWith(JUnitParamsRunner.class) public class DatabaseModelTest { //…
Hans-Helge
  • 1,764
  • 3
  • 14
  • 21
127
votes
3 answers

Separation of JUnit classes into special test package?

I am learning the concepts of Test-Driven Development through reading the Craftsman articles (click Craftsman under By Topic) recommended in an answer to my previous question, "Sample project for learning JUnit and proper software engineering". I…
Ricket
  • 33,368
  • 30
  • 112
  • 143
126
votes
12 answers

JUnit vs TestNG

At work we are currently still using JUnit 3 to run our tests. We have been considering switching over to JUnit 4 for new tests being written but I have been keeping an eye on TestNG for a while now. What experiences have you all had with either…
Sam Merrell
  • 2,352
  • 2
  • 16
  • 14
126
votes
30 answers

java.lang.Exception: No runnable methods exception in running JUnits

I am trying to run the JUnit on my Linux command prompt /opt/junit/ contains the necessary JARS(hamcrest-core-1.3.jar and junit.jar) and class files and I am using the following command to run the JUnit: java -cp hamcrest-core-1.3.jar:junit.jar:.…
Vipin Verma
  • 5,330
  • 11
  • 50
  • 92
124
votes
9 answers

How to test my servlet using JUnit

I have created a web system using Java Servlets and now want to make JUnit testing. My dataManager is just a basic piece of code that submits it to the database. How would you test a Servlet with JUnit? My code example that allows a user to…
Lunar
  • 4,663
  • 7
  • 43
  • 51
123
votes
12 answers

annotation to make a private method public only for test classes

Who has a solution for that common need. I have a class in my application. some methods are public, as they are part of the api, and some are private, as they for internal use of making the internal flow more readable now, say I want to write a unit…
Kumetix
  • 1,231
  • 2
  • 8
  • 3
121
votes
11 answers

JUnit: how to avoid "no runnable methods" in test utils classes

I have switched to JUnit4.4 from JUnit3.8. I run my tests using ant, all my tests run successfully but test utility classes fail with "No runnable methods" error. The pattern I am using is to include all classes with name *Test* under test folder. I…
LiorH
  • 18,524
  • 17
  • 70
  • 98
120
votes
6 answers

In JUnit 5, how to run code before all tests

The @BeforeAll annotation marks a method to run before all tests in a class. http://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations But is there a way to run some code before all tests, in all classes? I want to ensure that…
Rob N
  • 15,024
  • 17
  • 92
  • 165
120
votes
10 answers

Running junit tests in parallel in a Maven build?

I'm using JUnit 4.4 and Maven and I have a large number of long-running integration tests. When it comes to parallelizing test suites there are a few solutions that allow me to run each test method in a single test-class in parallel. But all of…
krosenvold
  • 75,535
  • 32
  • 152
  • 208
119
votes
6 answers

How to do an instanceof check with Scala(Test)

I'm trying to incorporate ScalaTest into my Java project; replacing all JUnit tests with ScalaTests. At one point, I want to check if Guice's Injector injects the correct type. In Java, I have a test like this: public class InjectorBehaviour { …
helpermethod
  • 59,493
  • 71
  • 188
  • 276
119
votes
49 answers

No tests found with test runner 'JUnit 4'

My Java test worked well from Eclipse. But now, when I relaunch test from the run menu, I get the following message: No tests found with test runner 'JUnit 4' In the .classpath file I have all jar files, and at the end have:
user281070
  • 1,341
  • 4
  • 11
  • 16
119
votes
11 answers

JUnit 4 compare Sets

How would you succinctly assert the equality of Collection elements, specifically a Set in JUnit 4?
Eqbal
  • 4,722
  • 12
  • 38
  • 47
118
votes
9 answers

How to run all tests in a particular package with Maven?

I can find in the Maven docs where it shows how to run: A single test All tests in a single test class All tests in classes matching a particular pattern But how to run all the tests in a package? Is this possible? I would prefer solutions that…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
118
votes
8 answers

Why must jUnit's fixtureSetup be static?

I marked a method with jUnit's @BeforeClass annotation, and got this exception saying it must be static. What's the rationale? This forces all my init to be on static fields, for no good reason as far as I see. In .Net (NUnit), this is not the…
ripper234
  • 222,824
  • 274
  • 634
  • 905
117
votes
15 answers

How to mock method e in Log

Here Utils.java is my class to be tested and following is the method which is called in UtilsTest class. Even if I am mocking Log.e method as shown below @Before public void setUp() { …
user3762991
  • 1,431
  • 3
  • 11
  • 16