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
10
votes
5 answers

run a junit test for a single method only

Can you run a test for a single method only, not a whole class? Ideally from the command line. e.g. for a class: public class TestClass extends Unittest { @Test public void test1_shouldbeRun() { } @Test public void test2_shouldNotBeRun()…
forste
  • 1,103
  • 3
  • 14
  • 33
10
votes
1 answer

Unit testing Activity.startService() call?

Attempting to write my first Android-by-TDD app (I've written a few small Android apps without TDD, so am familiar with the environment), but I can't seem to get my head around how to write my first test. The scenario: I have an activity,…
Jules
  • 14,841
  • 9
  • 83
  • 130
10
votes
1 answer

How can I make JUnit 4.8 run code after a failed test, but before any @After methods?

I'm driving a suite of Selenium tests (actually WebDriver-backed Selenium) using JUnit 4.8.2. I'd like the tests to automatically take a screenshot of the browser as soon as the test fails an assertion. All the tests inherit from…
Rowan
  • 2,585
  • 4
  • 24
  • 34
10
votes
1 answer

How can I run Android tests with sbt?

I developed for my application a small suite of Android tests written in Scala that uses the Robotium library. The suite is for all intents and purposes a standard Android JUnit test project and runs successfully if launched from Eclipse. I've…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
10
votes
8 answers

Setup and Tear Down of Complex Database State With Hibernate / Spring / JUnit

I have a class I'm unit testing that requires fairly extensive database setup before the individual test methods can run. This setup takes a long time: for reasons hopefully not relevant to the question at hand, I need to populate the DB…
Brian Ferris
  • 7,557
  • 5
  • 25
  • 27
10
votes
4 answers

Should every possible branch in a method have a separate junit?

This is more of a design question. Suppose you have a method like this (as an example): if (x == 5) { c = 1; } else { if (z != 2) { b = 6; } else { a = 3; } Do you think it's best practice to have a junit for each possible branch? Ie,…
AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
10
votes
6 answers

Has JUnit4 begun supporting ordering of test? Is it intentional?

A newbie to JUnit (in fact JUnit 4) and came across suite way of executing test @RunWith(Suite.class) @Suite.SuiteClasses( { CreateNewProfile.class, EditProfile.class, …
Tarun
  • 3,456
  • 10
  • 48
  • 82
10
votes
5 answers

How to separate unit testing and integration testing on a maven project

I have a maven project and lots of junit classes in it. I develop with Eclipse. I want to separate functional test classes and integration testing classes. When I build the project in Eclipse then I want only the functional test classes to be…
Kayser
  • 6,544
  • 19
  • 53
  • 86
10
votes
5 answers

Writing first JUnit test

So I've read the official JUnit docs, which contain a plethora of examples, but (as with many things) I have Eclipse fired up and I am writing my first JUnit test, and I'm choking on some basic design/conceptual issues. So if my WidgetUnitTest is…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
10
votes
5 answers

Unit tests not running after updated to Android Studio Chipmunk

I have been working on project in Android Studio Bumblebee, and the project contains several packages , some of them have test classes using Junit and Mockito. So inorder to increase code coverage, I had excluded the packages with zero unit tests in…
subin
  • 101
  • 1
  • 5
10
votes
2 answers

Eclipse: how to update a JUnit test file with newly added method in the source file?

Using Eclipse (Helios), I could create a JUnit test file ClassATest.java of the source file ClassA.java by using New -> JUnit Test Case -> Class under test..., then choose all the methods of ClassA to be tested. If later we add some more methods to…
CuongHuyTo
  • 1,333
  • 13
  • 19
10
votes
2 answers

Is mockito supposed to call default constructor of mocked class?

I'm trying to create a Mockito mock object of a class with some rather heavy network and transaction behavior which I don't want to have to deal with in the current unit test I'm writing. It does however seem like Mockito calls the default…
David Nordvall
  • 12,404
  • 6
  • 32
  • 52
10
votes
2 answers

Running JUnit Test in Eclipse opens console panel

I want to ask you a simple question regarding my development environment configuration for running tests. I'm using Eclipse Helios and Junit4 and I put all my tests in a separate source folder. When I run my tests (all, some or just one, it's the…
themarcuz
  • 2,573
  • 6
  • 36
  • 53
10
votes
4 answers

Building a test suite in a large existing Java code base

I am working on a web application with an existing code base that has probably been around for 10 years, there are ~1000 class files and ~100,000 lines of code. The good news is that the code is organized well, business logic is separate from the…
Peter Anthony
  • 487
  • 4
  • 11
10
votes
2 answers

How can I run the IntelliJ debugger on unit tests in a Maven project?

I'm working with a multi-artifact Maven project where artifacts in the project have a few dependencies on each other. I am using IntelliJ 9. I'd like to be able to set breakpoints in my unit tests, but when I right-click on the unit tests folder…
Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
1 2 3
99
100