A basic question on java/maven/testng - java

I am using RHEL 5. I want to be able to write a basic Java class, write corresponding test methods using testNG and be able to say something like "mvn install" which will run test cases and install the built jar.
Is there something called maven plugin to support testNG on RHEL? or there is no such thing and above can be achieved by default on linux. ALso how?

The usual maven plugin for testing, the maven-surefire-plugin, handles testng as well as junit.

There are a number of plugins for Maven that will auto-run your tests. I would suggest starting with an IDE solution, such as one provided by the Spring STS eclipse-based suite, which has pre-built project templates, including *.pom files already set up.
If you are looking to do more than that "out of the box" functionality, reading through the Maven docs and perhaps a book would probably be a good idea. In general, whenever you have a really powerful and flexible tool like Maven, things can get complicated really fast, so having a solid understanding is critical to be able to use the tool successfully.

Related

How can I use Arquillian without Maven?

I'm making my first Java EE project and can't figure how to test my EJB. I've read that Arquillian is really useful to do integration tests, but it seems like using it without Maven is difficult. Could someone tell me if there is a way to use it or a tutorial?
Are you using any sort of dependency management tool, eg Ivy? I really recommend using one, since it makes things much easier.
That said, Arquillian does not use Maven itself, so you can use it without Maven. You will just have to figure out which dependencies you need. You can either work it out by just looking at the Maven POM files, or you could start with a sample Maven based setup and use the maven-dependency-plugin to export the libraries you need. See also https://gist.github.com/mojavelinux/2363038.
Personally I would recommend to just use a dependency management tool, be it Maven or Ivy.

Is there a Java continuous testing plugin for Maven?

I'm looking for a plugin that would run in a console continuously to scan a Maven project's test sources directory, and when it detects a change kicks off a test cycle. Something analogous to mvn scala:cc or the Scala Build Tool, but for Java. Can anyone point me towards one?
I have personally used sbt even for a java only project just for continuous test feature.
I added a sbt build file to a maven based project and use sbt when developing, but use maven when building the final package, starting embedded jetty etc and this has worked out quite well.
I've just discovered that the scala-maven-plugin supports both continuous compilation & testing, as well as cross-compilation (Java + Scala). So it's possible to use it over a pure Java build and get all the continuous build goodness.
Recently, I have had a need for a solution to this. Having been learning scala and finding about the goodness of sbt ~test, I want to apply it to Java projects that do not have continuous test.
Using the scala-maven-plugin that you mentioned, I have created a github seed that will run Java Junit tests everytime I save a Java source or a Java test.
Check it out:
https://github.com/ailveen/maven-scala-java-continuous-test
The project is very simple right now (contains only Java files because that is my current need) but in the future I hope to add scala test (or specs2 or scalacheck) so it works with mixed java and scala files.
Hope you find it useful.
It is not exactly for Maven, but JUnit Max does continuous testing and if you're on Eclipse it might be the tool you would like to check out

Why do I need Maven if I use Eclipse?

I have seen that if I right click on a project in Eclipse and choose to run it on a server, then I can see output which means the project is running.
If everything is working fine without Maven, what's the point of using it. How is it different than simply running it via eclipse?
Maven is a build tool (build manager, in fact), similar to ANT. The main job of any build tool is configure the project, compile using required projects and do the final packaging. A build script in your project gives a blue-print of project's deliverable structure. This frees you from any configurable dependencies on specific IDE like Eclipse. All you need to know is the standard command to perform the build and you can build your code almost anywhere.
Now, back to your question, why wouldn't do it in Eclipse?
For a simple project and small team Maven is an overkill. You can easily communicate the configuration, IDE to use, and instruct any special steps to be taken. In big projects, however, there exits lots of loosely coupled dependencies. To start with, there will be different settings for developer machine build, test build and production build. There are requirements to run automated test, integration tests, store the build package (artifact) to a commonly accessible repository, update versions of various modules.
Obviously, if all the steps mentioned above is done manually there are chances of missing a step. Moreover, the manual process is time consuming.
Ideally, you should prefer a tool which fits the best for you. If you think that you're able to achieve what you required without Maven, it makes sense to not to use Maven/build-tool just because everyone uses it.
It is suggested to study automated deployment, this will give you bigger picture on what all the stuffs that you can do with build tools. And if you do not feel that it adds any value to your current process, you probably don't need Maven or any other build tool right now.
Your question does not make much sense. Do you expect your users to access your application from eclipse? If so that is a very strange set up in my opinion.
Perhaps your question should be about how to build your project. Maven provides you a way to centralize dependency libraries across the enterprise. It lets you automate your build process (most likely in conjunction with a CI server like hudson, cruise control, etc). It lets you automate your unit testing. Maven makes the packaging of app very easy to do. A developer does not have to follow arcane set of steps to package an application. You add the right plugin and maven takes care of it as part of the build life cycle. All of this magic can happen because of the principle of convention over configuration. There are many more benefits, I just named a few.
Maven is not replacing how you run the app, rather how you package the app, automate that process, and manage the dependencies of your app.
Some links on why someone should use maven:-
Why maven ? What are the benefits?
why I use Maven
Why you should use Maven
Use Maven

Why is my Mockito-based unit test unable to run in Eclipse?

I have a Maven-managed project that uses Mockito mocking in its unit tests. I can run all the tests within a Maven build, and they run without error (and pass!). However, if I right-click a single test function, and choose "Run As -> JUnit Test", I get an exception java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing/Answer;)Lorg/mockito/stubbing/Stubber;. Of course, the "missing" method is there if I look at the sources, and like I said the tests compile and run from the command line.
Best I can think of is if Eclipse is trying to "help" me by providing an outdated Mockito artifact (I'm using 1.8.5 in my Maven dependencies) for the JUnit plugin, akin to how the Maven plugin can stick you with an oddball version of the Maven runtime for certain tasks.
Is this the problem? Is it something else? Can I fix this?
ETA: Apparently this may relate to a known issue. There's a good chance that it stems from having multiple versions of Mockito in my classpath (thanks, Maven :-/ ). I seem to have my house in order -- Eclipse can run the tests now -- but unfortunately the bug has bitten my Hudson. I have to track down how to remove the old JAR from the classpath there as well.
Make sure the unit-test classpath has the correct mockito. You can check this from the run dialog. Btw, Eclipse does not ship with mockito, so perhaps you are having two versions of it. Take a look at your maven dependency graph and search for duplicates.
I had the similar problem and I found that I had both "mockito-all 1.8.x" and "mockito-core 1.9.5" in my classpath. I was supposed to use only 1.9 but somehow eclipse was putting 1.8 before 1.9.5 in the classpath. I removed 1.8.x and it worked ;)

Environment for java + scala + lift project in eclipse without maven

I'm trying to understand what's the best way to setup a project
that is a mix of java and scala and that will use lift.
Lift is intended to run embedded by jetty in another application.
I'm comfortable to work in Eclipse and also used to how it
works with ant as build tool.
I'm a relatively newbie in both scala and lift and get confused
about the different build tool and how to set up things.
I don't really understand maven and haven't really looked at sbt,
but it seem to be the choice of scala developers, but I'm also
dependent on quite some javacode.
Any suggestions, links, tutorials or else on how to setup
the best environment so I can work comfortably in Eclipse
and also build releases quite easily given my scala/java/lift/jetty
enrironment?
I would say that if you are comfortable using Ant as a build tool, Maven should be a piece of cake to use instead. It's main advantage is encouraging conforming to conventions rather than lots of configuration meaning that as long as you follow the standards, setup and usage of any most maven projects is a breeze. I strongly recommend that you invest the time to become familiar with this build tool because it will be very valuable experience/knowledge you can continue to use in your Java career.
With Maven aside, if you are convinced that you and the rest of the project's devs will all be using Eclipse, I would recommend using Eclipse's built in project management infrastructure. It uses Ant under the hood so you will be able to leverage you experience to also run/release from the command-line. All you need to do is set up a eclipse Lift project normally and then to add the scala integration, check out Scala IDE for pretty good IDE support of scala development inside of eclipse.
Good luck!
This screencast is what I used to get started. It starts with a git repository for an SBT template and then adds the Eclipsify sbt plugin to make it into an Eclipse project.

Categories

Resources