Questions tagged [junit4]

Version 4 of the popular Junit Java Unit testing framework

Version 4 of the popular JUnit Java Unit testing framework.

New features are the introducing of annotations to use the framework.

Use this tag only for question related to features provided by version 4. Use for general questions.

4127 questions
63
votes
7 answers

Writing a single unit test for multiple implementations of an interface

I have an interface List whose implementations include Singly Linked List, Doubly, Circular etc. The unit tests I wrote for Singly should do good for most of Doubly as well as Circular and any other new implementation of the interface. So instead of…
ChrisOdney
  • 6,066
  • 10
  • 38
  • 48
62
votes
5 answers

Difference between JUnit Theories and Parameterized Tests

What is the difference between a Theory and a Parameterized test? I'm not interested in implementation differences when creating the test classes, just when you would choose one over the other.
dogbane
  • 266,786
  • 75
  • 396
  • 414
61
votes
21 answers

Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'?

I am migrating my app to androidx, I can't seem to get my unit tests working. I took example from Google's AndroidJunitRunnerSample, which has been updated to use the new androidx api. I get the following error when trying to run my…
J. Nuutinen
  • 1,389
  • 1
  • 7
  • 12
60
votes
2 answers

JUnit assertions : make the assertion between floats

I need to compare two values : one a string and the other is float so I convert the string to float then try to call assertEquals(val1,val2) but this is not authorized , I guess that the assertEquals doesn't accept float as arguments. What is the…
lola
  • 5,649
  • 11
  • 49
  • 61
60
votes
7 answers

Continuing test execution in junit4 even when one of the asserts fails

I have my existing framework built up using Jfunc which provides a facility to continue exection even when one of the asserts in the test case fails. Jfunc uses junit 3.x framework. But now we are migrating to junit4 so I can't use Jfunc anymore…
user85
  • 1,526
  • 5
  • 26
  • 42
59
votes
27 answers

JUNIT Test class in Eclipse - java.lang.ClassNotFoundException

I'm trying to run my junit test (to verify that a properties file loads correctly) but I get ClassNotFoundException although the class is there and all required libraries are there too. Here it is the error I get : Class not found…
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
58
votes
4 answers

When using JUnit's @Parameterized, can I have some tests still run only once

I use @Parameterized in many cases to run tests on a number of permutations. This works very well and keeps the test-code itself simple and clean. However sometimes I would like to have some of the test-methods still run only once as they do not…
centic
  • 15,565
  • 9
  • 68
  • 125
58
votes
7 answers

Java/ JUnit - AssertTrue vs AssertFalse

I'm pretty new to Java and am following the Eclipse Total Beginner's Tutorials. They are all very helpful, but in Lesson 12, he uses assertTrue for one test case and assertFalse for another. Here's the code: // Check the book out to p1 (Thomas) //…
Thomas
  • 5,810
  • 7
  • 40
  • 48
57
votes
4 answers

How to use VisibleForTesting for pure JUnit tests

I´m running pure JUnit4 java tests over my pure java files on my project but I can't find a way to use @VisibleForTesting clearly without making the thing manually public. Ex: @VisibleForTesting public Address getAddress() { return…
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
55
votes
3 answers

Executing JUnit 4 and JUnit 5 tests in a same build

In Maven projects, I have some existing tests relying on JUnit 4. I cannot migrate these tests in JUnit 5 for multiple reasons. Essentially, some tests depend on a library which uses JUnit 4 runner and code migration may take time. I would like all…
davidxxx
  • 125,838
  • 23
  • 214
  • 215
55
votes
7 answers

How do I Dynamically create a Test Suite in JUnit 4?

I would like to create a junit test suite using JUnit 4 where the names of the test classes to be included are not known until the test suite is run. In JUnit 3 I could do this: public final class MasterTester extends TestCase { /** * Used by…
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129
53
votes
5 answers

Cleanup after all junit tests

In my project I have to do some repository setup before all tests. This is done using some tricky static rules. However I've got no clue how to do clean up after all the tests. I don't want to keep some magic static number referring the number of…
Mateusz Chromiński
  • 2,742
  • 4
  • 28
  • 45
53
votes
2 answers

JUnit @Before vs @Rule

I understand that, @Before and @BeforeClass run before each test, or the entire test class, respectively @Rule and @ClassRule wraps each test, or the entire test class, respectively. Let's say I need to initialize some data before each test…
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
50
votes
6 answers

Type Android Junit4 not present exception

I have an Android project that I am trying to run Instrumentation tests for using AndroidStudio, but when I try to run the whole class file for the tests, I get the exceptions as stated below : java.lang.TypeNotPresentException: Type…
firedrillsergeant
  • 695
  • 1
  • 8
  • 20
50
votes
8 answers

Failed to load ApplicationContext for JUnit test of Spring controller

I want to write a test case to check my controller (getPersons). This is a server side code. I have confusion what should i put inside @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app-contest.xml"}) Secondly, I'm getting some…
Saurabh
  • 955
  • 6
  • 14
  • 22