Is there a way to run a JUnit test for a particular class in IntelliJ without compiling the entire project? - intellij-idea

I have a project in IntelliJ that has a number of syntactically correct and incorrect classes and a unit test that I've written for a particular class.
Is there a way to execute the test even though other classes, completely independent from the current class I want to test, have errors that prevent the entire project from compiling?
The class I want to test has no compile-time errors. When I try to run the test, I'm directed to those other classes with syntax errors.

Replace the Before launch Build step with Build, no error check:
Do this change for the default JUnit configuration (under Defaults node in the tree on the left) so that it's applied to all the new JUnit run/debug configurations automatically.
You can also remove the Build step and compile the test files manually (from the file right click context menu):

Related

Why aren't "internal" types in the main source tree visible in my unit tests?

I've got a Kotlin project in IntelliJ that is structured like this:
main/com/foo/Bar.kt
test/com/foo/BarTest.kt
where Bar is defined as:
internal class Bar
and BarTest is something like:
class BarTest {
private lateinit var bar: Bar
}
I'm getting compiler errors in IntelliJ on the reference to Bar with the message:
Cannot access 'Bar': it is internal in 'com.foo'
The tests, however, compile and run from the command line (via Gradle).
How can I suppress/remove this error when using IntelliJ?
My setup is:
macOS 10.14.1
IntelliJ 2018.2.6
Kotlin 1.3.10
I opened up two projects concurrently using the same IntelliJ installation:
a former project in which I was able to unit test an internal class located in the main source tree (all within the boundaries of the same IntelliJ module)
the current project in which I was unable to reproduce this setup
The fact that IntelliJ still gave no errors in the former setup led me to conclude the issue was not to do with my IntelliJ setup. As #JaysonMinard suggested, I looked at the differences in the Gradle configuration.
The difference was in the current project's top-level settings.gradle, I use the convention of renaming modules, a la:
findProject(':module-blah')?.name = 'blah'
...so as to refer to my Gradle projects by shorter names while having them listed contiguously in my project browser (rather than being scattered around with other files alphabetically). Removing this shortcut restored the behavior I was looking for in my unit tests.

Run cucumber by right clicking on it in IntelliJ

I am trying to simplify the way we run cucumbers. We have a runner for each folder and we always change the runner in a run configuration that has some VM options set.
I can right click on a feature, select "Run feature :" but it doesn't run successfully without the parameters from the run configuration (one of them runs the tests with an in memory database for example)
Is there anyway I can add a default configuration so I can run them directly?
Run -> Edit Configurations
In the left-hand panel, there is a category called defaults. You can set the defaults to cucumber there. However, note that you might have to delete your previously existing configurations to get this to work with files you've already tried. They usually appear in a faded gray.
In addition, you could also add a runner class, which can run (a subset of) your tests from a testing framework. You can provide #CucumberOptions in this runner.

intellij System.getProperty ("user.dir"), how to get consistent result , whether ide, run script/app, or test?

Had a bit of a nightmare getting consistent result from my code when reading "user.dir" property depending on how the code was launched.
I have a gradle project, some code & scripts in src/main/groovy and tests in source/test/groovy, and running code from command line, or in the IDE (run script directly) and running some tests
i'd built myself a new gradle project and was trying to open a file resources in src/main/resources.
when i ran my unit tests, the user.dir = 'project root' (which is what i was expecting). when i built a script and right clicked in the IDE, then the user.dir property returned the current script directory path in source main!
so a lookup in a test returned a different test.
I also tried setting this in build.gradle
//setup proj.dir as project root
System.setProperty( "proj.dir", project.projectDir.toString() )
and build a gradle task to print this - and it shows the project directory as result.
There doesn't appear to a global 'fix or config' in intellij to always get consistent return when reading the property "user.dir" to be project root. unless i missed something?
in the IDE if i look at the run/debug configurations - under defaults the entry for groovy says the working directory is the project root (as id expected). however when i looked under the groovy node and my script there ('testPCE'), then the working directory was set as the scripts current location.
once i found out that i can edit the IDE run/debug configurations i reset the working directory to be the project root, and now get a consistent result. But seem to have to check this on each script if i need to.
so my questions is what can i do/configure/set so that i get an env variable which always returns the project root, whether from command line launch, in gradle, by running by right click of script from IDE, or from tests in the IDE/ or from commnd line
There must, be a 'robust' way to ensure that all lookups on some env variable will return the same answer regardless of how your code is executed.
know i know whats happening, i can adjust my ide to set where i want working directory to be - but have to keep checking. I want something reliable that will always work, whether its IDE, command line, inside tomcat web container etc to get the 'project root' and build paths reliably from this one stable point, that will work consistently
Does anyone know how to do this?
perhaps not ideal bu this is working. I'm trying to load a config file from the resources area that's in the classpath, but my code is a static block in class.
in this case i had to get the classLoader for the Class object and getResource on that (which includes checking across the classpath (i didnt want a spring dependency for this project if i could avoid it).
So the the code that seems to work - without having to resolve "user.dir" property is shown below
//configure standard converters
static {
// class class loader will include resources so file will be found
def configFile = "config/BinderConfig.groovy"
def resource = new File (configFile).canonicalPath
resource = Gbinder.getClassLoader().getResource(configFile)
def binderConfig = new ConfigSlurper().parse (resource)
binderConfig.global.converters.each {
typeConverters << it
}
typeConverters
}

Intellij No Tests Found Kotlin

I am trying to work through the Kotlin Koans in Intellij Idea Ultimate 2017.2 and I am having issues running the tests. I have marked the test directory as such, but when I try to run them as a whole or individually with the triangle arrow in the gutter that Intellij adds to tests I get a "No tests were found" error.
Screenshots are attached.
I have marked the test directory as such, but when I try to run them as a whole or individually with the triangle arrow in the gutter that Intellij adds to tests I get a "No tests were found" error.
You have it marked (incorrectly) as a test resource, not test source. Make sure to open the project by importing from Gradle then IDE will automatically configure the project structure and set the folders categories as needed.

how to set up an automatic compilation of test classes?

Intellij idea not compiles special classes when running test. Now I run the compilation manually via activator console. How can I customize the intellij idea to run automatically when start a compilation of test?
I'm not sure if I got you right but you can try to right click your test class and choose "Run [your_class]". It will compile the needed classes.