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
7 answers

How do I perform a Unit Test using threads?

Executive Summary: When assertion errors are thrown in the threads, the unit test doesn't die. This makes sense, since one thread shouldn't be allowed to crash another thread. The question is how do I either 1) make the whole test fail when the…
les2
  • 14,093
  • 16
  • 59
  • 76
11
votes
1 answer

"folder has not yet been created" error when using JUnit temporary folder in testclass

I get the error "the temporary folder has not yet been created", which comes from an IllegalStateException thrown by the TemporaryFolder.getRoot() method. It looks like it's not initialized, but my research showed me that this is usually the case…
olkoza
  • 715
  • 2
  • 17
  • 35
11
votes
2 answers

CDI testing with Arquillian

3 days ago I completed the Arquillian "getting started" guide and decided that this would be a good thing to use for unit testing of my part of a CQRS system. Unfortunately this has proved a little less than straight forwards. I have googled for the…
Ian
  • 412
  • 1
  • 4
  • 18
11
votes
3 answers

JUnit assertEquals() not working when comparing two objects

I'm trying to get the hang of Java. Unit testing is pretty important to me and so recently I've started to use JUnit. It was tough to begin with but I'm getting the hang of it. All of my tests have worked up to this point, with the exception of…
user4884986
11
votes
3 answers

JUnit Tests: Why is Maven (Surefire) so much slower than running on Eclipse?

I'm working on a fairly big application, with ~260k LOC and roughly 1800 unit tests on the suite. A little background: Multi-modular Maven project All tests run on a separate module and JaCoCo is used to check coverage. Tests are put together in…
Gustavo Ramos
  • 1,324
  • 1
  • 12
  • 23
11
votes
3 answers

Test Activity onCreate Exception

I have the following Activity that throws an exception if something is configured wrong. public class MyActivity extends AppCompatActivity { @Override protected void onCreate(final Bundle savedInstanceState) { …
passsy
  • 5,162
  • 4
  • 39
  • 65
11
votes
1 answer

JUnit Theories: Why can't I use Lists (instead of arrays) as DataPoints?

I've started using the new(ish) JUnit Theories feature for parameterizing tests. If your Theory is set up to take, for example, an Integer argument, the Theories test runner picks up any Integers marked with @DataPoint: @DataPoint public static…
Tyler
  • 21,762
  • 11
  • 61
  • 90
11
votes
5 answers

Failing Android JUnit tests, not breaking my Ant script like I expect?

Failing JUnit tests, not breaking my Ant script like I expect? My continuous integration server runs an Ant script, which calls something like: /tests/ant run-tests My JUnit tests run, but with errors: run-tests: [echo] run-tests-helper. …
Mark Brady
  • 113
  • 4
11
votes
4 answers

Mockito Allow different argument types to mock overloaded method

For JUnit testing I want to mock an overloaded method. There is no need to implement several methods in the mockbuilder though. I want to do something like this: Mockito.when(mock.getSomeInfo(Mockito.any(ArgumentType1.class) OR…
Chry007
  • 521
  • 2
  • 7
  • 25
11
votes
1 answer

java.util.ConcurrentModificationException in JUnit tests

This one is kind of a shot in the dark. I'm getting the following error occasionally when running my robolectric unit test suite: java.util.ConcurrentModificationException at…
loeschg
  • 29,961
  • 26
  • 97
  • 150
11
votes
3 answers

When should we use Mockery vs JUnit4Mockery?

If writing a Java unit test with mocking using JMock, should we use Mockery context = new Mockery() or Mockery context = new JUnit4Mockery() What is the difference between the two, and when should we use which?
Aman
  • 113
  • 1
  • 4
11
votes
2 answers

Passing parameter to @Before setup in jUnit test

Is there any way to avoid calling populateRandomData() method at the begining of each test without having a fixed parameter 100. I need to call the same method to setup data before execution of each test but I need to change the number of test data…
Neil
  • 5,919
  • 15
  • 58
  • 85
11
votes
2 answers

How to mock objects created inside method?

Consider this public class UserManager { private final CrudService crudService; @Inject public UserManager(@Nonnull final CrudService crudService) { this.crudService = crudService; } @Nonnull public…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
11
votes
5 answers

How to use assertTrue?

I have: package com.darlik.test; import org.junit.Assert; public class Test { public static void main(String[] args) { assertTrue(1, 2); } } package with org.junit is set and working but in line with assertTrue i have…
darlik
  • 245
  • 1
  • 2
  • 9
11
votes
2 answers

jUnit same exception in different cases

I'm writing a jUnit test for a constructor that parses a String and then check numerous things. When there's wrong data, for every thing, some IllegalArgumentException with different message is thrown. So I would like to write tests for it, but how…
ryskajakub
  • 6,351
  • 8
  • 45
  • 75