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
2 answers

spring MockMvc testing for model attribute

I have a controller method for which i have to write a junit test @RequestMapping(value = "/new", method = RequestMethod.GET) public ModelAndView getNewView(Model model) { EmployeeForm form = new EmployeeForm() Client client = (Client)…
nagendra
  • 593
  • 1
  • 9
  • 29
11
votes
3 answers

Setting up Powemockito for static mocking

I would like to make use of Powermock with Mockito to mock some static method calls. I have followed instructions and examples from SO as well as the PowerMock Getting Started and MockStatic pages as best I can, but I am yet to complete a…
Ubunfu
  • 1,083
  • 2
  • 10
  • 21
11
votes
8 answers

Automagic unit tests for upholding Object method contracts in Java?

When developing Java applications, I often override Object methods (usually equals and hashCode). I would like some way to systematically check that I'm adhering to the contract for Object methods for every one of my classes. For example, I want…
Julie
  • 6,221
  • 3
  • 31
  • 37
11
votes
1 answer

How to test afterPropertiesSet method in my spring application?

I am working on writing some junit test for my spring application. Below is my application which implements InitializingBean interface, public class InitializeFramework implements InitializingBean { @Override public void…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
11
votes
1 answer

Play Framework (2.1.3) doesn't run any tests

I have 4 test classes with an average of two test functions each. The first test is below and must be correct (its from Play's tutorial). public class ApplicationTest { @Test public void simpleCheck() { int a = 1 + 1; …
dialex
  • 2,706
  • 8
  • 44
  • 74
11
votes
1 answer

Which dependencies do I need to use Mockito and JUnit in an Eclipse RCP Tycho project

This is my current test fragment: eclipse-test-plugin org.junit com.springsource.org.junit 4.7.0
oers
  • 18,436
  • 13
  • 66
  • 75
11
votes
6 answers

Run JUnit automatically when building Eclipse project

I want to run my unit tests automatically when I save my Eclipse project. The project is built automatically whenever I save a file, so I think this should be possible in some way. How do I do it? Is the only option really to get an ant script and…
cringe
  • 13,401
  • 15
  • 69
  • 102
11
votes
3 answers

How do I write a unit test to verify that a function sorts its result?

I have a data source from which I can request a list of people that live in a (any) country, and a method which retrieves the people from that data source and sorts them by their name alphabetically. How should I write my unit test to make sure that…
f.ardelian
  • 6,716
  • 8
  • 36
  • 53
11
votes
2 answers

Can assertEquals(Long,Integer) succeed?

Currently I am doing some code review and I found this line of code which interrupts the testcase: assertEquals(Long.valueOf(4321), lMessage.getNumber()); getNumber returns an Integer which is also 4321. I changed it to this: assertTrue(4321 ==…
nano_nano
  • 12,351
  • 8
  • 55
  • 83
11
votes
3 answers

Having an issue with mocking a method which has a generic (? extends Collection) return type

I am having a problem with mocking a method with mockito that looks like the following: Map> getValue(); The following is how I am using it in the test: model = Mockito.mock(Model.class); Map> value =…
Malik Firose
  • 389
  • 1
  • 5
  • 14
11
votes
5 answers

Where can I find good unit testing resources for EJB and J2EE?

Which online resources, tutorials or books can you recommended to get started with unit testing J2EE / EJB3 applications? So far I have found ejb3unit, Jakarta Cactus (retired 2011/08) and the Maven Cargo plugin. It would be helpful if there are…
mjn
  • 36,362
  • 28
  • 176
  • 378
11
votes
2 answers

Junit with HSQL/H2 without Spring/Hibernate

I am trying to use H2 or HSQL for my unit testing. But my application is not of spring and hibernate. It seems most of the references are there only with spring and hibernate for HSQL/H2 in memory db for unit testing. Can someone point to a right…
Sripaul
  • 2,227
  • 9
  • 36
  • 60
11
votes
8 answers

Is it possible to ignore certain unit tests?

I'm currently working on a project which uses JUnit4 extensively throughout all the modules. We're using Maven2 to build the project and Hudson for continuous integration. The project is built and run under Java 1.5. We've recently added quite a…
James Camfield
  • 1,636
  • 3
  • 16
  • 24
11
votes
2 answers

Junit: less-than assertion?

Is there anything like assertThat(a, lessThan(b)); ? I'm currently using Junit 4.8.1 and I haven't been able to find lessThan. Instead I have to do assertTrue(a < b), but this has a drawback that it doesn't print two numbers in test log.
Shuo
  • 4,749
  • 9
  • 45
  • 63
11
votes
9 answers

Does JUnit4 testclasses require a public no arg constructor?

I have a test class, written in JUnit4 syntax, that can be run in eclipse with the "run as junit test" option without failing. When I run the same test via an ant target I get this error: java.lang.Exception: Test class should have public…
Thomas Baun
  • 169
  • 1
  • 1
  • 9