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
28
votes
3 answers

Maven doesn't find org.junit even though it's in the dependencies

I wanted to add a test to my small project (please note I removed some bits from the code & changed package names, so if there's any error you see regarding this it might be not this ;)): package my.pckg; import static org.junit.Assert.*; import…
Michael Weibel
  • 2,857
  • 3
  • 23
  • 25
27
votes
3 answers

Different teardown for each @Test in jUnit

Is there way to define a different teardown for each @Test in jUnit?
Daniel
  • 417
  • 1
  • 7
  • 14
27
votes
6 answers

Does Junit reinitialize the class with each test method invocation?

When i run the below code, both test cases come true: import static junit.framework.Assert.assertEquals; import org.junit.Test; public class MyTest{ private int count; @Before public void before(){ count=1; } @Test …
Prateek
  • 12,014
  • 12
  • 60
  • 81
27
votes
5 answers

junit testing with gradle for an android project

I am trying to get tests ( junit and robolectric ) working in an Android project but am totally stuck. My main problem is that all testing I found with gradle somehow pull in the java plugin and then I get this error: The 'java' plugin has been…
ligi
  • 39,001
  • 44
  • 144
  • 244
26
votes
3 answers

What's the best way to set up a per-test or per-class timeout when using in perBatch forkmode?

I want to fail the build if anyone writes a test that takes longer than 1 second to run, but if I run in perTest mode it takes much, much longer. I could probably write a custom task that parses the junit reports and fails the build based on that,…
26
votes
1 answer

Use @ClassRule in Kotlin

In JUnit you can use @ClassRule to annotate an static field. How can I do this in Kotlin? I tried: object companion { @ClassRule @JvmStatic val managedMongoDb = ... } and object companion { @ClassRule @JvmField val…
lujop
  • 13,504
  • 9
  • 62
  • 95
26
votes
7 answers

spring - @ContextConfiguration fail to load config file in src/test/resources

I've tried to load the spring config file in src/test/resources classpath with the following abstract class: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:/applicationContext.xml"}) public class…
robinmag
  • 17,520
  • 19
  • 54
  • 55
26
votes
3 answers

Exclude specific tests from being run in parallel in jUnit

I recently stumbled upon a simple way to parallelize the execute of tests via jUnit by specifying the following in a java project's pom.xml file: org.apache.maven.plugins maven-surefire-plugin
BSJ
  • 1,185
  • 2
  • 10
  • 15
25
votes
2 answers

Espresso - withEffectiveVisibility vs isDisplayed

Whats´s the difference between isDisplayed and withEffectiveVisibility? onView(withText("Much Dagger")).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))); onView(withText("Much…
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
25
votes
4 answers

@BeforeClass vs static{}

I am writing some test cases using JUnit. I need to initialize some static variables which will be used for all the test cases in that class. For this I can use either Static initializer block or Static method with @BeforeClass What are the…
shantanu
  • 1,748
  • 3
  • 19
  • 34
24
votes
2 answers

JUnit4 run all tests in a specific package using a testsuite

Is this possible in JUnit4? In JUnit3, I would do the following: public class MyTestSuite { public static Test suite() throws Exception { doBeforeActions(); try { TestSuite testSuite = new TestSuite(); for(Class clazz :…
Fortega
  • 19,463
  • 14
  • 75
  • 113
24
votes
9 answers

Running "pure" JUnit 4 tests using ant

We have migrated to both JUnit 4 and ant 1.7 The tests runs fine in eclipse, but the annotations are ignored when running the tests using ant. According to the Ant junit task documentation: It also works with JUnit 4.0, including "pure" JUnit 4…
LiorH
  • 18,524
  • 17
  • 70
  • 98
24
votes
2 answers

JUnit - assertSame

Can someone tell me why assertSame() do fail when I use values > 127? import static org.junit.Assert.*; ... @Test public void StationTest1() { .. assertSame(4, 4); // OK …
Michael W.
  • 531
  • 2
  • 7
  • 17
23
votes
16 answers

Missing "Run as JUnit Test"

I created a JUnit 4 test in Eclipse by right-clicking on a Java class and selecting New JUnit Test Case. When I right-click the test class I get "Run on Server", but not "Run as JUnit Test". I am using Eclipse 3.6.1.
amaran
  • 281
  • 1
  • 2
  • 3
23
votes
2 answers

Spring Boot Integration Testing with mocked Services/Components

Situation and Problem: In Spring Boot, how can I inject one or more mocked classes/beans into the application to do an integration test? There are a few answers on StackOverflow, but they are focused on the situation before Spring Boot 1.4 or are…
swaechter
  • 1,357
  • 3
  • 22
  • 46