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

Test default value and setter in same test-case or separate test cases

Would you recommend doing any grouping of test cases within @Test methods, or have one @Test method per test scenario? For example, let's suppose that there are different ways to set the context in an application. Is the following idea…
Ariod
  • 5,757
  • 22
  • 73
  • 103
40
votes
2 answers

Does TestNG support something like JUnit4's @Rule?

Does TestNG have something like @Rule? I am thinking specifically about: @Rule public TemporaryFolder folder = ... Or even @Rule public MethodRule globalTimeout = new Timeout(20); I am aware that I can manually implement these things using setUp()…
Francisco
  • 3,980
  • 1
  • 23
  • 27
39
votes
6 answers

Specifying an order to junit 4 tests at the Method level (not class level)

I know this is bad practice, but it needs to be done, or I'll need to switch to testng. Is there a way, similar to JUnit 3's testSuite, to specify the order of the tests to be run in a class?
K Poole
  • 525
  • 1
  • 5
  • 7
39
votes
6 answers

How to get selected option using Selenium WebDriver with Java

I want to get the selected label or value of a drop down using Selenium WebDriver and then print it on the console. I am able to select any value from the drop down, but I am not able to retrieve the selected value and print it: Select select = new…
WomenInTech
  • 1,141
  • 2
  • 18
  • 25
38
votes
3 answers

How to pass input from command line to junit maven test program

I wrote a junit test to add two numbers. I need to pass this numbers from command line. I am running this junit test from maven tool as mvn -Dtest=AddNumbers My test program looks like this int num1 = 1; int num2 = 2; @Test public void addNos() { …
Achaius
  • 5,904
  • 21
  • 65
  • 122
38
votes
6 answers

Spring JUnit Test Error

I am receiving the following error when I attempt to run my Spring JUnit test. I am just trying to get familiar with creating JUnits using the Spring Framework. JUnit Class: package org.xxx.springdao.mongo_datadictionary; import static…
Robert Brooks
  • 701
  • 2
  • 8
  • 12
38
votes
5 answers

SpringBootTest : No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available:

Hey i have started learing spring-boot junit testing using spring boot Test framework at the time of creating the test case i am facing issues below . import static org.hamcrest.Matchers.containsString; import static…
Prabhat Yadav
  • 1,181
  • 6
  • 18
  • 29
38
votes
5 answers

How to handle ordering of @Rule's when they are dependent on each other

I use embedded servers that run inside Junit test cases. Sometimes these servers require a working directory (for example the Apache Directory server). The new @Rule in Junit 4.7 can handle these cases. The TemporaryFolder-Rule can create a…
Lennart Schedin
  • 1,036
  • 1
  • 13
  • 24
37
votes
5 answers

Junit assert OR condition in my test case

In my test case, I get an integer value: int val = getXXX(); Then, I would like to check if val either equals to 3 or equals to 5 which is OK in either case. So, I did: assertTrue(val == 3 || val==5); I run my test, the log shows val is 5, but my…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
35
votes
7 answers

JUnit tests always rollback the transactions

I'm running a simple JUnit test agains an application DAO. The problem is that I always get: javax.persistence.RollbackException: Transaction marked as rollbackOnly The JUnit test…
Juan
  • 989
  • 1
  • 14
  • 22
35
votes
4 answers

How can I get Eclipse 2018.09 to use the JUnit 4 test runner by default?

I've just upgraded to Eclipse 2018.09 (the problem also occurs on Eclipse Photon), but it doesn't play nicely with a project that uses JUnit 4. The new version appears to run all tests using a JUnit 5 runner by default, which fails with the…
Kaypro II
  • 3,210
  • 8
  • 30
  • 41
35
votes
7 answers

Failed to Load ApplicationContext during Spring unit test

I am trying to run a Junit functional test using Springs java-config for my application context. I am not sure if it is a problem with Spring or Junit... just not sure.. The application runs fine on my local server (hitting the db), but when I try…
Prancer
  • 3,336
  • 2
  • 32
  • 38
35
votes
4 answers

How make JUnit print assertion results

How can I get the results of my JUnit assertions to be printed [to standard output]? I have some tests like this: @Test public void test01() { Position p = getPositionAt('a', 1); assertNotNull("a1 exists", p); assertNotNull("figure…
Petr Přikryl
  • 1,641
  • 4
  • 22
  • 34
34
votes
6 answers

android espresso login once before running tests

I've been trying to cover my Android app with tests and have started using espresso recently. Pretty impressed with it so far. However most of my app's functionality requires that users are logged in. And since all tests are independent, this…
source.rar
  • 8,002
  • 10
  • 50
  • 82
34
votes
2 answers

Can't run JUnit 4 test case in Eclipse Android project

I am new to Java and am trying to run a unit test on a class I am writing. Eclipse (3.5) created the unit test class for me and added Junit4 to my class path. My Class: public class DistanceUtil { public static double metersToMiles( double meters…
Roaders
  • 4,373
  • 8
  • 50
  • 71