Questions tagged [junit]

Popular unit testing framework for Java and Scala. The latest version, JUnit 5, supports rich annotation-based and parameterized tests. Consider using in conjunction with the Java or Scala tag to indicate your use case.

JUnit is a popular unit testing framework for JVM languages (Java, Scala, Groovy, Kotlin, and others). The current version, JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage. Consider using in conjunction with the appropriate language tag (, , , and others) tag to indicate your use case. It is one of the most popular Java frameworks and is used in circa 30% of all projects.

The latest iteration, JUnit 5 (use tag ) adds Java 8 idioms, parameterized tests and comprehensive support for custom extensions.

There are JUnit implementations for other programming languages, which are called xUnit.

Kent Beck, Erich Gamma and David Saff created the unit testing framework influencing Java development heavily.

Grab JUnit's source code at GitHub or even fork it.

Related Links:

27795 questions
11
votes
1 answer

Junit @AfterClass (non static)

Junit's @BeforeClass and @AfterClass must be declared static. There is a nice workaround here for @BeforeClass. I have a number of unit tests in my class and only want to initialize and clean up once. Any help on how to get a workaround for…
jamesw1234
  • 338
  • 4
  • 18
11
votes
1 answer

RxJava Schedules.io() NullPointerException in Unit Tests

I am attempting to test this class in a standard JUnit test, however, I am getting hung up on a NullPointerException with Schedulers.io(). Can Schedulers.io() be mocked? This is an Android application and I am attempting to provide full coverage…
dmfrey
  • 1,230
  • 1
  • 17
  • 33
11
votes
1 answer

Android instrumentation tests with Mockito

I'm trying to use Mockito framework with android instrumentation tests but I can't initialize it correctly. I have the following test class: class MainKontorTest extends ActivityInstrumentationTestCase2 { @Mock Dependency bar; …
Mikhail
  • 3,666
  • 4
  • 30
  • 43
11
votes
2 answers

Is it possible to verify tested object method call with Mockito?

I' ve got some business logic class: public class SomeService { public void doFirst() {} public void doSecond() { doFirst(); } } and test for it: public class SomeServiceTest { private SomeService service; @Before …
Michał Mielec
  • 1,582
  • 1
  • 14
  • 38
11
votes
1 answer

Removing logback `INFO` messages from Maven Console for junit tests

I have placed a logback.xml in src/test/resources from my maven project. My intention is to have a separate logback config for my tests and actual code. My logback.xml looks like below:-
tuk
  • 5,941
  • 14
  • 79
  • 162
11
votes
1 answer

JMeter, JUnit and Spring Java configuration

Is it possible to run JMeter with the JUnit plugin/sampler and Spring Java configuration? When I try to do this, the Spring autowired beans are not being created and although the test case runs, because the beans have not been created, I get null…
Joe
  • 121
  • 1
  • 8
11
votes
1 answer

Do any tools use the hamcrest Factory annotation?

I sat down to write a matcher today and decided to take a quick look at the jmock documentation to refresh my memory on the process, and noticed a reference to the org.hamcrest.Factory annotation. The documentation for the annotation states. Marks…
sonstone
  • 697
  • 6
  • 9
11
votes
2 answers

Server Sent Event Client with additional Cookie

I am trying to unit test a Server Sent Event resource with an additional cookie. I am already using Jersey for the EventSource and JavaX for the client. The following code works fine: WebTarget target = ClientBuilder.newBuilder() …
svenkubiak
  • 645
  • 1
  • 7
  • 19
11
votes
7 answers

Is Unit Testing worth the effort, in a large and old (5yr) codebase?

I've just joined a team which has been working in a main-always mode for the last 5 yrs (java, maven based project). Consequently plans to leverage unit testing have always been in the pipeline, never materialising (so far). A Great dev team has…
VGDIV
  • 679
  • 1
  • 7
  • 16
11
votes
3 answers

Manually instantiating the @InjectMock annotated field

I have gone through some of the blogs in order to understand the basics of how Mockito annotations work. However I am facing a doubt as to when does one go for manually instantiating the field annotated with @InjectMocks i.e @InjectMocks A a = new…
lakshaya agnani
  • 121
  • 1
  • 1
  • 5
11
votes
1 answer

Auto run tests before app build in Android Studio

I've written two test cases LoginTest and SignUpTest using JUnit and a Test Suite SessionUnitTestSuite. Every time I want to build and run my app, I have to run the test suite (by selecting the Test Suite and then clicking in "Run") before and, if…
regmoraes
  • 5,329
  • 3
  • 24
  • 33
11
votes
3 answers

Java Enumerating list in mockito's thenReturn

Is there a way to enumerate the items in a list within mockito's thenReturn function so I return each item in a list. So far I've done this: List returns = new ArrayList(); //populate returns list Mockito.when( /* some function is called…
Richard
  • 5,840
  • 36
  • 123
  • 208
11
votes
6 answers

Is running tests with JUnit 3.x vs JUnit 4.x still a best practice?

I haven't looked at this in a while, but if I recall correctly both ant and maven still rely on JUnit 3 for unit tests (as of maven 2.09 the default POM file still has JUnit 3.81). Does it still make sense to stick to JUnit 3 instead of using the…
agnul
  • 12,608
  • 14
  • 63
  • 85
11
votes
2 answers

How to unit test a class that implements Runnable

I have a class ExampleThread that implements the Runnable interface. public class ExampleThread implements Runnable { private int myVar; public ExampleThread(int var) { this.myVar = var; } @Override public void run()…
ѕтƒ
  • 3,547
  • 10
  • 47
  • 78
11
votes
6 answers

how to use assertj extracting map property

I am using AssertJ. I have a class like MyObj. And I have a List of MyObj. class MyObj { ... Map myMap; ... } When I use: assertThat(list).extracting("myMap"), I cannot use .containsKey() method. I also tried using…
lhoak
  • 131
  • 1
  • 1
  • 4