Sometimes there is a need to quickly test some expression's output. It would be convenient to execute selected fragment and see the result instantly in System.out without running project's main() method.
Is there some workaround for IntelliJ to provide this feature?
The easiest way is just to stop a debug before what you are trying to test, press ALT + F8 and execute the expression on the window that appears.
You are going to have access to everything declared to that point, so you can execute anything without changing the state of your code.
Use a test framework like JUnit.
If its maven, add a dependency to your pom.xml:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
Then, alongside to your project/src/main/java/mypkg/MyClass.java, add a new path: src/test/java/mypkg/MyClassTest.java
Inside add a function, call it whatever you like.
Add #Test annotation to it, and right-click to execute it.
#Test
public void testA() {
...
}
JUnit is very good practice to your java development applications, as you can test your code carefully and also run small fragments of code quickly. The bonus is that you have your classes and all your deps in your tests to quickly use them as well
I have a project called "untitled" where I copy-paste the code into and run it there. I have classes which have this template setup already.
Another approach is to debug your code and "evaluate expression". You can give it any snippet of code. ie. it doesn't even have to be a statement.
Related
I am using Junit5, and my IDE (IntelijIdea) is not recognize Assumptions. I am actually do not know why, but may be there is some dependecy on Maven I do not connect into project. Below I will show you the sample of my code.
This is my Assumptions import.
import org.junit.jupiter.api.Assumptions;
And this is wrong code (I can not compile it, compiler does not know what is assumeTrue() )
#Test
#EnabledOnOs(OS.MAC)
void testInsertion() {
assumeTrue(isServerUp); //That place is crashing
assertThrows(NullPointerException.class, () -> Connection.insertTheInstance(person),
"");
If you are familiar with this case then, please, share you knowledge) Many thanks!
It's a import problem probably. Try:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
Check if maven import is ok
Close your project in intellij
Delete your .m2 folder in your HOME directory (user/yourusername in windows, /home in linux)
Execute in a terminal in your project: mvn dependency:purge-local-repository clean.
Open your project again
If it's not ok yet, try the junit 4.12. Its have Assumptions too.
assumeTrue method is part of JUnit 4, but you can also use it in JUnit 5 through the
org.junit.jupiter.api.Assumptions class.
For intelliJ - Junit5 combination, make sure to import the following in your class:
import static org.junit.jupiter.api.Assumptions.assumeTrue;
When I debug the JdbcTemplate sourcecode use IDEA,the IDE tips me:'Source code does not match the bytecode'
Screenshot:
and i use mvn to manage my project;my maven pom config is:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.orm</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
This can also happen if you have multiple dependencies that themselves have different versions of the same dependency. This post on the JetBrains site shows how to enable the alternate source switcher in preferences.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206822215-what-does-Choose-Sources-do-and-how-can-I-undo-what-it-does-
Intellij gives a such warning when compiled code doesn't match the source code, i.e. you try to debug the code but it has changed and not rebuilt.
Make sure after you imported your code you didn't modify it. If you modify then first build/compile it before starting the debugger.
For example below code will cause this warning :-
public class HelloSO {
public static void main(String[] args) {
System.out.println("First time source code");
}
}
Now you compiled above class and start debugging it, everything works fine.
But after that you add one more print statement and try to put the debug point on that line without re-compile it, then in this case as byte code for new line is not generated, hence you will get same warning from IntelliJ.
After viewing the other similar questions and answers on this problem, none of which helped me. What solved the problem was simply adding a dependency. In my case I ran into this issue when I was attempting to debug org.springframework.web.servlet.DispatcherServlet. I finally noticed that IntelliJ could not find javax.servlet in my imports.
In my Maven project, I added
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0.1</version>
</dependency>
to my pom.xml which solved the problem.
Double check that all of your imports are being resolved.
You can try this
Open or move to 'Project Panel'
Scroll down and find your library
Left click on your library
Inside the context menu, select 'Open Library Settings'
Once there, check that the binaries and sources are associated:
For me this issue was arising because I'd made changes to my source code but had not yet deployed them to the target device. It still allowed me to set up the debugging which was confusing, but then gave me this error.
To fix:
Rebuild your project / module
Redeploy to your target device
Run the debugger
After rebuilding / redeploying, your debug and deployed code will match up and you shouldn't get any more errors! Just a matter of matching up the two binaries.
I had the same issue, too. The root reason is two different jar packages have some conflicts. So I solved it by removing one of the conflicting jar package.
I had a problem like yours today. I found that my IntelliJ was set to work with Java version 16 and my project was built in Java version 11.
To fix it, I did this: clicked on File -> Project Structure -> Project Settings -> Project, and changed "Project SDK" property to version 11.
After clicking on OK button, I didn't get the message "source code does not match bytecode" anymore.
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
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.
I'm trying to write JUnit4 tests for my web app, and they had been working fine previously. However, now when I try to run the tests by right clicking the class file -> Run As -> JUnit Test I don't see that option. I think it could be because a colleague committed some Eclipse settings/property files on accident that messed with mine. I'm using Eclipse Helios on a Mac running 10.6.X.
I noticed that the icons on the test classes changed from the "filled" J to a "bubble" J and I'm not sure if that is signifying some kind of problem:
I've double checked and made sure that JUnit4 is on my Build Path, and I've gone to the Eclipse -> Preferences -> JUnit pane and verified there are JUnit4 imports being used.
My test classes look like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration( { "classpath*:/resources/action-test-appconfig.xml" })
#Transactional
public class UserControllerTest extends BaseStrutsTestCase<UserController> {
/**
* Tests the ability of a user to change their login username
* #throws Exception
*/
#Test
public void testChangeLogin() throws Exception {
Any thoughts and suggestions are appreciated.
The problem is with the way you are trying to access and run java files in eclipse. You should be observing this empty 'J' icons on your java files. It is a classpath problem, when you click, you are actually accessing the file from the classpath.
To view the Java file, you have to add a reference to your project in the classpath and move it to the top of the classpath list.
Once you do that, then you should be able to run your junits.
I had the same issue, and I restarted eclipse and got "Run as JUnit test" back.
Looks like a bug in eclipse.
That kind of J icon filled to a "bubble" means that Eclipse doesn't recognize your project as a Java project, and therefore doesn't provide Java options such as Run as JUnit.
Try reimporting the project as a Java Project.
Try adding following dependency in the pom.xml of your project in which the test case is located:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.test</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>