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
87
votes
10 answers

Why can't I import AndroidJUnit4 and ActivityTestRule into my unit test class?

I'm having trouble importing some of the Android UI testing framework classes - I just can't figure out what is going wrong! This is my class: @RunWith(AndroidJUnit4.class) @LargeTest public class ExampleUnitTest { @Rule public…
Hallupa
  • 1,155
  • 1
  • 9
  • 14
85
votes
6 answers

Test cases in inner classes with JUnit

I read about Structuring Unit Tests with having a test class per class and an inner class per method. Figured that seemed like a handy way to organize the tests, so I tried it in our Java project. However, the tests in the inner classes doesn't seem…
Svish
  • 152,914
  • 173
  • 462
  • 620
82
votes
5 answers

Easy way to get a test file into JUnit

Can somebody suggest an easy way to get a reference to a file as a String/InputStream/File/etc type object in a junit test class? Obviously I could paste the file (xml in this case) in as a giant String or read it in as a file but is there a…
benstpierre
  • 32,833
  • 51
  • 177
  • 288
81
votes
9 answers

Cannot resolve symbol InstantTaskExecutorRule

I open example code BasicRxJavaSample (from this article Room+RxJava) The main thing is there: @Rule public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule(); And BasicRxJavaSample is all ok. But I can not apply…
tim4dev
  • 2,846
  • 2
  • 24
  • 30
81
votes
6 answers

Spring MVC controller Test - print the result JSON String

Hi I have a Spring mvc controller @RequestMapping(value = "/jobsdetails/{userId}", method = RequestMethod.GET) @ResponseBody public List jobsDetails(@PathVariable Integer userId,HttpServletResponse response) throws IOException { try { …
iCode
  • 8,892
  • 21
  • 57
  • 91
79
votes
7 answers

What is the correct way to write to temp file during unit tests with Maven?

I have written a unit test that writes a file to the file-system, given no path it writes to the working directory; so if executed from the project directory it writes in the project root, if in the projects parent directory it writes to the parents…
Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
76
votes
6 answers

How to get Junit 4 to ignore a Base Test Class?

I have a base class for many tests that has some helper methods they all need. It does not by itself have any tests on it, but JUnit (in eclipse) is invoking the test runner on it and complaining that there are no methods to test. How can I make it…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
74
votes
14 answers

Test Container test cases are failing due to "Could not find a valid Docker environment"

I am very new to using test containers. My test is failing with below exception. Running com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest 2020-04-08 14:27:08.441 INFO --- [ main]…
Ketan
  • 2,612
  • 5
  • 31
  • 44
74
votes
10 answers

Unit test Java class that loads native library

I'm running unit tests in Android Studio. I have a Java class that loads a native library with the following code static { System.loadLibrary("mylibrary"); } But when I test this class inside my src/test directory I get…
ThanosFisherman
  • 5,626
  • 12
  • 38
  • 63
73
votes
6 answers

How to run all tests belonging to a certain Category in JUnit 4

JUnit 4.8 contains a nice new feature called "Categories" that allows you to group certain kinds of tests together. This is very useful, e.g. to have separate test runs for slow and fast tests. I know the stuff mentioned in JUnit 4.8 release notes,…
Kaitsu
  • 4,094
  • 3
  • 30
  • 37
71
votes
6 answers

setUp/tearDown (@Before/@After) why we need them in JUnit?

I believe that we are all know that setUp (@Before) will execute before any test method and tearDown(@After) will execute after test method. Also we know that Junit will create one instance of Test per test method. my question is that can we just…
mhshams
  • 16,384
  • 17
  • 55
  • 65
71
votes
7 answers

assert that a list is not empty in JUnit

I want to assert that a list is not empty in JUnit 4, when I googled about it I found this post : Checking that a List is not empty in Hamcrest which was using Hamcrest. assertThat(result.isEmpty(), is(false)); which gives me this error : The…
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
71
votes
10 answers

IntelliJ IDEA cannot see Lombok generated code

I have a Gradle-based project that uses lombok. I have imported this project into IntelliJ IDEA 14.1 (using the Import External Model import method). I can run the JUnit4 unit tests without problem in Gradle, but IntelliJ seems to have a problem…
copolii
  • 14,208
  • 10
  • 51
  • 80
68
votes
8 answers

Create multiple parameter sets in one parameterized class (junit)

Currently I have to create a parameterized test class for every method that I want to test with several different inputs. Is there a way to add this together in one file? Right now there's CalculatorTestAdd.java which has a set of parameters that…
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
67
votes
7 answers

How to configure log4j.properties for SpringJUnit4ClassRunner?

Suddenly this keeps happening during a JUnit test. Everything was working, I wrote some new tests and this error occured. If I revert it, it won't go away. Why is that? log4j:WARN No appenders could be found for logger…
user219882
  • 15,274
  • 23
  • 93
  • 138