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

Why is assertEquals false if it is the same date? Hibernate

I'm generating one date and saving in a database through hibernate, and when I get the value and I compare with the value before it was inserted. The result is not equal! I created the date as following Date rightnow =…
Joe
  • 7,749
  • 19
  • 60
  • 110
11
votes
2 answers

Unit-testing of libgdx-using classes

I'm writing a game over libgdx; I'm using the junit framework to simplify unit-testing my code. Now there's part of the code (a map generator, a class converting my own map format into TiledMap...) which I need to test thoroughly, but it uses libgdx…
Calimar41
  • 222
  • 2
  • 11
11
votes
5 answers

JUnit @Parameterized function is run before @BeforeClass in a test suite?

I am using a JUnit test suite to run a few tests, one of which is run multiple times using @Parameterized. I am finding that when I run my tests, the @Parameterized function is run before @BeforeClass. Is this expected behavior or is something else…
Corey Wu
  • 1,209
  • 1
  • 22
  • 39
11
votes
4 answers

Add Spring 3.0.0 java based IOC to JUnit 4.7 tests

There is a doc http://static.springsource.org/spring/docs/2.5.6/reference/testing.html how to add IoC support to junit tests using xml-configuration, but I can not find example for java-based configuration... For example, I have java-based…
Vladimir Mihailenco
  • 3,382
  • 2
  • 24
  • 38
11
votes
2 answers

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

My method in the service and test class : public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) { try { for (int i = 0; i < data.length; i++) { SubModule subModule=new SubModule(); int temp…
user2375298
  • 1,001
  • 4
  • 15
  • 28
11
votes
4 answers

How to start and stop an Tomcat container with Java?

I have a Maven project that starts a tomcat container for pre-integration-tests (jUnit Tests). Most of my tests require that the web-application under tests is restarted. So I'd like to restart the Tomcat container before each jUnit test is…
Sebastian Rapp
  • 133
  • 1
  • 1
  • 5
11
votes
2 answers

Integration test with Arquillian and Wildfly

I try to run integration test with Arquillian and Wildfly. My dependencies in Maven look as follows: org.jboss.arquillian arquillian-bom 1.1.2.Final-wildfly-1
user3318442
  • 163
  • 2
  • 10
11
votes
1 answer

Consecutive Android Junit tests do not reflect the real data in the underlying database

Additional Information: To clarify, the app under test uses a ContentProvider to access the database. Edit: If anyone is willing and able to help me debug this. The full project is available here. In the issue107-contentprovider branch,…
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
11
votes
2 answers

Can I test for multiple thrown exceptions in one test method?

I have a well specified interface and against that I write my JUnit tests: public interface ShortMessageService { /** * Creates a message. A message is related to a topic * Creates a date for the message * @throws…
chillyistkult
  • 959
  • 2
  • 11
  • 22
11
votes
5 answers

Using JMockit to mock autowired interface implementations

We are writing JUnit tests for a class that uses Spring autowiring to inject a dependency which is some instance of an interface. Since the class under test never explicitly instantiates the dependency or has it passed in a constructor, it appears…
SwimsZoots
  • 387
  • 2
  • 4
  • 13
11
votes
3 answers

Hamcrest equal collections

Is there a matcher in Hamcrest to compare collections for equality? There is contains and containsInAnyOrder but I need equals not bound to concrete collection type. E.g. I cannot compare Arrays.asList and Map.values with Hamcrest equals. Thanks in…
Andrey Minogin
  • 4,521
  • 6
  • 38
  • 60
11
votes
2 answers

Is it possible to use different @Before @After for each test case in JUnit?

I am new to Java & JUnit and came across different Fixtures. I searched a lot on net but I couldn't get the answer. Is it possible to use different @Before @After for different test case in JUnit? For eg: I have the following TC then is it possible…
Amrit
  • 2,295
  • 4
  • 25
  • 42
11
votes
2 answers

JUnit testing custom exception

I'm using JUnit and not quite sure how to test custom exception class. I have created, public class CustomException extends Exception { //@param message is the exception message public CustomException(final String message) { …
Hash
  • 7,726
  • 9
  • 34
  • 53
11
votes
1 answer

Difference between runOnUiThread() method and @UiThreadTest annotation

As the title, can anyone explain the difference between the runOnUiThread() method and the @UiThreadTest annotation? I've been reading through the Android testing tutorial (http://developer.android.com/tools/testing/activity_test.html) which uses…
barry
  • 4,037
  • 6
  • 41
  • 68
11
votes
1 answer

How to test that method has added redirectAttributes ?(by MockMvc)

I have wrote this method: @RequestMapping(value="/someURL", method=GET) public String myMethod(RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute("rd", "rdValue"); redirectAttributes.addFlashAttribute("fa", faValue); …
user2740224