Questions tagged [junit4]

Version 4 of the popular Junit Java Unit testing framework

Version 4 of the popular JUnit Java Unit testing framework.

New features are the introducing of annotations to use the framework.

Use this tag only for question related to features provided by version 4. Use for general questions.

4127 questions
33
votes
2 answers

Why is JUnit 4 on Android not working?

as the documentation of Android says, "Note that the Android testing API supports JUnit 3 code style, but not JUnit 4." (Testing Fundamentals). It should be clear that JUnit 4 cannot be used out of the box with Android. But why is this the case? Is…
Matthew
  • 816
  • 1
  • 7
  • 10
33
votes
7 answers

Run all tests in Junit 4

I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests…
alexloh
  • 1,606
  • 2
  • 15
  • 29
31
votes
3 answers

How to share JUnit BeforeClass logic among multiple test classes

Currently, all of my JUnit tests extend from a common base class that provides methods tagged with @BeforeClass and @AfterClass annotations - all these really do is setup a bunch of static resources/services for the tests to use. This seems a…
Krease
  • 15,805
  • 8
  • 54
  • 86
30
votes
7 answers

No tests found in TestClass Haven't you forgot @Test annotation?

I am getting an error like this when running my test: org.mockito.exceptions.base.MockitoException: No tests found in TestCase Haven't you forgot @Test annotation? I certainly do have a method annotated with @Test. What am I doing wrong?
Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
30
votes
4 answers

assertEquals, what is actual and what is expected?

I always wondered what exactly is the meaning of actual and expected in assertEquals in libraries like TestNG. If we read the Java Docs we see: public static void assertEquals(... actual, ... expected) Parameters: actual - the actual value …
insumity
  • 5,311
  • 8
  • 36
  • 64
30
votes
3 answers

Cannot instantiate @InjectMocks field named exception with java class

I have a class with user defined constructor. public class Employee { @Inject private MyBean myBean; private String abcd; protected Employee(Parameter1 param1, Parameter2 param2) { //some operations on method params …
Patan
  • 17,073
  • 36
  • 124
  • 198
30
votes
3 answers

JUnit 4 @BeforeClass & @AfterClass when using Suites

When using this approach below, by setting up the jUnit with Suites. We got the problem when all @BeforeClass in every Testclass will be executed before any tests starts to execute. (For each n TestClass file the @BeforeClass runs, then after they…
Andreas Mattisson
  • 1,051
  • 2
  • 19
  • 39
30
votes
3 answers

Mock class in class under test

How I can mock with Mockito other classes in my class which is under test? For example: MyClass.java class MyClass { public boolean performAnything() { AnythingPerformerClass clazz = new AnythingPerformerClass(); return…
NikedLab
  • 883
  • 4
  • 11
  • 31
30
votes
4 answers

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing in Intellij

I used Intellij Idea 12 Community edition. I am trying to create test case for my class by creating test case. When i run my test case it says java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at…
jackyesind
  • 3,343
  • 14
  • 47
  • 74
29
votes
5 answers

How to make JUnit test cases to run in sequential order?

I am using JUnit4. I have a set of test methods in a test case. Each test method inserts some records and verify a test result and finally delete the records inserted. Since the JUnit run in parallel, test methods fail because of some records…
Athiruban
  • 616
  • 1
  • 5
  • 17
29
votes
6 answers

JUnit test class order

I have a java app with maven. Junit for tests, with failsafe and surefire plugins. I have more than 2000 integration tests. To speed up the test running, I use failsafe jvmfork to run my tests parallel. I have some heavy test class, and they…
Sándor Juhos
  • 1,535
  • 1
  • 12
  • 19
29
votes
11 answers

Need help to write a unit test using Mockito and JUnit4

Need help to write a unit test for the below code using Mockito and JUnit4, public class MyFragmentPresenterImpl { public Boolean isValid(String value) { return !(TextUtils.isEmpty(value)); } } I tried below…
Kadari
  • 311
  • 1
  • 3
  • 5
29
votes
7 answers

Spring Boot Application not reading application.properties file when using Maven test

UPDATE: I realized a couple of things now. My application.properties file is being loaded properly because I verified via the /env path (thanks Dave) that my DB properties are being loaded. The problem appears to be that when I run it using the…
John F.
  • 4,780
  • 5
  • 28
  • 40
29
votes
1 answer

List of annotations in JUnit

Recently, I have studied and implemented the JUnit framework. As a result i am aware of few annotations which are used in JUnit :- @Test, @Before, @After, @Ignore, @BeforeClass, @AfterClass, @Runwith(Suite.class), @SuiteClasses({}), @Parameters,…
silver_noodles
  • 361
  • 2
  • 4
  • 12
28
votes
8 answers

junit assertEquals ignore case

im just moving from c# -> java. I need to write some tests using junit. In my test i need to compare two strings to see if they match. So we also have the Assert.assertEquals, but this is case sensitive. How can i make it case insensitive? What i…
Erki M.
  • 5,022
  • 1
  • 48
  • 74