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
21
votes
8 answers

No suitable classloader found for grab

I have this at the beginning of a class: @Grab(group = 'org.ccil.cowan.tagsoup', module = 'tagsoup', version = '1.2') class MyClass{... I'm trying to unit test this class, but whenever I try to run JUnit 4 tests, I get this error: Caused by:…
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
21
votes
4 answers

JUnit right way of test expected exceptions

Hello guys I was wondering if this way of testing my exception is ok, i have this exception i need to throw in the second test annotation, im receiving as result a red evil bar, and a succeed and a failure, as you can guess the failure is my…
Daniel Vega
  • 478
  • 1
  • 6
  • 21
21
votes
3 answers

AssertEquals(String, String) ComparisonFailure when contents are identical

I'm facing the following scenario: I have an app that spits everything out to the STDOUT (simple company test) and I'm trying to JUnit this. My problem is, when I run the application, it returns me in the Console: (copy and pasted from IntelliJ) Id…
Leonardo
  • 3,141
  • 3
  • 31
  • 60
21
votes
7 answers

PowerMock throws NoSuchMethodError (setMockName)

I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error: java.lang.NoSuchMethodError:…
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
21
votes
5 answers

Ehcache shutdown causing an exception while running test suite

I'm experiencing the following problem. I have a test suit in my project and each individual test runs fine. However when I run them as a suite I some of them fails with the following exception: Caused by: java.lang.IllegalStateException: The dao…
genjosanzo
  • 3,264
  • 2
  • 16
  • 21
21
votes
1 answer

unit testing functions with Apache Camel Exchange as parameter

I am doing java camel development and I want to unit test(junit4) a bunch of functions with Exchange being passed in as parameter. For example : public finalObject getProperty(final Exchange exchange, final String property) throws Exception { …
RoundPi
  • 5,819
  • 7
  • 49
  • 75
21
votes
2 answers

Is there a general way to mark a JUnit test as pending?

Before stepping into the TDD cycle, I like to sketch out the tests that need to be implemented - i.e. write empty test methods with speaking names. Unfortunately I have not found a way to "paint them yellow" - mark them as pending for JUnit. I can…
kostja
  • 60,521
  • 48
  • 179
  • 224
21
votes
5 answers

Can I avoid running junit tests twice in eclipse when using a TestSuite?

I need to do some per-suite initialisation (starting a web-server). It is working fine except that when I run all tests in my project in eclipse my tests run twice. My test suite looks a bit like this: @RunWith(Suite.class) @Suite.SuiteClasses({ …
lexicalscope
  • 7,158
  • 6
  • 37
  • 57
20
votes
1 answer

How to run JUnit5 and JUnit4 in same Gradle build?

I read an answer about Maven but I was wondering how I would achieve this task in Gradle - Executing JUnit 4 and JUnit 5 tests in a same build. Currently my Gradle build only picks up tests with: import org.junit.jupiter.api.Test; My problem is that…
Viv
  • 1,706
  • 1
  • 18
  • 27
20
votes
3 answers

How can I build a configurable JUnit4 test suite?

Guava has an extensive set of tests for collection implementations written in JUnit3 that look like: /* * Copyright (C) 2008 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except…
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
20
votes
3 answers

Android: Robolectric does not support API level 1

This is my basic test class: @RunWith(RobolectricTestRunner.class) public class MainActivityTest { @Before public void setup() { //do whatever is necessary before every test } @Test public void testActivityFound() { Activity…
anthony
  • 7,653
  • 8
  • 49
  • 101
19
votes
2 answers

Equivalent for @RunWith(JUnitPlatform.class) for JUnit5

In my project I'm doing some cleanup and decided to move everything to JUnit5. Till this time, I was using @RunWith(JUnitPlatform.class) Now I want to migrate it to @ExtendWith. Is there any equivalent for this JUnitPlatform.class in JUnit5?
Szymon Żak
  • 491
  • 1
  • 3
  • 17
19
votes
3 answers

Loop through array, each element a JUnit test

I have a JUnit 4 test that loops through an array of test data: public @Test void testAll() { final Object[][] sets = new Object[][] { // SET TYPE VALUE // --- valid sets //…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
19
votes
7 answers

Testing ClassNotFound Exception

I'm trying to test that a class is not found with UnitTest on Android. What's going on: 1. I'm writing an android library with transitive dependencies which are resolved in the host application 2. The developer may remove some dependencies for…
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
19
votes
1 answer

Can I force Intellij-IDEA to run an ignored test?

I wrote a performance test class marked as @Ignore because it takes a very long time to run and I don't normally need to run it. I'd like to be able to right click one of the @Tests and say "run" but when I do that in intellij it doesn't run the…
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356