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

Test the error code of a custom exception with JUnit 4

I would like to test the return code of an exception. Here is my production code: class A { try { something... } catch (Exception e) { throw new MyExceptionClass(INTERNAL_ERROR_CODE, e); } } And the corresponding exception: class…
Guillaume
  • 135
  • 1
  • 8
11
votes
3 answers

JUnit tests in eclipse failing when run together

I have a set of 44 JUnit tests that I run using Eclipse (I got those tests from someone else - I am new to JUnit tests). When I run them all together, 24 of them fail. However, if I then run the failing ones individually, some of them do pass after…
pyvi
  • 675
  • 1
  • 4
  • 15
11
votes
2 answers

intellij junit @RunWith not resolved

I have the following test code: package soundSystem; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import…
Z.better
  • 348
  • 1
  • 5
  • 15
11
votes
2 answers

Mockito - internal method call

I have a class called Availability.java and have two methods. public Long getStockLevelStage() { //some logic getStockLevelLimit(); } public Long getStockLevelLimit() { String primaryOnlineArea =…
user2057006
  • 617
  • 4
  • 15
  • 28
11
votes
2 answers

Why doesn't IntelliJ Idea recognize my Spek tests?

I have a Gradle-based Kotlin project with some Spek tests, which are based on JUnit and should be compatible with Idea. But I don't see a "Run" menu item in the context menu. Why? What do I need to do in order to be able to run Spek tests in Idea…
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
11
votes
4 answers

Fortify high: Access specifier manipulation on reflection that is used to invoke a private constructor

I used reflection to invoke a private constructor of a class in order to solve insufficient branch coverage issue shown by sonar scan report. This is the snippet of my code I was working: // reflection to access a private constructor of a class …
Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
11
votes
1 answer

How to run junit tests by maven in terminal with JVM arguments

As mentioned in here we can run test methods using, mvn -Dtest=TestCircle#xyz test But I need to set some JVM arguments before run the test case. Like I need to use -Djava.security.manager -Djava.security.policy=mypolicy.policy How can I tell…
prime
  • 14,464
  • 14
  • 99
  • 131
11
votes
4 answers

Error when defining inner classes in a Test class in JUnit

I'm having some problems when defining inner classes in a Test class inherited from TestCase, for JUnit 3. Scenario is like following: Foo.java public class Foo { public void method() { ... } } FooTest.java public class FooTest extends TestCase…
Gothmog
  • 871
  • 1
  • 8
  • 20
11
votes
2 answers

JUnit and Mocks in Liferay

I need to make JUnit tests using Mockito or PowerMock or smth else but I don't know what to start with. I created testing folder, set mockito, but what should I do next? I couldn't find any examples so Im stucked with it. Can you show me how to…
Al.Boldyrev
  • 486
  • 7
  • 30
11
votes
4 answers

Android plain Junit with Dagger 2

I used to work in MVP and I usually test my presenters using a plain Junit (Not the Instrumentation !) , since Presenters only have the business logic and no references to Android internals whatsoever. Now by switching to Dagger 2 , I understood…
Ivelius
  • 4,953
  • 7
  • 30
  • 54
11
votes
3 answers

Difference between @SpringApplicationConfiguration and @ContextConfiguration

What is the Difference between @SpringApplicationConfiguration and @ContextConfiguration with respect to JUnit test cases?
Prince Varshney
  • 109
  • 1
  • 7
11
votes
3 answers

How to set the Loglevel for a JUnit Test

I am using java logging in my classes. Example: public class MyClass { private static Logger logger = Logger.getLogger(MyClass.class.getName()); .... } When I am writing a JUnit Test for that class, I would like to set the Loglevel to…
Ralph
  • 4,500
  • 9
  • 48
  • 87
11
votes
6 answers

MockMvc returning 404 status

I am trying to write test cases for controller. I do not want to mock out my service as I would like to use these tests as complete functionality tests. I am trying to test this controller: @Controller public class PlanController { @Autowired …
Gaurav Kumar Singh
  • 582
  • 2
  • 5
  • 14
11
votes
3 answers

Java Compare complex objects excluding some fields for JUnit

I tried the Unitils tool which is really great, but doesn't provide you an option for excluding fields. The only way by doing this is to set null the objects and enable the IGNORE_DEFAULTS flag, but in my case, it's not helpful, since I've got some…
Harter Oliver
  • 111
  • 1
  • 1
  • 3
11
votes
3 answers

How to test Maven module project with Spring Boot

I have split a project, based on Spring Boot, into several Maven modules. Now only the war-project contains a starter class (having a main method, starting Spring), the other modules are of type jar. How do I test the jar projects, if they don't…
Stefan
  • 12,108
  • 5
  • 47
  • 66