1. Testing timeout
Chances are that you have some kind of loop (
while etc.) in
your application, in particular when working with data structures that have to be traversed.
In such cases, adding a Timeout to your tests can be a great help. Adding a timeout
import java.util.concurrent.TimeUnit; import org.junit.Rule; import org.junit.rules.Timeout; . . . . @Rule public Timeout timeout = new Timeout( 2000, TimeUnit.MILLISECONDS );
A 2000 millisecond timeout should be adequate for most unit tests.
The teachers tests have a timeout rule like above. All Alda-Run test are also timebound, as all maven tests are aborted after a running time of 20 seconds. If the tests is timed out in this way, no test results will be produced. So better add a timeout on your own tests too.
We will use Maven as the preferred build tool. The link in the previous sentence points to the very readable complete reference manual, which also has some pointers for beginners, might you categorize yourselves as one.
2.1. Maven Repositories
A maven repository is a server that provides ready built maven 'artifacts', such as (binary) jar files containing libraries, frameworks or APIs and in many (but not all) companion jar files containing the javadoc (often) and sources of the same binary. A maven repository is NOT a source code. management system (CMS), although it typically holds several versions of a binary, and there are tools, such as Nexus Repository Manager.
There is a default repository, called maven central. You (as a team or company) can have your own repository. We at sebi Venlo do that too. Our repository lives at https://www.fontysvenlo.org/repository. This allows us to easily share libraries and such with our students with having to go through the processes to get these libraries approved.
Our sebi Venlo maven repository at fontysvenlo is the simplest of simple: A plain (apache2) web server that does automatic indexing. Users with write access, such as your dear teachers (:P), can simply publish their maven artifacts using maven itself, which, with the help of secure copy, places the artefact in the appropriate directory structure, considering names and versions.
If you look at a maven repository you will see that it is organized along the three axes: groupId, artifactId and version.
2.2. Maven Settings
To make the sebi Venlo repository available for your own projects, add the following to
~/.m2/settings.xml in the profiles section. You may also want to make that profile active.
If you create or open a maven project in NetBeans and open the Project Files folder, it should show a settings.xml file. If not, you can easily create your own in the proper spot on you platform (Linux, Windows, Mac OS-X) by right-clicking on the folder and choose create settings.xml. The settings file applies to all your maven projects, irrespective of the fact of using maven from the IDE or the command line. You could even switch IDE, as long as all support maven, which they typically do.
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <profiles> <profile> (1) <id>sebivenlo</id> <repositories> <repository> <id>fontysvenlo.org</id> (2) <url>https://www.fontysvenlo.org/repository</url> (3) </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>sebivenlo</activeProfile> (4) </activeProfiles> </settings>
|1||You define a profile inside the profiles section of your settings.xml.|
|2||Give the profile a name.|
|3||Specify the URL.|
|4||Use the given name to make it an active profile by default.|
You could of course also simply copy (ctrl-C) the xml above and slam (ctrl-V) it into your newly created settings.xml file.
Spelled all lower case:
sebipom, is a super pom to use in Fontys Venlo projects. It keeps your own pom.xml file simple and focused to declare just the
dependencies you need. Niceties to do proper Test Drivern Development are provided for in the sebipom definitions, including things as being able to create
javadoc in a professional way, including code snippets, checkstyle, reporting, code coverage with maven etc. Sebipom is hosted on github as part of statewalker.
|Note that I am using a separately installed maven (3.5.3 at the time of writing) which you can download from apache maven, not the one bundled with netbeans. You can tell netbeans (via tools→ options →java→maven) to use the installed one instead of the bundled one.|
Doing this gives you the access to
sebipom, which defines a parent pom for projects. This again provides some goodies that you would otherwise have to provide yourselves.
Such a parent pom is similar to a super class in Java; your pom inherits all the traits of the parent.
Including that parent is simple. See below for the sebipom (version 1.3.4).
<parent> <groupId>nl.fontys.sebivenlo</groupId> <artifactId>sebipom</artifactId> <version>2.3.9</version> </parent>
Some of the starter projects we provide already use this parent. To get those working, apply the settings above.
If you want to use sebipom in your own project, either copy the parent definition into the pom file or start by replacing the NetBeans provided pom.xml file in your project by the basic one below. Add dependencies as required.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>nl.fontys.sebivenlo</groupId> <artifactId>basicsebimaven</artifactId> <version>1.0</version> <packaging>jar</packaging> <parent> <groupId>nl.fontys.sebivenlo</groupId> <artifactId>sebipom</artifactId> <version>2.3.9</version> <relativePath/> </parent> <!-- The name in the IDE --> <name>My App</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- change mainClass to the proper starting point of your program, if any.--> <exec.mainClass>basicsebimaven.Hello</exec.mainClass> <pitTargetClasses>shoppingcart.*</pitTargetClasses> <excludedMethod>**.#hashCode</excludedMethod> </properties> <dependencies> <!--example --> <dependency> </dependency> </dpendencies> </project>
2.4. sebipom and Java FX
If you want to use semipom to build a JavaFX project, you need to activate the fx-profile. In the commandline that would be
In netbeans you can do that by setting the configuration like in this picture:
3. Maven on Steroids or: use it properly
Sometimes you think, maven is not your thing because it is slowing you down, certainly if you are trying to work your way through your code TDD style: Write test, run test red, implement code, run test green, be happy.
But reconsider: Maven takes it’s job very serious, but since it is a computer program it stubbornly stupid, so it might be that you are using it in a wrong way.
Let me explain what maven does:
maven uses build phases: initialize, compile, compile tests, run tests, package the application etc., when you build your application. It will check if all is in order, look if any updates are needed etc. This keeps maven busy and you waiting.
Packaging (which is what you do when you click build in the IDE) the application and
is only useful when your are really done: all tests are complete and the code turns them a nice green.
So do not mvn build, but instead mvn test, then maven will do the minimum required.
So if you are in the habit of clicking build and then run your tests, you might want to change you habit.
3.1. Maven the proper way, when still in the TDD phase
As long as you are writing test and writing code, just do:
mvn test, or the equivalent in you IDE (click the test button, not the build button)
mvn clean and then
mvn test. (click clean, click test).
Only when you are done with all your tests, do
clean (mvn clean)
build (mvn package mvn -P fx package for fx projects)
and then run your application.
Then maven can do all of it’s serious work of assembling your application for deployment
TODO maven tweaking…..