I have some classes for which I dont have to write junit classes so I want to skip Junit for some Java classes and want to increase Junit code coverage using Maven. For example, I have placed all Java classes in com.test.xxx package so that I can tell them all classes are to be skipped.
You can use a #Disabled Tag to disable a specific Test. If you just have empty test classes, for whatever reason. Delete them.
If you want to fake code coverage for whatever reason, you can create tests and call your services inside the tests, but do not add any assertations... However this is extremly bad practice and I cant really suggest it to you.
Related
I've seen some examples showing that running a JUnit class with the JUnitCore.runClasses(Test.class). However, we can easily run the JUnit test classes by right clicking the class file and "Run With->JUnit" in most IDEs. Then, my question is: with the IDEs, what's the usage of JUnitCore.runClasses? Is it still necessary to write JUnit classes using JUnitCore.runClasses?
JUnitCore#runClasses is usually used when you want to write a program to tests (i.e., a runner).
Since you're running from inside an IDE, there's probably no reason for you to use it in this scenario.
I'm new to unit testing.
I have a existing maven based project, now my management has asked me to create unit test for all the classes.
So is there any way to automatically generate test classes based on my src package structure.
What I mean is rather than manually replicating the package structure is it possible to automatically generate Test classes.
For instance, if I have com.company.HelloWorld in src folder can it automatically generate com.company.HelloWorldTest in test folder.
Note: actually I do not want the code to be generated automatically just wanted the package structure and test classes to be created within which I can write my own code.
I do not know a way to create unit tests automatically. Actually it is almost impossible. Unit test is a class, i.e. code that is typically written by human.
You want to create templates for unit tests. But if you do this automatically they will be empty and there is no way then to prevent you from running empty test cases and be happy that all tests succeed.
So, taking in consideration that you have a tiny project (45 classes) I'd recommend you to create your tests manually. You can however use Eclipse plugin that helps you to create empty test case in correct package.
I'm working with a large project with 50,000 source files about 5000 tests spread over 2000 classes. These are mostly JUnit 3 with a smattering of JUnit 4. These tests are run on a frequent (daily or weekly) basis from batch files to ensure the product is working and not suffering from regressions or other issues.
I want to enumerate through the source or class files and produce a list of classes and methods that represent the test cases in the project. I can compare this list to determine which tests are not being run by the batch files.
Is there any simple way to do this such as functionality in JUnit? Or is there functionality in JUnit I could programatically drive to get it, e.g. to scan a dir full of class files and figure out which are tests. I realise that I could write code to individually load each class and start examining it for certain characteristics but if something exists I'd like to be able to use it.
The second part of the question, is there any commonly used way to to annotate tests? If I could annotate tests I could potentially filter them and generate batch files on the fly that run them according to the filter criteria. e.g. run all tests which need a network connection, run all tests that use subsystem A and so on.
Is there anything which would help me do this? I realise I could roll my own annotation with a comma separated list of values or something, but perhaps this sort of thing has been formalised and there are tools that work with it.
Tests are classes that extend TestCase (in case of JUnit 3) or annotated as #Test (in case of JUnit 4). So it is not a problem to do either using IDE or programmatically: write program that finds all *class files recursively into your classes folder(s) and tests whether class extends TestCase or is annotated with #Test.
When I run the wizard in Eclipse, not all my methods appear in the list to add to the new test case.
Is this to say I can't test them? Or that if I somehow can, I shouldn't test them?
Thanks.
The Eclipse wizards won't show private methods - you can't call them directly hence you can't test them directly, which might be what you are seeing.
Instead of thinking about which methods you are testing, you might however want to start thinking of testing in terms of testing the behavior of the class.
I'm new to unit testing using nunit (and Java development in general). When creating unit tests for private methods on classes, it looks as though the test file must be in the same package as the class being tested. What is the typical way of avoiding exporting the APIs of the unit tests? Can I make the classes/test methods package-protected? Or do developers typically have a separate build for release that excludes unit test files?
I can tell IntelliJ or Ant not to package JUnit tests in the deployment. I have tests in a separate directory from the source code, which is what makes it possible.
Don't mingle source and test classes together. Keep them separate to make it easier for the tool/script you use to deploy.
The test file does not necessarily have to be in the same package as the class being tested. In fact, it is a good practice to have the test files in a completely separate package, allowing them to test the public API without being concerned with package-level implementation details.
Alternately, you can set up your build script (e.g. Nant) to ignore files containing "Test" when you build your release executable.
Personally my approach is only to test exposed functionality, so you end up testing well encapsulated parts only.
This usually leads my design to contain small classes with well defined functionality, which are easier to test.
Generally, when unit testing you shouldn't be concerned with the internals of what you're testing, so I find this is the best way to approach it.
I also agree it's best to seperate test and production code.
Keep test source code out of application source code. In general, only test exposed functionality. If you really need to test private behavior, create a test object that extends the real object and allows publec access to the private behavior.
I think it's a mistake to move your test code out of the package of the CUT (Class Under Test). At some point you may want to test a protected method or class, and having your test code in another package makes that hard or impossible.
A better solution is to create a separate directory for your test code that simply mirrors the package structure of your production code. Here's what I do:
src/main/java/com/example/Foo.java
src/test/java/com/example/FooTest.java
Then your build script can very simply ignore src/test/** when it comes time for packaging and deployment.