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.

3828 questions
129
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,196
  • 13
  • 59
  • 112
123
votes
8 answers

How do I assert an Iterable contains elements with a certain property?

Assume I want to unit test a method with this signature: List getMyItems(); Assume MyItem is a Pojo that has many properties, one of which is "name", accessed via getName(). All I care about verifying is that the List, or any…
Kevin Pauli
  • 7,438
  • 13
  • 45
  • 67
121
votes
6 answers

Mockito: Mock private field initialization

How I can mock a field variable which is being initialized inline? class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock…
Arun
  • 1,822
  • 2
  • 14
  • 23
111
votes
43 answers

No tests found with test runner 'JUnit 4'

My Java test worked well from Eclipse. But now, when I relaunch test from the run menu, I get the following message: No tests found with test runner 'JUnit 4' In the .classpath file I have all jar files, and at the end have:
user281070
  • 1,261
  • 4
  • 11
  • 14
108
votes
5 answers

How to run JUnit tests with Gradle?

Currently I have the following build.gradle file: apply plugin: 'java' sourceSets { main { java { srcDir 'src/model' } } } dependencies { compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar') …
Wang-Zhao-Liu Q
  • 12,331
  • 27
  • 70
  • 106
106
votes
4 answers

Mock static methods from multiple class using PowerMock

I know how to mock static methods from a class using PowerMock. But I want to mock static methods from multiple classes in a test class using JUnit and PowerMock. Can anyone tell me is it possible to do this and how to do it?
Newbie
  • 2,839
  • 9
  • 30
  • 38
103
votes
11 answers

Test expected exceptions in Kotlin

In Java, the programmer can specify expected exceptions for JUnit test cases like this: @Test(expected = ArithmeticException.class) public void omg() { int blackHole = 1 / 0; } How would I do this in Kotlin? I have tried two syntax variations,…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
87
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,581
  • 5
  • 20
  • 28
86
votes
21 answers

java.lang.Exception: No runnable methods exception in running JUnits

I am trying to run the JUnit on my Linux command prompt /opt/junit/ contains the necessary JARS(hamcrest-core-1.3.jar and junit.jar) and class files and I am using the following command to run the JUnit: java -cp hamcrest-core-1.3.jar:junit.jar:.…
vipin8169
  • 4,121
  • 9
  • 33
  • 66
84
votes
16 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
  • 1,949
  • 2
  • 15
  • 14
82
votes
4 answers

maven error: package org.junit does not exist

I'm trying to create the javadoc with maven and it fails. It also fails when doing the verify. mvn verify I get the following error: (...) [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : …
theme
  • 1,017
  • 1
  • 8
  • 12
81
votes
4 answers

Why is assertEquals(double,double) deprecated in JUnit?

I was wondering why assertEquals(double, double) is deprecated. I used import static org.junit.Assert.assertEquals; and I used JUnit 4.11. Below is my code: import org.junit.Test; import static org.junit.Assert.assertEquals; public class…
jsh6303
  • 1,790
  • 2
  • 20
  • 46
79
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
  • 30,487
  • 46
  • 163
  • 272
76
votes
8 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,015
  • 1
  • 9
  • 13
74
votes
5 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
  • 138,188
  • 158
  • 423
  • 589