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
378
votes
8 answers

How to capture a list of specific type with mockito

Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn't work: ArgumentCaptor> argument = ArgumentCaptor.forClass(ArrayList.class);
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
374
votes
13 answers

Populating Spring @Value during Unit Test

I'm trying to write a Unit Test for a simple bean that's used in my program to validate forms. The bean is annotated with @Component and has a class variable that is initialized using @Value("${this.property.value}") private String thisProperty; I…
Kyle
  • 14,036
  • 11
  • 46
  • 61
371
votes
28 answers

How to mock a final class with mockito

I have a final class, something like this: public final class RainOnTrees{ public void startRain(){ // some code here } } I am using this class in some other class like this: public class Seasons{ RainOnTrees rain = new…
buttowski
  • 4,657
  • 8
  • 27
  • 33
369
votes
11 answers

Mockito. Verify method arguments

I've googled about this, but didn't find anything relevant. I've got something like this: Object obj = getObject(); Mockeable mock= Mockito.mock(Mockeable.class); Mockito.when(mock.mymethod(obj )).thenReturn(null); Testeable testableObj = new…
manolowar
  • 6,772
  • 5
  • 23
  • 18
330
votes
16 answers

How to get the path of src/test/resources directory in JUnit?

I know I can load a file from src/test/resources with: getClass().getResource("somefile").getFile() But how can I get the full path to the src/test/resources directory, i.e. I don't want to load a file, I just want to know the path of the…
Rory
  • 4,030
  • 4
  • 17
  • 21
329
votes
8 answers

AndroidJUnit4.class is deprecated: How to use androidx.test.ext.junit.runners.AndroidJUnit4?

For my instrumentation tests I was using @RunWith(AndroidJUnit4.class) from import androidx.test.runner.AndroidJUnit4; in order to establish my test cases. Now this line gets marked as deprecated with the hint to use AndroidJUnit4 from import…
Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
307
votes
39 answers

No tests found for given includes Error, when running Parameterized Unit test in Android Studio

I have tried to run Parameterized Unit Tests in Android Studio, as shown below: import android.test.suitebuilder.annotation.SmallTest; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import…
Elye
  • 53,639
  • 54
  • 212
  • 474
304
votes
23 answers

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields. I have considered using…
teabot
  • 15,358
  • 11
  • 64
  • 79
303
votes
32 answers

How to do a JUnit assert on a message in a logger

I have some code-under-test that calls on a Java logger to report its status. In the JUnit test code, I would like to verify that the correct log entry was made in this logger. Something along the following lines: methodUnderTest(bool x){ if(x) …
Jon
  • 3,509
  • 2
  • 17
  • 8
295
votes
5 answers

How to tell a Mockito mock object to return something different the next time it is called?

So, I'm creating a mock object as a static variable on the class level like so... In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I'm having is that it seems…
DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
291
votes
28 answers

Testing two JSON objects for equality ignoring child order in Java

I'm looking for a JSON parsing library that supports comparing two JSON objects ignoring child order, specifically for unit testing JSON returning from a web service. Do any of the major JSON libraries support this? The org.json library simply does…
Jeff
  • 5,746
  • 4
  • 33
  • 40
267
votes
18 answers

How to use JUnit to test asynchronous processes

How do you test methods that fire asynchronous processes with JUnit? I don't know how to make my test wait for the process to end (it is not exactly a unit test, it is more like an integration test as it involves several classes and not just one).
Sam
  • 6,437
  • 6
  • 33
  • 41
265
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
  • 185,044
  • 174
  • 569
  • 824
263
votes
17 answers

Get name of currently executing test in JUnit 4

In JUnit 3, I could get the name of the currently running test like this: public class MyTest extends TestCase { public void testSomething() { System.out.println("Current test is " + getName()); ... } } which would print…
Dave Ray
  • 39,616
  • 7
  • 83
  • 82
253
votes
4 answers

Difference between @Mock, @MockBean and Mockito.mock()

When creating tests and mocking dependencies, what is the difference between these three approaches? @MockBean: @MockBean MyService myservice; @Mock: @Mock MyService myservice; Mockito.mock() MyService myservice = Mockito.mock(MyService.class);
Doug
  • 5,661
  • 2
  • 26
  • 27