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
143
votes
25 answers

Class Not Found Exception when running JUnit test

I am getting this error when running JUnit test in Eclipse: Class not found com.myproject.server.MyTest java.lang.ClassNotFoundException: com.myproject.server.MyTest at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at…
quarks
  • 33,478
  • 73
  • 290
  • 513
141
votes
6 answers

Junit: splitting integration test and Unit tests

I've inherited a load of Junit test, but these tests (apart from most not working) are a mixture of actual unit test and integration tests (requiring external systems, db etc). So I'm trying to think of a way to actually separate them out, so that I…
jeff porter
  • 6,560
  • 13
  • 65
  • 123
140
votes
12 answers

Get context of test project in Android junit test case

Does anyone know how can you get the context of the Test project in Android junit test case (extends AndroidTestCase). Note: The test is NOT instrumentation test. Note 2: I need the context of the test project, not the context of the actual…
peceps
  • 17,370
  • 11
  • 72
  • 79
140
votes
47 answers

Error:(23, 17) Failed to resolve: junit:junit:4.12

Why is it that every time I create a new project in Android Studio, it always comes up with: Error:(23, 17) Failed to resolve: junit:junit:4.12? When I remove testCompile 'junit:junit:4.12' in dependencies it is not a problem…
fathurzero
  • 1,486
  • 2
  • 10
  • 9
140
votes
10 answers

How to intercept SLF4J (with logback) logging via a JUnit test?

Is it possible to somehow intercept the logging (SLF4J + logback) and get an InputStream (or something else that is readable) via a JUnit test case...?
carlspring
  • 31,231
  • 29
  • 115
  • 197
140
votes
4 answers

Can you add a custom message to AssertJ assertThat?

We have a test suite that primarily uses JUnit assertions with Hamcrest matchers. One of our team started experimenting with AssertJ and impressed people with its syntax, flexibility and declarative-ness. There is one feature that JUnit provides…
Patrick M
  • 10,547
  • 9
  • 68
  • 101
139
votes
14 answers

Junit - run set up method once

I set up a class with a couple of tests and rather than using @Before I would like to have a setup method that executes only once before all tests. Is that possible with Junit 4.8?
Bober02
  • 15,034
  • 31
  • 92
  • 178
138
votes
9 answers

Best Practice: Initialize JUnit class fields in setUp() or at declaration?

Should I initialize class fields at declaration like this? public class SomeTest extends TestCase { private final List list = new ArrayList(); public void testPopulateList() { // Add stuff to the list // Assert the list…
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126
135
votes
11 answers

Embedded MongoDB when running integration tests

My question is a variation of this one. Since my Java Web-app project requires a lot of read filters/queries and interfaces with tools like GridFS, I'm struggling to think of a sensible way to employ MongoDB in the way the above solution…
seanhodges
  • 17,426
  • 15
  • 71
  • 93
135
votes
9 answers

How can I make a JUnit test wait?

I have a JUnit test that I want to wait for a period of time synchronously. My JUnit test looks like this: @Test public void testExipres(){ SomeCacheObject sco = new SomeCacheObject(); sco.putWithExipration("foo", 1000); // WAIT FOR 2…
Kylar
  • 8,876
  • 8
  • 41
  • 75
133
votes
19 answers

mock instance is null after @Mock annotation

I try to run this test: @Mock IRoutingObjHttpClient routingClientMock; @Mock IRoutingResponseRepository routingResponseRepositoryMock; @Test public void testSendRoutingRequest() throws Exception { CompleteRoutingResponse…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
133
votes
11 answers

Why use JUnit for testing?

Maybe my question is a newbie one, but I can not really understand the circumstances under which I would use junit? Whether I write simple applications or larger ones I test them with the System.out statements and it seams quite easy to me. Why…
Artem Moskalev
  • 5,748
  • 11
  • 36
  • 55
130
votes
20 answers

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

While running junit test in eclipse I am getting this Exception: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing I've added junit.jar library file. I've tried different versions of junit.jar: 4.4, 4.8, etc. How do I fix this Exception?
user2013948
  • 1,301
  • 2
  • 8
  • 3
129
votes
46 answers

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory

The problem Whenever I run my projects JUnit test (using JUnit 5 with Java 9 and Eclipse Oxygen 1.a) I encounter the problem that eclipse can't find any tests. The description Under the run configuration, eclipse can't even find the method which is…
ShadowDragon
  • 2,238
  • 5
  • 20
  • 32
127
votes
3 answers

Using PowerMockito.whenNew() is not getting mocked and original method is called

I have a code somewhat like this below: Class A { public boolean myMethod(someargs) { MyQueryClass query = new MyQueryClass(); Long id = query.getNextId(); // some more code } } Class MyQueryClass { .... public Long…
user3942446
  • 1,273
  • 2
  • 9
  • 4