UnitTest autorunner on Intellij IDEA for java - java

I try to use plugin 'fireworks' http://plugins.jetbrains.com/plugin?pr=idea&pluginId=1106 for autrorunning unittests for java code. but it doesn't work. Is any alternative for it?
Is anybody autroexecute unittests for java code after sources have changed?
PS
My project is tiny - it's just exercise for Algorithms courses so i don't want build complex deployment system on Maven or something to resolve so trivial tasks. So if any plugin for IDEA has exist it will be better for me.
But if you have simple decision on Maven it also can fit to it.

You could bind the ^S to run the tests (^shiftR) that would pretty much give you what you are after(I think).
Then Set the test runner to always save before running.
Then every time you do a ^S the tests will be triggered.

Yes Infinitest also suits this purpose. For larger projects it becomes annoying but for your small project it should work well. Also, it works well in Eclipse.

Related

Automate test from an existing java project

I'm looking for a way to do a Unit tests on an existing project (java project).
I read the legacy code book. It advance to do some refactoring method, because the Unit tests on an existing project are consuming.
Honestly speaking, the project is big one. I found a question in stackoverflow :
Unit Test existing UI code
I'm trying to find the best solution from it.
Another way that I found it, is on Intellij IDE, we can add a plugin JUnit5 https://blog.jetbrains.com/idea/2016/08/using-junit-5-in-intellij-idea/
We create a test case on each package or module. I started creating the unit test classes, I found a lot of errors, warnings, deprecated frameworks. But, it's consuming a lot.
I'm looking again for a solution. In the meanwhile, please can some one suggest a solution, idea to automate unit tests on an existing java project ?
Thanks in advance.

How to run Karate with a Junit runner without using Maven

We rely heavily on logic in our Junit runner and currently call into it with mvn test
The logic mainly consists of
Instantiating a RuntimeHook and binding it to Runner.Builder().hook if an appropriate JVM switch is specified on the command line.
Calling Runner.Builder().clientFactory() with a mock, again if an appropriate JVM switch is specified on the command line.
Last setting minor things like Runner.Builder().tags and Runner.Builder().path
All of this works perfect today. Our main gripe however is Maven is slow.
Is there a way to accomplish the above logic and run without the expensive build time of Maven?
Since you are mixing a bit of Java code, I think you are going to depend on Maven for build + dependencies. That said, there are ways to ask Maven to dump all JAR dependencies into a text file. For example:
mvn dependency:build-classpath -Dmdep.includeScope=test -Dmdep.outputFile=classpath.txt
And then you may be able to shape a command that uses the java binary directly and you probably already know that com.intuit.karate.Main supports all the extension things. The bad news is I don't think we support things like the custom HTTP Client yet, you can see this PR where someone did this for the RuntimeHook. Perhaps you can contribute. Or maybe you have logic in Java code anyway, so calling the Karate Runner directly may be the way to go.
I think maybe the solution you will land on is mvnd - I haven't used it yet, but I'm hearing very good things on Twitter from some experienced Java folks I follow.

Hotreload with tests. playframework how?

So Im currently in a project where we are using Java playframework 2.3.7 with activator.
One of the things I liked about playframework is the hot-reloading feature. I can modify java files save and the changes are compiled and refreshed on runtime.
How do I get that functionality but for testing? I want to be able to run a single test with this hot reloading feature, so that when I save. Tests for the given file (specified by test-only) is re-runned automatically.
There is not such a solution, however you have two choices:
Use IntellJ: To re-run the previous test(s) in IntellJ, you press shift + F10.
Write a watcher: Write a file/directory watcher such as this question/answer here, and then as soon as there are changes, the program, re-runs the test command, such as sbt clean compile test or activator compile test.
Little advice auto running tests: I don't know how complicated your application is, but as soon as you have couple of injections here and there and with additional concurrency; you do not want to run the tests as soon as you put a char in.
Little advice on Test Driven Development: Your approach should be the other way around! You write a test, which fails because there is no implementation; then you leave it alone. You go and write the implementation, then rerun the test to pass it or to get a feedback. Again, you need your cpu/memory power to focus on one thing, you don't want to brute force your implementation. Hope this makes sense!.
Little advice on your Play version: The Play 2.6 is way much better than Play 2.3; you should slowly but surely update your application; at least for the sake of security.
Ok so I found what I was looking for.
For anybody in need of this particular feature in that particular version of play (I'm not sure about other versions) what you need to do is really simple. run activator and put the ~ prefix before test. for example
#activator
[my-cool-project]~test
That will reload your tests when you make a change. if you want to do this for a particular test then you have to do the same but with test-only
#activator
[my-cool-project]~test-only MyCoolTest
hope it helps anyone looking for the same thing

Building a test suite in a large existing Java code base

I am working on a web application with an existing code base that has probably been around for 10 years, there are ~1000 class files and ~100,000 lines of code. The good news is that the code is organized well, business logic is separate from the controller domain, and there is a high level of reusability. The bad news is there is only the very beginnings of a test suite (JUnit); there's maybe 12 dozen tests at most.
The code is organized fairly typically for an enterprise Java project. There is a stuts-esque controller package, the model consists of almost purely data objects, there is a hibernate like database layer that is largely encapsulated within data access objects, and a handful of service packages that are simple, self contained, and logical. The end goal of building this test suite is to move towards a continuous integration development process.
How would you go about building a test suite for such an application?
What tools would you use to make the process simpler?
Any suggestions welcome. thanks!
Start by reading Working Effectively with Legacy Code (short version here). Next I would write a couple of end-to-end smoke tests to cover the most common use cases. Here are some ideas on how to approach it: http://simpleprogrammer.com/getting-up-to-bat-series/
Then when I need to change some part of the system, I would cover it with focused unit tests (refer to the aforementioned book) and then do the change. Little by little the system - or at least the parts which change the most often - would be better covered and working with it would become easier.
I would create a few integration tests. Since they toch a lot of code, you probably will get an error when you screw up bigtime.
I wouldn't 'build a testsuite' as such, but rather before changing some part define a testset for it, and then go about changing it.
I would suggest looking into a test coverage tool (I don't code Java, so no clue what tool the best is for Java). While it does not tell you when you've tested enough, it does tell you when you tested too little ;)
Good luck!
If the project isn't already maven-ized I would do that. Also be sure to use a mocking framework like mockito. Hudson is a nice CI tool that integrates nicely with maven.
It looks like you are going to be writing both unit and functional tests, so JUnit might not be the best fit for this. Have you considered TestNG? Since you only have very few tests right now, you have the option to pick what's best for the job.

Java: Can I run a given method in my program without invocating from main?

Is there a way to launch methods written in Java from Eclipse without having to write all the driver code for it? I would like some effecient way of just right clicking a method, populating it with params and feeding it a debugger.
The closest I can think of is the "scrap page" in eclipse. Don't think you can debug though.
If I were you, I'd use moreUnit plugin + JUnit (+ preferably maven). That way, you can not only write some tests quickly, but they will be organized so that you can have a nice unit test battery afterwards...
This is not part of any standard eclipse plugin that comes in the stock package.

Categories