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
210
votes
20 answers

How to test code dependent on environment variables using JUnit?

I have a piece of Java code which uses an environment variable and the behaviour of the code depends on the value of this variable. I would like to test this code with different values of the environment variable. How can I do this in JUnit? I've…
vitaut
  • 49,672
  • 25
  • 199
  • 336
210
votes
14 answers

How to resolve Unneccessary Stubbing exception

My Code is as below, @RunWith(MockitoJUnitRunner.class) public class MyClass { private static final String code ="Test"; @Mock private MyClassDAO dao; @InjectMocks private MyClassService Service = new…
VHS
  • 2,109
  • 2
  • 8
  • 4
210
votes
25 answers

Getting "Skipping JaCoCo execution due to missing execution data file" upon executing JaCoCo

I'm using Maven 3.0.3, JUnit 4.8.1, and Jacoco 0.6.3.201306030806, and I am trying to create test coverage reports. I have a project with unit tests only, but I can't get reports to run, I'm repeatedly getting the error: Skipping JaCoCo execution…
Dave
  • 15,639
  • 133
  • 442
  • 830
209
votes
6 answers

When to use Mockito.verify()?

I write jUnit test cases for 3 purposes: To ensure that my code satisfies all of the required functionality, under all (or most of) the input combinations/values. To ensure that I can change the implementation, and rely on JUnit test cases to tell…
Russell
  • 2,091
  • 2
  • 13
  • 5
205
votes
42 answers

Class Not Found: Empty Test Suite in IntelliJ

I'm just starting the computer science program at my college, and I'm having some issues with IntelliJ. When I try to run unit tests, I get the message Process finished with exit code 1 Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty…
arnbobo
  • 2,515
  • 2
  • 12
  • 18
197
votes
8 answers

Pass a local file in to URL in Java

How do I create a new URL object using a local file, for the purpose of unit tests?
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
187
votes
10 answers

Spring Test & Security: How to mock authentication?

I was trying to figure out how to unit test if my the URLs of my controllers are properly secured. Just in case someone changes things around and accidentally removes security settings. My controller method looks like…
Martin Becker
  • 3,331
  • 3
  • 22
  • 25
186
votes
3 answers

Warning: The method assertEquals from the type Assert is deprecated

Since the method Assert.assertEquals is deprecated, which method are we supposed to use now? The following code: String arg1 = "test"; String arg2 = "me"; Assert.assertEquals(arg1, arg2); Gives the following warnings: Multiple markers at this…
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
184
votes
5 answers

Configuring IntelliJ IDEA for unit testing with JUnit

I decided to try out IntelliJ this morning via the trial version and installed the JUnit plugin. I made a new Java project and I want to write a test case for it. How do I add the junit.jar file to my project? (I actually want to add it to every…
Bob
  • 3,283
  • 3
  • 20
  • 14
184
votes
1 answer

Java verify void method calls n times with Mockito

I'm trying to verify that a (void) method is being called inside of a DAO - I'm using a commit point that sends a list of results up to that point, resets the list and continues. Say I have 4 things in the list and I have a commit point of 1, I…
nbpeth
  • 2,967
  • 4
  • 24
  • 34
180
votes
15 answers

Mockito How to mock and assert a thrown exception?

I'm using mockito in a junit test. How do you make an exception happen and then assert that it has (generic pseudo-code)
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
180
votes
23 answers

How do I assert equality on two classes without an equals method?

Say I have a class with no equals() method, to which do not have the source. I want to assert equality on two instances of that class. I can do multiple asserts: assertEquals(obj1.getFieldA(), obj2.getFieldA()); assertEquals(obj1.getFieldB(),…
Ryan Nelson
  • 4,466
  • 5
  • 29
  • 45
179
votes
33 answers

Mockito - NullpointerException when stubbing Method

So I started writing tests for our Java-Spring-project. What I use is JUnit and Mockito. It's said, that when I use the when()...thenReturn() option I can mock services, without simulating them or so. So what I want to do is, to…
user5417542
  • 3,146
  • 6
  • 29
  • 50
179
votes
5 answers

JUnit confusion: use 'extends TestCase' or '@Test'?

I've found the proper use (or at least the documentation) of JUnit very confusing. This question serves both as a future reference and as a real question. If I've understood correctly, there are two main approaches to create and run a JUnit…
Rabarberski
  • 23,854
  • 21
  • 74
  • 96
173
votes
8 answers

Initialising mock objects - Mockito

There are many ways to initialize a mock object using MockIto. What is best way among these ? 1. public class SampleBaseTestCase { @Before public void initMocks() { MockitoAnnotations.initMocks(this); …
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56