#Test Annotation in TestNG is not working in Eclipse - java

I have installed TestNG(ver:6.8.6.20130517_2218) in Eclipse (Ver: 1.4.2.20120213-0813) on Windows7(64bit). I verified that TestNG is successfully installed
When I write #Test in one of the sample jave file that I created, Annotation(#Test) is not resolved. It is giving Syntax error.What could be the issue? Please see the attached screenshot for more details

Make sure you are importing annotations.
import org.testng.annotations.*;

Thanks for all your answers and help. I got the mistake what I am doing:
I am just leaving by writing #Test annotation. Instead if I write the annotation along with function this error is not coming. For e.g.
#Test
public void sampletest() { }
then the error which I am facing won't come.

For any future visitors, I was also able to fix this issue by making the test method public.

testng annotations not working.
Inclusion of the following (post by Korey Hinton) helped.
import org.testng.annotations.*; -

TestNG only picks the Files with 'test' in the file Name or Public Class.
For Eg : the File name is App.java it won't run the #Test method but if we rename the file to AppTest it runs.

Related

VS code doesn't resolve junit test

I've made java project on vs code and now I'm trying to run junit tests but vs code sees there's something wrong.I've installed JUnit JAR Downloader ,Java Extension pack and Junit Testfile Generator, but the problem still exists as the image shows.
Error says: package org.junit does not exist
I think this problem is caused by the incorrect placement of the package. We can put the package in the part shown in my picture to solve it.

Is there a way to reload application.yml before test class?

I'm trying to test a method with #SpringBootTest and #ActiveProfile("test") in my class. When I run the new tests, everything works fine and the properties are loaded from the test YML file correctly. But when I run all tests, those same tests load the properties from the production YML file.
I've tried using #DirtiesContext, with BEFORE_CLASS and BEFORE_EACH_METHOD flags, on the new test class but it doesn't seem to help.
I have something similar to the following:
#ActiveProfiles("test")
#SpringBootTest
public class NewTestClass{
...
#Test
public void testThatLoadsPropertiesAndShouldLoadApplicationTestYmlFile() {
...
}
}
What am I missing here? is there a simple way of solving this problem?
UPDATE
I disabled the other Integration Tests on the project using #Disable from Junit 5 and checked if the problem persisted. surprisingly, it did. So I'm suspecting it is not an ApplicationContext caching problem.
Having the problem description provided I can only suggest the that there might be another test suite configured so that the context is setup using the production config (yml). Please consult this answer (the question raised is similar in a way...): https://stackoverflow.com/a/43332643/8085853
Ok, I believe I found the cause of the problem. I'm running all tests using IntelliJ, but "all tests" is being managed by the JUnit plugin and the single tests are being managed by the Gradle plugin. Once I used gradle to run "all tests", everything worked.

Why classes loaded with boot classloader (bootclasspath) doesn't include annotations

I was writing unit tests for my javaagent but faced with behavior of JVM I guess which I wasn't aware before and I curious if there is any explanation or article about it. I tried google it and search on SO but with no success.
I found that classes which are included into boot classpath doesn't have annotations with them. To demonstrate it I created a simple JUnit test
import org.junit.Test;
public class SimpleTest {
#Test
public void myTest() {
}
}
It perfectly runs of course :) but if I configure eclipse project like this:
Then it fails with java.lang.Exception: No runnable methods. I see in the debugger that code which checks annotations cannot find them.
I found answer on SO to my question which is perfectly explains what is going on (I voted up there).
https://stackoverflow.com/a/23502439/2013497
JUnit library is added by Eclipse and it goes to regular classpath whenever bootloaded classes doesn't have a way to reference them.

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

Junit import using * wildcard

I've noticed that when importing JUnit, the * wildcard doesn't always work.
e.g. for the annotation #Test you must import org.junit.Test since org.junit.* doesn't recognize the annotation.
Is there a reason for this, is it something that needs setting? or just a quirk in the way somethings like JUnit are.
FYI, I am using: Junit 4.6, Intelli-J 8.1.3.
Based on your comment above:
I've copy-pasted it and got "annontation type expected".
it sounds to me like it could be a name collision. Are you importing a class or interface named Test from somewhere else? Is there a class named Test in the same package as the one where you're having the problem? It could be that Java is seeing one of these instead of the annotation.
I'm reading something at http://www.velocityreviews.com/forums/t369296-p2-disadvantage-of-using-wildcards-in-import-statement.html that suggests that there's an "optimize imports" setting in IntelliJ that might relate to this.
There's no reason I know of why importing org.junit.* wouldn't give you access to org.junit.Test. In fact, I just tried it in Eclipse, and it works there. Perhaps it's a problem with your IDEA workspace?
I had a similar problem today in Eclipse. I made a static import to org.junit.Assert.assertEquals, but a static import of org.junit.Assert.assertThat fails! And they are in the same class!
I'll bet it's an Eclipse bug. I'm using junit 4.4 and eclipse 3.5
I don't do it, but using import org.junit.*; works fine here, the following test turns on a green light:
import static junit.framework.Assert.*;
import org.junit.*;
public class AppTest {
#Test
public void testApp() {
assertTrue(true);
}
}
Tested with Java 6u16 on the command line, under Eclipse 3.5, under IntelliJ IDEA 9.0 BETA CE. Works everywhere as expected.
alt text http://img18.imageshack.us/img18/7906/screenshotmavenpowermoc.png

Categories

Resources