Does java source code has test cases? - java

I was not able to find any test cases for java source code. I thought having a look at test cases for some of the advanced java classes would be a great way to learn about the class and also would be a great insight on writing test cases. Are there any test cases that test Java source code ?

You can check out the OpenJDK source code here, and browse around. They're using a Mercurial repo, so you can click on jdk, then browse on the left bar, then test to start digging through the test code.
It's worth noting that this isn't necessarily the most modern way of testing java code - usually, you'd want to use a library like JUnit to manage your tests. OpenJDK is simply using a makefile, to ensure that there are no dependencies when building Java.

Related

How to use AceUnit?

I want to use AceUnit framework for unit test modules. In order to use this framework I have downloaded AceUnit source aceunit-0.3.2-src.tar but found No generator AceUnit.jar in /src/java. So, I used independently downloaded AceUnit-0.12.0.jar to make it work.
To test the framework I executed the example sortTest but Test Program crashed due to segmentation fault.
Is there anybody who have used it already or help me to guide further?
here is a bit of information that hopefully solves the issues for you.
It is normal that the source code does not contain any binaries. You can build the binaries, including the AceUnit.jar, from the source code.
aceunit-0.3.2 is unsupported because it is an old version. Please use the latest version.
Also, the version needs to be consistent. The generator of AceUnit-0.12.0 will not work with aceunit-0.3.2. Please consistently use the same version, i.e. 0.12.0.
For error reports, please include detailed information: What OS do you use, what compiler do you use etc.. Also, a paste of the terminal window contents or log is useful. That can reduce the number of loops we have to take in order to solve your problem.
I hope this helps and after updating you can run the demos and tests. If you still can't, please provide information like OS, compiler and contents of the terminal window.
The very latest version of AceUnit is currently hosted on Github: https://github.com/christianhujer/aceunit it has more features and should be more convenient to use, although support for some exotic compilers like C51, C251, C166 is currently not tested. But it should work perfectly well with GCC, armcc and clang. Practically, there's currently a segfault on clang which will probably be removed by next week.

Testing non-executable library in Netbeans?

I am currently working on a Java library - that is, a bunch of classes that are exclusively intended to be used in other projects. Naturally, it has no main() function.
Now, I want to test my progress. And by "test" I don't mean some professional standardized system; I mean I have a very simple function that I want to run to gather information, which will be modified as the project becomes more complete.
I was hoping I could drop an executable class into the Test Packages folder, and just click Run. Unfortunately, NetBeans complains that there are no main classes found.
So, how do I test a library project, without adding an executable class to my distributable source?
You should absolutely look into unit testing frameworks, such as JUnit. IDEs typically have support for running tests easily, and it looks like Netbeans does too. (I don't use Netbeans myself, but I'd have been shocked if it didn't support JUnit.) It's a lot simpler to do this than to have main methods everywhere. After all, a main method will only test one route through your code - with unit tests, you can have lots of tests, each testing one small piece of your code.
Even if you don't want to go into unit testing in a fully-fledged way (which I'd strongly urge you to, by the way), unit tests can be a very straightforward way of just running some code and experimenting with it. I sometimes use it when developing against a 3rd party library for the first time - leaving unit tests to show and document my understanding of the library's behaviour. (Obviously the better the library and its documentation, the less need there is for this, but it's still useful...)
I have used both JUnit and CPPUnit in Netbeans extensively and find that it is fairly easy to get test coverage for libraries with those tools. IntelliJ IDEA does a decent job with JUnit as well so that is an option if you don't like the Netbeans interface. The xUnit frameworks have gotten me out of a jam many times since they are very good at isolating errors quickly. As Jon said they also help to capture the requirements/behavior of your system so that is an added bonus.

finding execution flow in a huge java code base

I am trying to find the execution flow in a large java code base which is not written by me. I have searched for tools which make that possible (JSonde, JTrace, Java Call Tracert, JavacallTracer), but the problem is that they all should be used with a single java/jar/class file.
The code I am trying to understand is built with Ant and has hundreds of jars. So, it runs using a shell script. I do not know how to use those tools with this code.
I really appreciate your help.
I know, that this is an old question, but now I found a solution and I put it here if somebody else searches the same thing: http://findtheflow.io/#gettingstarted.
I think what you should consider is a code coverage tool. This will report what parts of your code are executed and which are not. There are several such tools to consider. Jacoco is an emerging favourite and is associated with the Emma Eclipse plugin.
The thing to remember about code coverage is that it needs to be driven by something. Normally this is accomplished by running your code's tests (unit or integration).
Finally, once you've comfortable with how to enable code coverage you could also consider uploading and archiving it's results in Sonar.

What is the workflow for using Selenium in Java?

I'm really confused by the Selenium web site. At first they go to great lengths to explain Selenium IDE, but it outputs HTML test cases. But when you go to the Java documentation, it uses WebDriver which is pure java and your HTML test cases are useless. I found an export to JUnit feature from Selenium IDE but it doesn't use the WebDriver api, it wraps it inside the Selenium api, which doesn't even work. I get errors that I can only have one session at a time. I had only one test, made sure using netstat that I didn't have any other software listening on the port and disconnected the selenium instance. It just wouldn't work. Additionally the testcase extended from a deprecated class.
You cannot get back your test case to Selenium IDE from Java code so you can at that point throw the Selenium IDE away.
I converted the test case to pure WebDriver and I got it to work. So what is the recommended workflow for working with selenium and JUnit? Should I forget about Selenium IDE and recording actions in browser and just write everything in Java? Or is it still possible to use Selenium IDE somehow?
Having recently completed a project that used Selenium 2.0, I could find that the Selenium IDE is good only for prototying tests.
There are several drawbacks with the IDE that prevent it from being used to run Selenium tests. I could recall the following:
Typically you would want to run tests in a Suite. While the IDE does have this feature, I found that the IDE lacks a more important feature of running test setup and tear down scripts. This is trivial to achieve in JUnit/TestNG, but quite a pain with the recorded scripts in HTML. In short, the recorded tests aren't maintainable until you use a unit-testing library to run the tests from Java.
Data within the tests cannot be shared across tests; you will need to duplicate data in each test that requires it. This is expected when the tests are stored in a presentation language like HTML.
The default format of the exported tests does not use the page-object design pattern (which works very well for organizing Selenium tests). I didn't attempt creating my own format template for this, but this only convinced me that the best tests involving WebDriver and JUnit/TestNG are written by hand.
The optimal way of using the Selenium IDE is to create recordings of failed tests (by functional testers), instead of directly exporting the tests into your test suite. You could use the IDE to record the preliminary test so that the important aspects of the test (the assert/verify calls) are captured, and then rewrite it in your suite.
I use the IDE to create a "Work Flow Script", convert it into java code. Then I write everything from scratch in Java but with the info from the converted IDE script. That will have all ID's and so on but also in what order you have planned to "click" around, even some parts of it might be copied right off. Anyway it does speed things up a bit, but if you are using the webdriver it will complicate things a bit more and I have not yet moved over to the latest version.
Cheers
Stefan
The point is you can use either one, depending on your goal. In your case, WebDriver sounds like the way to go.
Selenium IDE is useful if you want to generate the HTML test cases.
Selenium WebDriver is useful for writing unit tests in Java (or other languages).
For a clear indication of this from the source, see the SeleniumHQ home page. It has a section on "Which part of Selenium is appropriate for me?", which answers your question.

Writing JUnit tests in Jython

I was wondering whether anyone is using Jython to write JUnit tests? The reason for this is that I try to find a neat way to test my SOAP based web services without the need to generate any code. The reason for sticking with JUnit is that I would love to eventually integrate the tests into my Ant based CI system.
I already had a look at the article http://www.devx.com/java/Article/26602/1954 which describes exactly what I need. Unfortunately, I wasn't able to get it working.
Any thoughts and experiences on this would be of great help.
Thanks,
Alex
I'd rather go the other way round: write tests using python unittest to test java code from python. You could then have doctests as well to test Java code, which is neat.
Then you could attempt integration in ant by creating an ant task that e.g. runs 'nose' to find and execute the tests.
Or you can decide to use Hudson for continuous integration (which is going a long step beyond simple Ant) and never look back... ;)
A nice article with this and more (including a mention of the jython plugin for Hudson):
http://www.jython.org/jythonbook/en/1.0/TestingIntegration.html
I have written jython code to test Java projects in the past. Saves me the headache of typecasting and accelerating the death of my keyboard with excessive keystrokes.
import sys
sys.path.append('path/to/library.jar')
from com.example.library import Foo
import unittest
class TestFoo(unittest.TestCase):
def testFoo():
bar = (2, 3, 4)
foo = Foo(bar)
self.assertIn(2, foo)

Categories

Resources