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
19
votes
1 answer

How to exclude all JUnit4 tests with a given category using Maven surefire?

I intend on annotating some of my JUnit4 tests with an @Category annotation. I would then like to exclude that category of tests from running on my Maven builds. I see from the Maven documentation that it is possible to specify a category of tests…
Adam
  • 43,763
  • 16
  • 104
  • 144
19
votes
3 answers

Mockito: when Method A.a is called then execute B.b

I'm using Mockito for JUnit tests. So there is a given class A that is used from the code i want to test: class A{ public A(){} public final String a(String x){ return "A.a: " + x; } } and i want to replace the Method call…
user1448982
  • 1,200
  • 3
  • 12
  • 22
19
votes
3 answers

Apply '@Rule' after each '@Test' and before each '@After' in JUnit

I have a test suite where I am logging out of the system in @After and closing the browser in @AfterClass. I am trying to use @Rule to take failed test screenshot using Selenium for every test method. I checked manually that @Rule only runs before…
Reaz Patwary
  • 824
  • 2
  • 7
  • 23
18
votes
1 answer

@Before/@BeforeEach inheritance behaviour change JUnit4 / JUnit5

When migrating from JUnit4 to JUnit5 I found a change in the behaviour of JUnit4 and JUnit5 and wanted to check if the change was a bug in JUnit4 or in JUnit5 and how to do it correctly. Lets assume the following structure: One base class public…
ChristophE
  • 760
  • 1
  • 9
  • 21
18
votes
1 answer

How can I verify the content of a POST Body with MockWebServer?

I have several unit tests that use Squares OkHttps MockWebServer. The tests run all fine, and everything works very well. The one thing I couldn't do so far is to verify the content of the POST requests. Why do I want to do that? The REST Api I'm…
ASP
  • 773
  • 1
  • 8
  • 22
18
votes
5 answers

System.out.print doesn't output to console when running from Junit

When running: public static void main(String... args) throws InterruptedException { while (true) { System.out.print("."); Thread.sleep(200); } } vs. when running same code from junit: @Test public void test() throws…
Lika
  • 1,043
  • 2
  • 10
  • 13
18
votes
3 answers

Inject mock into Spring MockMvc WebApplicationContext

I'm working to test (via JUnit4 and Spring MockMvc) a REST service adapter using Spring-boot. The adapter simply passes along requests made to it, to another REST service (using a custom RestTemplate) and appends additional data to the…
ethesx
  • 1,339
  • 5
  • 19
  • 35
18
votes
5 answers

Error creating bean with name defaultServletHandlerMapping

I converted all my XML Spring configuration to Java code ones, but i am not getting able to run all my test (which they worked before) because i have an ugly exception: org.springframework.beans.factory.BeanCreationException: Error creating bean…
user3697569
18
votes
3 answers

JUnit test failing but showing identical Expected and Actual results

So, I don't even know where to start with this one. This whole fiasco began over eight hours ago, jerry rigging my way out of one issue after another. Now, this: And it gets even better, after clicking "Click to see difference": I have: -…
nihilon
  • 834
  • 2
  • 8
  • 23
18
votes
4 answers

Is it possible to programmatically generate JUnit test cases and suites?

I have to write a very large test suite for a complex set of business rules that are currently captured in several tabular forms (e.g., if parameters X Y Z are such and such, the value should be between V1 and V2). Each rule has a name and its own…
Uri
  • 88,451
  • 51
  • 221
  • 321
18
votes
1 answer

Generating JUnit reports from the command line

I have a test setup for a cloud system that uses a mixture of python for process level control and junit for internal state inspection. Essentially, I bring up several VMs to server as the cloud and then a junit VM which is a member of the cloud…
Matt
  • 2,139
  • 1
  • 16
  • 20
18
votes
6 answers

Testing drag-and-drop files onto application

I am looking for a method to perform a drag-and-drop of a file/multiple files onto my application from a unit test. For example selecting some files in Windows Explorer, drag them and drop them on my application. I am capable of testing…
Robin
  • 36,233
  • 5
  • 47
  • 99
17
votes
2 answers

How to mock UUID.randomUUID() from java.util package?

What is wrong with the UUID.randomUUID() - it just can't be mocked Is it possible to mock? Or i have an error in my source? Look at example: 1) Class that is tested package com.grayen; import java.util.UUID; public class TestedClass { public…
Maksim Petrov
  • 313
  • 1
  • 2
  • 7
17
votes
2 answers

How to pass command line arguments to tests with gradle test?

I am using gradle to run JUnit tests. The problem is that I need to pass arguments from the command line to tests. I tries to pass System properties but failed. gradle test -Darg1=something Here is my test: public class MyTest { @Test …
Oleksandr
  • 3,574
  • 8
  • 41
  • 78
17
votes
2 answers

Testing EditText errors with Espresso on Android

I want to test if an EditText field has an error (set with editText.setError("Cannot be blank!")). I've created an Espresso test case with the new AndroidStudio 2.2 feature, to record Espresso tests. So the code is pretty much auto-generated. But…
Michael Woywod
  • 448
  • 4
  • 12