Why is my JUnit test not running in Eclipse? - java

It's been 3 hours now and I still didn't find a solution, even though I seem to have read all related questions already.
I am building an Android application and I just want to create a couple of simple Unit Tests that test my basic functions. I don't need to test any Android related logic or activity features.
So I have created a new directory in my solution in which I have created a new JUnit Test Case.
To keep things simple my test methods are not testing much yet, but even when doing a Right Click > Run As > JUnit Test, it's not doing anything.
As you can see in my screenshot the JUnit pane on the left shows my test is terminated but does not show any test that has been executed.
I have created a simple Unit Test in a new Java Project and then it's working. If I repeat the same steps in a new Android Application Project it's not working.
What do I need to do to run my simple Unit Tests?!
Thanks!
(My Compiler Compliance Level is 1.6)

Go to Window -> Show View -> Error Log to see what the actual error is.
For my case it was No test found with test runner 'Junit 5'.
Then one can google for respective solution.

You either don't have JUnit on the build path or you don't have the library (jar) at hand. Make sure both are in place.
I think you will need an unit test suite if all else fails:
#RunWith(Suite.class)
#SuiteClasses({ GpsLocationTest.class })
public class AllTests {
}
There is one more thing you can do: check whether you import the right #Test annotation. Restart Eclipse and clean your project if the problem persists.
You may want to refer to the vogella guide about unit testing.

You can use AndroidTestCase, which inherits from junit.framework.TestCase, not org.junit.Test.

This is related to Memory Issue.
Simply add these line in VM argument:
right click on Junit Test -> Run as -> Run Configuration -> Arguments -> add "-XX:MaxPermSize=512m" under VM argument

The java Build path of the project has Junit4.jar lets say and the Run configuration for the test you are running has Junit5 - then it causes to terminate and nothing happens.

Related

java.lang.Exception: No tests found matching Method using Intellij IDEA

I am experiencing a strange behavior of Intellij IDEA 2016.3. Having a class with method foo and a JUnit test for the method when I get java.lang.Exception: No tests found matching Method foo when running the test. After I do mvn test it succeeds and then running the unit test right after executing mvn command it suddenly runs green. Seems like IDEA does not compile automatically. How can I fix this?
P.S. No settings were altered after upgrading to v. 2016.3
If you're using a theory testing framework like Junit's or Robolectric's, make sure to run the class containing the test you want, instead the test itself. Since these frameworks use the test methods as instance methods instead of static methods, any testing framework looking for a normal public static test won't find anything.
The same issue i got with Gradle (4.5+) + new Build Cache feature
Sometimes it's unable to find new test methods and throws exception (like you mentioned in topic)
Solution: clean .gradle, build and out directories and try again ;)
Well, after "playing" a bit with run configurations of each unit test I noticed that each Run Config has a Build goal preset in the Before Launch option (See pic below):
After changing Build to Build Project the tests run fine.
If you originally run a test named "foo", and then rename it to "fooBar", you must subsequently run "fooBar" with a new Run Configuration.
If you use the same original Run Configuration for "foo" to run "fooBar", it still looks for a test named "foo" which it does not find (thus the Exception) because it was renamed to "fooBar". The new Run Configuration would correctly look for "fooBar" test.
I made this mistake unknowingly because I renamed a test, but then just clicked the green run button in IntelliJ: Doing that runs the last Run Configuration, which in this scenario has the old "foo" name.
Deleting Intellij's out directory fixed this issue for me.
In addition to the other answers here: the error can also happen when you forget #Test before your test method declaration. IntelliJ (2018.1) will still show you the green "Play-Button" for test execution, but that public method in your Test-Class will not be an actual test.
Make sure that your #test methods as well as the test class are public.
Since you got your answer and for others searching for solution,
Look if your test class is extending TestCase abstract class which is a JUnit 3 related. In order to fix this you have to start your method name with "test".
For example public void testFoo().
If JUnit 3 is not the case, you're missing #Test annotation from your JUnit 4 test method.
Note: If you're extending from TestCase and test methods are annotated with #Test and also your methods' names start with "test", then probably you're mixing JUnit 3 with JUnit 4. Don't do that. It will lead to other errors such as methods that annotated with #Ignore will not be ignored if those methods' names start with "test".
This situation can also occur if you do not place #Test annotation above the test method.
Maybe you just give a wrong name for test method.
I met this problem because I used '—' instead of '_' which intelliJ cannot represent.
I had to add test before the test Method Name.
methodtest() does not work but testMethod() worked
Make sure #org.junit.Test is added on top of your method. I forgot them and that fixed it for me!
Make sure you've correct runner mentioned above your class.
I was getting this weird message when I was using runner CucumberWithSerenity.class. When I changed to SerenityRunner.class it got fixed.
#RunWith(SerenityRunner.class)
//#RunWith(CucumberWithSerenity.class)
public class WordPressAppTest {
I'm using Serenity framework for web automation and use below runner class
import net.serenitybdd.cucumber.CucumberWithSerenity;
import net.serenitybdd.junit.runners.SerenityRunner;
import org.junit.runner.RunWith;
I feel IDEA ( 2017.2.6 ) can show some better error message than this
You may check that Run/Debug Configurations in the top of the IntelliJ screen.
Delete all of then with the "minus button" and hit "run" green button again to run the test.
You may reload the project on the maven tab on the right as well.
In my spring mvc project. I solved this problem by adding
#RunWith(SpringJUnit4ClassRunner.class)
to my test class.
In my case, I copied a test from another class and modified it, but while running the test it was still pointing to the previous one.
Build > Clean Project solved the problem

Spock unit tests in grails for individual test does not work in intellij

I am trying to run individual spock unit tests using intellij idea.
Consider:
// rest of code
def "Test Something"() {
// test code below
}
In above test, when I goto the test body and right context menu, I get two kinds of tests for Test Something. One is the grails test and other is the junit test.
Referring to this question, the accepted answer recommends using the jUnit runner. But using it, the code simply does not compile(probably because certain plugins and other classes are not available).
(I am not sure though as this is the desired behavior because I am just running a single test and not all tests. So wonder why is it compiling all classes ,including plugin classes not required by the test target class.)
Using the grails runner, I check the configuration and here is the screenshot:
So nothing looks wrong with the command there.
But the test on running gives Test framework quit unexpectedly error.
I try running same command from grails console(CMD windows) and it runs without any error message.
But on checking the output html files(in target/test-reports) I see that none of the tests actually ran!
So what is going on here and why are not individual tests running?
PS:
When I run All tests using test-app command, tests run as expected. Only individual (unit)tests are not running.
Part of the price paid for Spock's nice test naming, is that you can't specify an individual test to run anymore.
Here are some articles about it. The first seems pretty on-point:
Run a specific test in a single test class with Spock and Maven
This one isn't about running a single test, but has some relevance and talks about Spock's test-name conversions, plus Peter Niederwieser chimes in with comments:
Can TestNG see my Spock (JUnit) test results?
A workaround for this could be the #IgnoreRest annotation. Simply annotate the test you want to run with #IgnoreRest, and then specify that test class to run, and only the annotated test will run. http://spockframework.github.io/spock/javadoc/1.0/spock/lang/IgnoreRest.html
Try using the grails unit test and add the following in the command line part:
-Dgrails.env=development
This will run the test as we change the running environment to development . Hope this will help to everyone facing such problems.

Why are JUnit tests run twice from within Eclipse?

A similar question has already been asked here.
One (unaccepted) answer states:
the test class will always be started directly and then through the
"link" in the suite. This is as expected.
Can someone explain what this actually means and whether or not it is possible to prevent the tests running twice.
When I run the tests from the command line using mvn test they only run once.
UPDATE
I have a test suite defined as follows:
#RunWith(Suite.class)
#SuiteClasses({ TestCase1.class, TestCase2.class })
public class MyTestSuite
{
}
When you run tests in Eclipse on project level (or package level), Eclipse searches all project's source folders for JUnit classes (or selected package). These are all classes with #Test annotations and all classes with #RunWith (probably some more too). Then for all these classes it runs them as tests.
As a result of this behavior, if you have a suite class that references tests classes in the same project, these tests will run twice. If you had another suite that did the same, they would run three times and so on. To understand this behavior try running a suite that contains one test case twice, for instance:
#RunWith(Suite.class)
#SuiteClasses({ TestCase1.class, TestCase1.class })
public class TestSuite {}
Accepted strategy here is to define a suite or suites for a project an run them exclusively. Do not start tests on a project level but run selected suites only.
As far as Maven is concerned, I suspect that its default configuration only picks out suite class and omits test cases. Had it been configured differently, it would behave the same as Eclipse.
Elipse tests 2 classes and give you 2 results.
Maven tests 2 classes and give you one result with 2 sub results.
I think is somethink like this, but still most important thing is that result are
positive! :)
Regards!
Same as this question https://github.com/spring-projects/spring-boot/issues/13750
Just exclude individual test cases and include the suite test cases.

Selenium JUnit Tests failed

I am learning Selenium with the aim of exporting UI tests into JUnit tests and then running them in a CI build.
As a basic test I have a Suite.java and a BasicTest.java which is a test case.
I have used the Selenium export and saved the files to the same location.
When I try and compile the tests it fails.
1st Error - http://pastebin.com/0j37KZ08
To get past this error I add an import to the Suite.java for the BasicTest class, which then gives me the following error:
2nd Error - http://pastebin.com/PKxmdj3L
Source code:
Suite.java - http://pastebin.com/Q6HVNtqT
BasicTest.java - http://pastebin.com/fKCk6iN2
Ant build.xml - http://pastebin.com/x16zHKP0
Any help appreciated.
I don't know if you need a Suite here. The issue seems to be that you are calling addTestSuite() with your BasicTest.class which does not extend junit.framework.TestCase. You should see this if you scrutinize the second error message closely. Of course, you would not subclass junit.framework.TestCase for a JUnit4-style test, so there's a disconnect here as far as what the Suite/framework expects and what you are providing. Maybe just avoid using a suite altogether--personally, I haven't bothered much with suites for some time now. Or, if you are following an example that insists on a suite, you can switch over to the JUnit3-style for purposes of learning.
EDIT: This link should help with JUnit4-style suites if you decide you want to keep the suite.

passing test.includes into intellij idea test runner parameters

for example, in ant i would use something like
fileset dir="src/tests" includes="${test.includes}" excludes="${test.excludes}"/>
in the junit task. there is a Test Runner Parameters field in the Run Configurations in Intellij, but i cant seem to find how to pass that sort of information in.
basic use case: i have abstract tests which have to extend TestCase, but are being run by intellij's test runner (and failing with "no tests found..").
if anyone knows how to do this.. it would be good :)
I am afraid the answer is - this cannot be done this way. Check out the IntelliJ Jira.

Categories

Resources