how to use JUNIT using makefile? - junit4

I am very new to JUNIT. I have Makefile to compile my code and which will generate a jar file and now I want to run my JUNIT test case for that.
I not sure how to find out .class file using JAR. I am using LINUX as I know I need a .class file to execute a JUNIT case.
can some one help me?

from http://c2.com/cgi/wiki?UnitTestCookbooks:
I use JavaUnit for my unit tests. For a class Foo, its source is in
Foo.java. Its unit test lives in a separate class, with the source in
TestFoo.java. A command line invokes the unit test, in either console
mode or with a GUI. I use the console mode tester:
java junit.textui.TestRunner TestFoo
I build an "all-in-one" test suite for a package, which runs all of
the unit tests in the package. It's called TestAll. This is usually
the test I run when working on the package.
I add a rule like this to my makefile:
test: TestAll.class
java junit.textui.TestRunner TestAll
when I run "make test", Test'All.class gets built (through another
rule) and then junit is run with the standard command-line.

Related

Run only one package test cases scala

I have the package structure as:
src -> test -> scala -> notification
Inside notification i have two packages unit and integration.
unit package has the unit tests and integration package has integration tests. I want to execute only the unit test cases. Is there i way i can do it by sbt test only?
for one class, i know it can be done like this:
I have tried for the one class but do not know how to do it for a pacakge.
sbt "test:testOnly *LoginServiceSpec"
testOnly allows you to run wildcards using *. So if all your tests are in package namespace x you can call
> testOnly x.*
This wildcard can be put everythere, so if you have a subpackage in x, but all your tests end with e.g. Spec you could run
> testOnly x.*Spec
However if you are using integration test I recommend creating a separate configuration for them so that you would run:
> test
for unit tests and
> it:test
for integration test. Then you would put them in src/it/scala. directory.
Try this
sbt "test:testOnly notification.unit.*"

IntelliJ print test run dependencies

I am working on a scala project which uses sbt for build tools. When we run unit tests on command line 'sbt test', the tests are running fine. However, when I run unit tests in IntelliJ, it seems to be picking up incorrect version of a dependency as well.
I was wondering if there is a way for me to print the classpath that IntelliJ is running the unit tests with?
IntelliJ IDEA already does that, actually, for each test run.
Tests are run as an invocation of JVM with the classpath passed to command-line parameter.
You need to press on the ellipsis to see the whole command line.
Classpath will be there after -classpath argument.
It's better to copy it to another window and enable line wrapping for the further digging.

How to run all Specs2 tests under IntelliJ IDEA?

In my Scala project, my Specs2 tests are structured as follows:
src/test/scala
-> my.package
---> my.package.sub1
------> SomeTest1
------> SomeTest2
---> my.package.sub2
------> SomeTest3
I'm using SBT to build all of this, and I can use sbt test to run all tests in my package.
I'd like to use IntelliJ IDEA's built-in Specs2 run configuration support. I point it to use all tests in my.package.
Running this yields the error message Error running <run config name>: Not found suite class. It cannot find Specs2 test suites. IDEA runs my tests if I point it to a subpackage.
How do I configure IDEA to look in all packages and run all the test suites it finds?
I've managed to run all my Specs2 tests in IDEA 13.1.4 and the more recent 14.0.1 using All in package for Test kind and In whole project or In single module for Search for tests. I left Test Package field empty.
I had to create this configuration manually.
You may want to use Ctrl+Shift+F10 to create a Specs2 configuration and then modify it accordingly.

How to compile tests with SBT without running them

Is there a way to build tests with SBT without running them?
My own use case is to run static analysis on the test code by using a scalac plugin. Another possible use case is to run some or all of the test code using a separate runner than the one built into SBT.
Ideally there would be a solution to this problem that applies to any SBT project. For example, Maven has a test-compile command that can be used just to compile the tests without running them. It would be great if SBT had the same thing.
Less ideal, but still very helpful, would be solutions that involve modifying the project's build files.
Just use the Test / compile command.
Test/compile works for compiling your unit tests.
To compile integration tests you can use IntegrationTest/compile.
Another hint to continuously compile on every file change: ~Test/compile
We have a build.sbt file that is used for multiple projects. Doing sbt test:compile compiled the tests for every single project and took over 30 minutes.
I found out I can compile only the tests for a specific project named xyz by doing:
sbt xyz/test:compile
Using sbt version 1.5.0 and higher test:compile returns deprecation warning.
Use Test / compile.
(docs)

Run tests in broken project using SBT

When doing a serious refactor in a Java Eclipse project I will often break the build, but focus on getting one test to pass at a time. When running the tests Eclipse warns that the project cannot be compiled, but it will still run the tests it can compile.
Now I'm using SBT and would like to achieve the same thing with 'test-only', but it tries to compile the whole project, fails, and doesn't run the tests. How can I tell it to just compile the bits it can and run the tests.
You should add the following task to your project definition:
import sbt._
class Project(info: ProjectInfo) extends DefaultProject(info) {
lazy val justTest = testTask(testFrameworks, testClasspath, testCompileConditional.analysis, testOptions)
}
This is the same as the ordinary test task, but has no dependencies attached at the end. If you'd like it to have dependencies, call dependsOn on the testTask(...) expression and provide the tasks you want it to depend on.
testTask(testFrameworks, testClasspath, testCompileConditional.analysis, testOptions).dependsOn(testCompile, copyResources, copyTestResources)