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

Why Can't I access src/test/resources in Junit test run with Maven?

I am having a problems running the following code: configService.setMainConfig("src/test/resources/MainConfig.xml"); From within a Junit @Before method. Is this the way Maven builds out its target folder?
Michael W
  • 3,515
  • 8
  • 39
  • 62
48
votes
3 answers

JUnit expected tag not working as expected

I have the following test case in eclipse, using JUnit 4 which is refusing to pass. What could be wrong? @Test(expected = IllegalArgumentException.class) public void testIAE() { throw new IllegalArgumentException(); } This exact testcase came…
Ben S
  • 68,394
  • 30
  • 171
  • 212
47
votes
5 answers

how to mock resultset and populate it using Mockito in Java

I have code where I populate Resultset with CallableStatement.executeQuery(). I have mocked ResultSet and CallableStatement but in order to test the method i have to populate ResultSet. Here is the code from the method I am testing ResultSet rset…
Tejas Shah
  • 531
  • 1
  • 6
  • 6
47
votes
12 answers

How to execute JUnit and TestNG tests in same project using maven-surefire-plugin?

Right now I have both type of tests but when I say "mvn test" it only executes TestNG tests and not Junit. I want to execute both one after another. Any Idea ?
ravinikam
  • 3,666
  • 6
  • 28
  • 25
46
votes
2 answers

why cannot we create spy for Parameterized Constructor using Mockito

I have only parameterized constructor in my code and i need to inject through it. I want to spy parameterized constructor to inject mock object as dependency for my junit. public RegDao(){ //original object instantiation here Notification…
Pradeep
  • 1,192
  • 2
  • 12
  • 30
46
votes
8 answers

How to reset between tests

I have a test class @RunWith(SpringRunner.class) @DataJpaTest I have two tests. In every test I do the same operation, persist the object. Only the find call are different. If I run both tests together they fail but if I run test one after another…
robert trudel
  • 5,283
  • 17
  • 72
  • 124
46
votes
1 answer

Pass command line arguments to JUnit test case being run programmatically

I am attempting to run a JUnit Test from a Java Class with: JUnitCore core = new JUnitCore(); core.addListener(new RunListener()); core.run(classToRun); Problem is my JUnit test requires a database connection that is currently hardcoded…
Nick Vallely
  • 1,396
  • 1
  • 13
  • 18
45
votes
2 answers

JUnit4 - AssertionFailedError: No tests found

I'm using AndroidJUnitRunner with Espresso. I wrote a simple test but always receive this exception. According to Stackoverflow answers, the problem is messing up the JUnit3 and JUnit4 but I have never used JUnit3 in my…
Val
  • 4,225
  • 8
  • 36
  • 55
44
votes
7 answers

What do I use instead of Whitebox in Mockito 2.2 to set fields?

When using Mockito 1.9.x I have been using Whiteboxto set values of fields to "inject" mocks. Se example below: @Before public void setUp() { eventHandler = new ProcessEventHandler(); securityService = new SecurityServiceMock(); …
emanciperingsivraren
  • 1,215
  • 2
  • 15
  • 27
44
votes
8 answers

How to write JUnit test with Spring Autowire?

Here are the files that I use: component.xml
Premraj
  • 72,055
  • 26
  • 237
  • 180
44
votes
1 answer

Difference between stub and when in mockito

I am new to mockito. need to know difference between stub and when 1. stub(cpproxy.getBinList()).toReturn(gettestbins()); 2. when(cpproxy.getBinList()).thenReturn(gettestbins()); whats the difference between these two?
Ramya
  • 1,067
  • 6
  • 16
  • 29
43
votes
1 answer

The differences between JUnit 3 and JUnit 4

Could someone describe in a few words what the main differences between JUnit 3 and 4 are?
cody
  • 6,389
  • 15
  • 52
  • 77
43
votes
3 answers

Unit Test Cases with JUnit +(Robolectric or Mockito or both in Android)

This is first time I have to write unit test cases in Android. So I have searched lots of things. Robolectric framewordk - Runs on JVM Mockito Framwork - Mocking objects So I have some doubts in Robolectric & Mokito. Should I have to use…
Deepanker Chaudhary
  • 1,694
  • 3
  • 15
  • 35
43
votes
5 answers

Espresso UI testing doesn't recognize onView()

I have spent all day setting up Junit4 instrumentation tests with Espresso, but just can't seem to get that final step. No matter what I do it won't recognize the onView() method. I have tried multiple SDK/support-lib versions and so far nothing. I…
43
votes
8 answers

NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

Another instance of the NoSuchMethodError for the JUnit & Hamcrest combination. Offending code: assertThat(dirReader.document(0).getFields(), hasItem( new FeatureMatcher(equalTo("Patisnummer"), "Field key", "Field key")…
Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70