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
90
votes
17 answers

java.lang.NoClassDefFoundError in junit

I am getting this error in java in my junit test code. I looked up on the net and it says that I need to add the junit.jar in the classpath. In Eclipse I have added it in the classpath of Project Properties windows but I still get initialisation…
user506710
90
votes
21 answers

Compare Date objects with different levels of precision

I have a JUnit test that fails because the milliseconds are different. In this case I don't care about the milliseconds. How can I change the precision of the assert to ignore milliseconds (or any precision I would like it set to)? Example of a…
brainimus
  • 10,586
  • 12
  • 42
  • 64
89
votes
6 answers

How to write junit tests for interfaces?

What is the best way to write junit tests for interfaces so they can be used for the concrete implementing classes? e.g. You have this interface and implementing classes: public interface MyInterface { /** Return the given value. */ public…
Xeno Lupus
  • 1,504
  • 3
  • 12
  • 17
89
votes
6 answers

IntelliJ Error when running unit test: Could not find or load main class ${surefireArgLine}

I get the following error when running Unit tests in IntelliJ: Error: Could not find or load main class ${surefireArgLine}. I am using maven and in pom.xml I have: ...
BlueLettuce16
  • 2,013
  • 4
  • 20
  • 31
89
votes
8 answers

How to JUnit test that two List contain the same elements in the same order?

Context I am writing a simple JUnit test for the MyObject class. A MyObject can be created from a static factory method that takes a varargs of String. MyObject.ofComponents("Uno", "Dos", "Tres"); At any time during the existence of MyObject,…
Robottinosino
  • 10,384
  • 17
  • 59
  • 97
88
votes
8 answers

Junit before class ( non static )

Are there any best practices to get Junit execute a function once in a test file , and it should also not be static. like @BeforeClass on non static function? Here is an ugly solution : @Before void init(){ if (init.get() == false){ …
Roman
  • 7,933
  • 17
  • 56
  • 72
88
votes
8 answers

Spring JUnit: How to Mock autowired component in autowired component

I've got a Spring component I'd like to test and this component has an autowired attribute which I need to change for the purpose of unit testing. The problem is, that the class uses the autowired component inside the post-construct method so I'm…
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
88
votes
6 answers

JUnit 4 vs TestNG - Update 2013 - 2014

JUnit 4 and TestNG used to be comparable. What are the pros and cons of the two testing frameworks?
Arian
  • 3,183
  • 5
  • 30
  • 58
87
votes
9 answers

Maven not running JUnit 5 tests

I'm trying to get a simple junit test running with maven but it is not detecting any tests. Where am I going wrong? The project directory Project -> src -> test-> java -> MyTest.java Results : Tests run: 0, Failures: 0, Errors: 0, Skipped:…
mogoli
  • 2,153
  • 6
  • 26
  • 41
87
votes
14 answers

How to exclude *AutoConfiguration classes in Spring Boot JUnit tests?

I tried: @RunWith(SpringJUnit4ClassRunner.class) @EnableAutoConfiguration(exclude=CrshAutoConfiguration.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class LikeControllerTest { However the CRaSSHD…
Hendy Irawan
  • 20,498
  • 11
  • 103
  • 114
87
votes
3 answers

Difference between MockMvc and RestTemplate in integration tests

Both MockMvc and RestTemplate are used for integration tests with Spring and JUnit. Question is: what's the difference between them and when we should choose one over another? Here are just examples of both options: //MockMVC…
Denis C de Azevedo
  • 6,276
  • 2
  • 32
  • 49
87
votes
5 answers

CollectionAssert in jUnit?

Is there a jUnit parallel to NUnit's CollectionAssert?
ripper234
  • 222,824
  • 274
  • 634
  • 905
86
votes
3 answers

Multiple correct results with Hamcrest (is there an or-matcher?)

I am relatively new to matchers. I am toying around with hamcrest in combination with JUnit and I kinda like it. Is there a way, to state that one of multiple choices is correct? Something like assertThat( result, is( either( 1, or( 2, or( 3 ) ) ) )…
Mo.
  • 15,033
  • 14
  • 47
  • 57
86
votes
10 answers

Testing a JAX-RS Web Service?

I'm currently looking for ways to create automated tests for a JAX-RS (Java API for RESTful Web Services) based web service. I basically need a way to send it certain inputs and verify that I get the expected responses. I'd prefer to do this via…
Einar
  • 3,279
  • 2
  • 26
  • 28
85
votes
11 answers

Before and After Suite execution hook in jUnit 4.x

I'm trying to preform setup and teardown for a set of integration tests, using jUnit 4.4 to execute the tests. The teardown needs to be run reliably. I'm having other problems with TestNG, so I'm looking to port back to jUnit. What hooks are…
sblundy
  • 60,628
  • 22
  • 121
  • 123