Tuesday, 22 March 2016

What is a Java `main` method - simple example

One of the topics I don’t cover in "Java For Testers" is the main method. I explain why my coding style ‘as a tester’ doesn’t really require main methods in this blog post.
And in this blog post I’m going to start to explain the main method.
Why?
To round off our Java education a little so that if you do want to start writing small applications or package your well written Java library code into an app, then you know how to go about doing it.


Create a Project

My basic steps, and I’m not going to cover them in detail because they are covered in Java For Testers:
  • creating a maven project in intellij,
  • but I’m not going to add any dependencies into the pom file.

Create a class with main method

And I will create the standard “Hello World” application:
  • create a class
  • create a public static void method called main which takes a String array as arguments
  • for the body of the method I will println the String "Hello World!" to the standard out
package com.javafortesters.main;

public class HelloWorldOutputter {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
Because we have written a lot of @Test methods in the IDE, we know that we can right click on a method and run it as a JUnit Test.
We can do the same to the main method we have just written and right click it to run it.
And we should see "Hello World!"printed to the console.
Great, so we’ve written our first app then?
Actually, no. We have written a class with a main method.

Create a .jar file

We have to first of all create a .jar file.
Since we used Maven we can do that very simply from the command line by typing mvn package
And then in our target folder we will see the ‘application’ as a .jar file.
Woo hoo. So now we have written our first app?
Actually no. Try running it
D:\>java -jar mainMethodInvestigation-1.0-SNAPSHOT.jar

no main manifest attribute, in mainMethodInvestigation-1.0-SNAPSHOT.jar

I need a manifest attribute?

Yes.
But we could run it now without a manifest attribute.
Java allows us to run any main class in the classpath from the command line, it just makes things a little more complicated for the average user. You can see examples of me doing this in my "Technical Testing Case Study"
The manifest attribute makes it easier.
D:\>java -cp mainMethodInvestigation-1.0-SNAPSHOT.jar com.javafortesters.main.HelloWorldOutputter
Hello World!
What we’ve basically said here is. Java, add mainMethodInvestigation-1.0-SNAPSHOT.jar to the classpatth and run the main method that you find in class com.javafortesters.main.HelloWorldOutputter and yes, you need the full package.
Woo hoo?

But I really want a manifest attribute

OK, that is easy to add into maven.
At its most basic, and for this current ‘application’ it can be pretty basic. We just need to use the maven-jar-plugin and configure the mainClass. documentation
To do that, we add the following into the pom.xml
<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <!-- http://maven.apache.org/shared/maven-archiver/index.html#class_manifest -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.javafortesters.main.HelloWorldOutputter</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
And now, when I run mvn package.
And then when I run the .jar
D:\>java -jar mainMethodInvestigation-1.0-SNAPSHOT.jar
Hello World!
So is that an application?
Yes it is.
Woo hoo!
There is some information on the Oracle site:

Tuesday, 8 March 2016

Use of Java for Technical Testing Case Study


When we learn Java, or think about learning Java, we may have a limited model of the possibilities that the knowledge opens up for us.

As we learn we develop skills we may be unaware of:


  • Use of IDE
  • General Debugging Skills
  • Breakpoint running code
  • Inspect variables when debugging
  • Running Java applications from the command line
  • Ability to review Java Code
  • Understanding exception messages
All of this can help us with our Technical Testing.

I have created a set of case study videos, about an hour long, showing these skills in action, while technical testing of a couple of Java desktop applications.

If you haven't read Java For Testers yet, then this video series will give you an idea of the way that Testers can apply they Java knowledge they learn.

And if you have read Java For Testers, then this might reveal opportunities to expand your testing that you haven't explored yet.

The case study videos are free, you just have to sign up to the video course to watch it.