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
102
votes
11 answers

AssertEquals 2 Lists ignore order

That should be really simple question I believe. But somehow I can't find answer in Google. Assume that I have 2 Lists of Strings. First contains "String A" and "String B", second one contains "String B" and "String A" (notice difference in order).…
kukis
  • 4,489
  • 6
  • 27
  • 50
102
votes
6 answers

Injecting @Autowired private field during testing

I have a component setup that is essentially a launcher for an application. It is configured like so: @Component public class MyLauncher { @Autowired MyService myService; //other methods } MyService is annotated with the @Service…
Kyle
  • 14,036
  • 11
  • 46
  • 61
101
votes
23 answers

Eclipse - debugger doesn't stop at breakpoint

I am trying to trouble shoot a JUnit. In the source code, I have set break point in two places: 1) in a line where a static member is initialized 2) the first line of one of the test cases. The debugger stops in the static field initializing line.…
Roy
  • 1,015
  • 2
  • 7
  • 5
100
votes
5 answers

How to test an Android Library Project

I am writing an Android Library Project basing on Android Bitmap class (call it AndroindLib) which contains only utility class (no activity). I tried to test it using Android JUnit, but it keeps complaining that can't find the AnroidLib.apk What's…
Lily
  • 5,872
  • 19
  • 56
  • 75
100
votes
6 answers

assert vs. JUnit Assertions

Today I saw a JUnit test case with a java assertion instead of the JUnit assertions—Are there significant advantages or disadvantages to prefer one over the other?
Robert
  • 8,406
  • 9
  • 38
  • 57
100
votes
9 answers

Android AsyncTask testing with Android Test Framework

I have a very simple AsyncTask implementation example and am having problem in testing it using Android JUnit framework. It works just fine when I instantiate and execute it in normal application. However when it's executed from any of Android…
Vladimir Kroz
  • 5,237
  • 6
  • 39
  • 50
99
votes
12 answers

How to test abstract class in Java with JUnit?

I am new to Java testing with JUnit. I have to work with Java and I would like to use unit tests. My problem is: I have an abstract class with some abstract methods. But there are some methods which are not abstract. How can I test this class with…
vasco
  • 1,502
  • 1
  • 16
  • 19
99
votes
8 answers

JUnit: using constructor instead of @Before

I'm using JUnit 4. I can't see the difference between initializing in the constructor or using a dedicated init function annotated by @Before. Does this mean that I don't have to worry about it? Is there any case when @Before gives more than just…
vbence
  • 20,084
  • 9
  • 69
  • 118
99
votes
12 answers

How to test main class of Spring-boot application

I have a spring-boot application where my @SpringBootApplication starter class looks like a standard one. So I created many tests for all my functionalities and send the summary to sonarqube to see my coverage. For my starter class Sonarqube tells…
Patrick
  • 12,336
  • 15
  • 73
  • 115
98
votes
7 answers

Test class with a new() call in it with Mockito

I have a legacy class that contains a new() call to instantiate a LoginContext object: public class TestedClass { public LoginContext login(String user, String password) { LoginContext lc = new LoginContext("login", callbackHandler); } } I…
bwobbones
  • 2,398
  • 3
  • 23
  • 32
97
votes
5 answers

Reuse spring application context across junit test classes

We've a bunch of JUnit test cases (Integration tests) and they are logically grouped into different test classes. We are able to load Spring application context once per test class and re-use it for all test cases in a JUnit test class as mentioned…
Ramesh
  • 3,841
  • 5
  • 22
  • 28
97
votes
7 answers

JUnit testing with simulated user input

I am trying to create some JUnit tests for a method that requires user input. The method under test looks somewhat like the following method: public static int testUserInput() { Scanner keyboard = new Scanner(System.in); …
Wimpey
  • 1,081
  • 1
  • 8
  • 5
96
votes
7 answers

JUnit test with dynamic number of tests

In our project I have several JUnit tests that e.g. take every file from a directory and run a test on it. If I implement a testEveryFileInDirectory method in the TestCase this shows up as only one test that may fail or succeed. But I am interested…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
96
votes
13 answers

Disable @EnableScheduling on Spring Tests

When I run my unit tests, it invokes my scheduled tasks. I want to prevent this behaviour, which is caused by the fact that I have @EnableScheduling on my main app configuration. How can I disable this on my unit tests? I have come across this…
Robbo_UK
  • 11,351
  • 25
  • 81
  • 117
95
votes
17 answers

Maven 3 and JUnit 4 compilation problem: package org.junit does not exist

I am trying to build a simple Java project with Maven. In my pom-file I declare JUnit 4.8.2 as the only dependency. Still Maven insists on using JUnit version 3.8.1. How do I fix it? The problem manifests itself in a compilation failure: "package…
Are Husby
  • 2,089
  • 2
  • 16
  • 14