I added groovy nature to my java project. My goal is to write Spock tests for my project. I'm using sts 3.6 and Grails. This messes up all my plain Junit test. Now I get a Classnotfoundexception everytime I run one. Also the compiler seems to be confused. It compiles groovy class as a java class. I understand that the problem is that my classes are not built but what I don't know is how to set up a compiler in a java/groovy project. A link, a help, a guide, a tutorial, an advice, all is welcome.
See the Spock example for a complete project that does what you want https://github.com/spockframework/spock-example
It uses both Gradle and Maven for demonstration purposes, but you can select either in your real application.
Related
Context
I have a project with following traits
IntelliJ Ultimate 2020.1
Java 13 (with module-info.java)
Kotlin 1.3.72
JUnit (+ truth)
maven (I believe this to be unimportant)
The code base is mixed, some classes are written using plain Java, others with Kotlin, the same is true for tests. Everything works as expected, that is
all code is compiled in proper order and fully interoperable between Kotlin and Java
all test can be executed using either mvn test or IntelliJ "Run Test"
the resulting jar can be run (for the sake of providing context)
but...
apart from the fact that everything works, IntelliJ warns me about a non declared module dependency only if the test class is written in Kotlin. This warning is not displayed for test classes written in plain Java.
The warning:
Error:(9, 6) Symbol is declared in module 'org.junit.jupiter.api' which current module does not depend on
That warning normally allows one to import / require the respective module / dependency, but there is no solution offered in [alt]+[enter] dialog.
Things I have tried so far:
upgrading from JUnit 4 to 5 didn't change the situation
googling to no avail :(
making sure tests written in Kotlin are really executed when mvn test is run by making a test fail
manually running test using IntelliJ "Run Test"
converting tests back and forth from / to Kotlin
explicitly requiring the the JUnit API / truth in module-info
The latter obviously prevents the warning but is no solution since that actually produces a hard dependency. From what I found out while googling, the maven-surefire-plugin makes sure the test-dependencies are included. Also: running mvn test works like charm as stated above, so this does not seem to be part of the problem.
Seeing all the red lines when writing test is really annyoing...
the suspect behavior
same test but in java - everything is fine
Question:
How can I fix that warning for Kotlin Test Classes in IntelliJ?
Note
I have come to believe this is a bug in IntelliJ but I'd be happy to be shown what I overlooked.
Since everything from compiling to running with Maven works like a charm, I excluded details regarding project structure and so on. The issue at hand is about the warning in the IntelliJ, not about a broken build or non-functional jars. I'll glady add those in case they turn out to be necessary.
also since everthing actually works (apart from the annoying warning), I really don't know where to continue researching and hence created a question.
This is a bug in IDEA Kotlin plugin error highlighting: https://youtrack.jetbrains.com/issue/KT-26037
Workaround: add #file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE") to the test file before the package declaration.
I'm new in Java and Hibernate as well. I'm following this tutorial # http://howtodoinjava.com/2014/09/03/hibernate-4-using-in-memory-database-with-hibernate/
I managed to compile the code and understood the concept as well. But the problem is that I don't know how to execute the code to debug some stuff. When I start app as java application in eclipse. I have a long list to of option. But I don't see my actual test class to run. So How to run it and debug this app? Also I tried this # https://gist.github.com/twasink/2881461. Same is issue. No idea how to run.
Please check the screen shoot of eclipse list while:-
There is a main() method in TestHibernate.java. Use it.
The tutorial that you are following is maven project.
Try creating a simple maven project in eclipse and move the required artifacts to thelocation
as specified in the screenshot in the tutorial.
It will be a good learning curve to you.
As a hint use archetype as maven-archetypes-quickstart while creating maven project.
Hope this helps.
I have an existing Java project set up in Intellij 12 and am attempting to add some Groovy classes to it. I've started with attempting to add a simple Spock class for testing purposes, but when I right-click on the class it does not give me an option to run it.
I've taken a look at my Intellij configuration and it pulls in groovy correctly. Further, I can write a Groovy script that uses a Groovy class and that runs without problems so it appears that Groovy is wired in. Is there something else I need to configure to specifically run Spock tests?
Easiest way to get spock and all dependencies is add library from maven.
Then, you should place your test in a folder, marked as test folder, if you want to allow batch processing of them.
If you place your script in folder, not marked as test, or source folder, you will be unable to run it.
If you can't see run button, it looks like Idea cannot recognise file as runnable, it isn't under source/test root, or it's extension is invalid.
You can add Spock plugin by:
Downloading the jar
In IDEA File->Settings->Plugins->Install from disk. Choose the jar.
I was having the same issue and ended up here. I found that I had forgotten to extend spock.lang.Specification. As soon as I did, the Run option showed up.
Just posting in case it helps any other Spock novices like myself.
I'm fairly new with ScalaTest, and now that I've got it running with Maven, of course I'd like to have it working well in Eclipse as well. My project is a Java project, but I want to improve my Scala skills by writing the tests with ScalaTest.
I understood it so that I should right-click on my project, say "Configure" and "Add Scala Nature". Doing that, however, makes Eclipse try to compile all my Java files with scalac, giving me a lot of "Scala Problem" entries in the problem list. Of course, not having the Scala nature gives me a lot of "Java Problem" entries in my project for all of my Scala files. How can I add the Scala nature only to src/test/scala?
Cheers
Nik
Maybe the simplest solution (in your context, i.e. classic Java project, without M2Eclipse and a Maven project) would be to have two separate projects:
one with only the Java Nature
one with the scala nature for tst.
Since you can link a directory in your second project, you don't have to move the sources of the tests(src/test/scala) from your existing file set.
You only have to exclude src/test/scala from any compilation in the first (Java only) project.
I am new to java and to netbeans so my apologies if this is a simple question.
I made a 'Java Class Library' Project in Netbeans 6.9.1
I added a few classes to it and hit 'Build'. It builds with no errors. However the problem is I know there are errors.
It seems as though I can make up class names and hit build and it doesnt provide me with any feedback.
How can I make it so that netbeans validates my code when buildling a class library?
I am unable to 'run' the project becuase there is no Main. However this is a class library and I dont want to make test applications in my library.
Edit:
For example I can write the following and a 'clean & build' still works
MadeUpName x = new MadeUpName();
This will build even though I have no class or reference to a class that contains
MadeUpName
I also have no 'Import' statments as of yet and it still builds....
Thanks, stephanie
1) To test a Java Class Library project you should create another project and add your "Java Class Library" project as project dependency. In this new testing project you can write class with main method to test features of the class library. When you will build the test project NetBeans IDE will also build the class library project and will add the JAR file in the class path of the testing project.
2) If the Java Class Library projects compiles through "Clean & Build" that means the project has no syntax errors. That means your code may have logical errors which are not detected by the Java compiler. Usually class library developers create JUnit test cases to find out logical errors, and you are also strongly advised to use JUnit tests for your project.
with regards
Tushar Joshi, Nagpur
Ideally you should test a class library by writing unit tests (rather than test applications) that exercise the classes in your library. There are several unit-testing frameworks available that can help you write the unit tests. The most popular is JUnit.
The test cases should be placed in a different source folder in the same project as the code they are testing. When you build the project you should ensure that the test classes are not included in the JAR file. If you use a build tool like Maven it will do this for you as long as you follow it's project conventions.
Update
Based on your comments, and the fact that you tagged your question with Groovy, I'm guessing that the library is written in Groovy? Your problem seems to be that Netbeans doesn't perform the kind of type-checking that you get with Java (or other statically-typed languages)?
Because Groovy is a dynamic language, it's not possible for the compiler to perform the same rigorous type-checking that you get with Java, but at the very least the Netbeans Groovy editor should provide some hints/warnings if you're referencing classes that don't exist (for example). Are you sure you're opening the code in the correct editor (you may need to install a Groovy plugin first).
You should use GroovyTestCase, rather than JUnit directly to test a Groovy library.