JUnit - some of my class methods don't appear Eclipse JUnit wizard? - java

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.

Related

Configure set of parameters for a Junit5 parameters

I have a gradle project using Kotlin and JUnit5 if it helps.
Currently I have a bunch of parametrized tests to run using #MethodSource. A tests with all parameters runs for a quite long time, and during local development itn's enough to run lesser with just one value.
Is it possible to define a switch or a configuration to run all tests, but with limited numbers of parameters?
Solutions tested, which didn't work:
Edit source method(s) everytime I need to run locally -> this leads to mistakes like "I forgot to uncomment tests".
Limit tests to a single test with all parameters -> This won't work as I need all tests running on every, may be just with a different parameter.
Make an abstract superclass, extend two with just different annotations -> I have enough tests to skip this solution, I prefer another way if possible.
check System.properties & System.env -> Something didn't work as I'd like to do.

How to skip Junit for some java classes using mavan?

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.

What's the usage of JUnitCore.runClasses?

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.

Use Javassist in JUnit test

I have quite shallow understanding of JUnit and Javassist, i just want to use them to do some program analysis. For example given a library, I want to know during the runtime what methods in the library have been invoked. I can use bytecode manipulation to insert a system.out.println("method_name"); statement in the beginning of a method. So during the runtime, it will print out what methods have been invoked.
In standalone application i can intercept before the main() is called and use my own class loader(see below), however in JUnit there is no main(), could anyone show me how to intercept at this situation?
Many thanks.
...
Loader loader = new Loader( pool );
loader.addTranslator( pool, xlat );
loader.run( className, args );
...
Edit: I use JUnit 4.8 and Javassist 3.15.0.GA
Might I recommend an alternative approach instead? You can use an aspect-oriented approach instead, using AspectJ. With that, you can define pointcuts around a subset of or all methods that you want to monitor.
Another option is, if you're looking to monitor code coverage (the fact that you're using JUnit and just looking to do System.out.println(...) are good hints of this), maybe you're looking for a code coverage tool? If so, Cobertura would be your best bet - with no custom coding required.
Both of these options do their own byte-code manipulation, but without being something that needs to be maintained by the developer.
If you're using Eclipse as your IDE, both of these tie-in very nicely to Eclipse. AspectJ is actually an Eclipse project, but doesn't require Eclipse. The Eclipse plug-in for Cobertura is eCobertura.
Yet another option for this is to do it within JUnit itself - and this wouldn't require any bytecode manipulation. Take a look at its TestWatchman class. I don't yet have this documented online as I do with my other libraries, but you could take a look at my BaseTest class as part of my JUnit utilities library. Any JUnit test class that extends this will automatically log (to SLF4J) when each test starts, succeeds, or fails. However, this is all at a test-level only, and won't help monitor other methods that each test runs.

Creating nunit tests without exporting them with the api

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.

Categories

Resources