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
11
votes
1 answer

IntelliJ warning: Unchecked generics array creation for varargs parameter

IntelliJ Idea is giving following warning for one of the statements in my test cases. Warning: Unchecked generics array creation for varargs parameter Signals places where an unchecked warning is issued by the compiler All I am doing is…
Nitin S
  • 153
  • 2
  • 10
11
votes
3 answers

Parallel test case execution with JUnit 5

Is it possible to execute a test case parallel with JUnit 5? I'm looking for something like threadPoolSize and invocationCount from TestNG: @Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
deamon
  • 89,107
  • 111
  • 320
  • 448
11
votes
7 answers

How to avoid multiple asserts in a JUnit test?

I have a DTO which I'm populating from the request object, and the request object has many fields. I want to write a test to check if the populateDTO() method is putting values in the right places or not. If I follow the rule of one assert per test,…
Ravi
  • 7,939
  • 14
  • 40
  • 43
11
votes
3 answers

How to mock object with constructor that takes a Class?

This is the test: import static junit.framework.Assert.assertTrue; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.whenNew; import org.junit.Test; import…
Belun
  • 4,151
  • 7
  • 34
  • 51
11
votes
7 answers

Asserting properties on list elements with assertJ

I have a working hamcrest assertion: assertThat(mylist, contains( containsString("15"), containsString("217"))); The intended behavior is: mylist == asList("Abcd15", "217aB") => success myList == asList("Abcd15", "218") => failure How can I…
CoronA
  • 7,717
  • 2
  • 26
  • 53
11
votes
4 answers

Java: how to "restart" a static class?

I have a static class (Foo) and a main class (Main) See Main.java: public class Main { public static void main(String[] args) { System.out.println(Foo.i); // 0 Foo.i++; System.out.println(Foo.i); // 1 // restart…
Topera
  • 12,223
  • 15
  • 67
  • 104
11
votes
4 answers

How to call assertEquals with Double Epsilon/Precision in Kotlin?

I was wondering, in Kotlin, is there the possibility to call the equivalent of the java method: assertEquals(double expected, double actual, double precision) because everytime I'm getting this method instead assertEquals(expected: T, actual: T,…
Stefano.Maffullo
  • 801
  • 8
  • 21
11
votes
4 answers

Mockito mocking a method calls the actual method

I am trying to mock method createInstanceB() using Mockito.when() as well as doReturn(). This always calls real method. For example: Class A { public B createInstanceB(any, any) { B b = new B(); b.api(); } } I am using the code…
user3754993
  • 143
  • 1
  • 2
  • 8
11
votes
1 answer

"Empty test suite." in pure kotlin module. (Spock/Android)

My android app is multi module project: include (android-app/kotlin-android)':application', (pure kotlin)':presentation', (pure kotlin)':domain', (android-library/kotin-android)':dataproviders' Modules :application and :dataproviders working fine…
Hype
  • 1,039
  • 1
  • 10
  • 27
11
votes
1 answer

Run all tests except for a JUnit Category in IntelliJ

I pretty much only use JUnit Categories for non-unit tests that I don't want to run as part of the test suite. In NUnit I could use Explicit, but the only thing I've found comparable in JUnit is a Category. In gradle, it's simple to exclude a…
Novaterata
  • 4,356
  • 3
  • 29
  • 51
11
votes
5 answers

Prefix for testing methods in Unit: "test" vs "should"

It is a common practice to prefix the tests method names in JUnit with "test". But in the last few years, some people changed this to the prefix "should". If I want to test the customer creation in a database, I would normally name the method…
11
votes
4 answers

How can I test exception in completable future?

I have been converting some code to be asynchronous. The original unit test used the annotation @Test(expected = MyExcpetion.class) but I don't think this will work because the exception I want to assert on is wrapped in…
Barry
  • 1,800
  • 2
  • 25
  • 46
11
votes
1 answer

How are Dynamic Tests different from Parameterized Tests in JUnit 5?

Almost all the examples of dynamic tests that I have seen, can be reworked and written using parameterized tests. So, which is a practical scenario where the Dynamic tests are the only option, or at least, better suitable than the parameterized…
Yasin
  • 1,906
  • 1
  • 21
  • 37
11
votes
2 answers

Spring DAO Test Fails - says "requires JUnit 4.12 or higher"

I'm trying to write a DAO test using Spring. When I run the test, it errors out with the following stack trace below. I don't know why I am getting an error when I believe I am including the proper version of…
Scott Stella
  • 111
  • 1
  • 1
  • 5
11
votes
2 answers

What are advantages of using @ContextHierarchy over pure @ContextConfiguration

Hi I don't understand what advantages give me usage of @ContextHierarchy like below: @ContextHierarchy({ @ContextConfiguration("/test-db-setup-context.xml"), …