lastcommons-test

Last.fm common test utilities

License

License

GroupId

GroupId

fm.last.commons
ArtifactId

ArtifactId

lastcommons-test
Last Version

Last Version

7.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

lastcommons-test
Last.fm common test utilities
Project URL

Project URL

https://github.com/lastfm/lastcommons-test
Project Organization

Project Organization

Last.fm
Source Code Management

Source Code Management

https://github.com/lastfm/lastcommons-test

Download lastcommons-test

How to add to project

<!-- https://jarcasting.com/artifacts/fm.last.commons/lastcommons-test/ -->
<dependency>
    <groupId>fm.last.commons</groupId>
    <artifactId>lastcommons-test</artifactId>
    <version>7.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/fm.last.commons/lastcommons-test/
implementation 'fm.last.commons:lastcommons-test:7.0.2'
// https://jarcasting.com/artifacts/fm.last.commons/lastcommons-test/
implementation ("fm.last.commons:lastcommons-test:7.0.2")
'fm.last.commons:lastcommons-test:jar:7.0.2'
<dependency org="fm.last.commons" name="lastcommons-test" rev="7.0.2">
  <artifact name="lastcommons-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='fm.last.commons', module='lastcommons-test', version='7.0.2')
)
libraryDependencies += "fm.last.commons" % "lastcommons-test" % "7.0.2"
[fm.last.commons/lastcommons-test "7.0.2"]

Dependencies

compile (7)

Group / Artifact Type Version
fm.last.commons : lastcommons-lang jar 3.0.0
commons-collections : commons-collections jar 3.2.2
commons-lang : commons-lang jar 2.6
commons-io : commons-io jar 2.4
log4j : log4j jar 1.2.17
org.mockito : mockito-all jar 1.9.0
joda-time : joda-time jar 2.1

provided (2)

Group / Artifact Type Version
junit : junit jar 4.10
org.junit.jupiter : junit-jupiter-api jar 5.5.1

Project Modules

There are no modules declared in this project.

About

Common test classes used by some Last.fm projects. We have open-sourced this code to allow us to open-source additional projects that may have a dependency on this library.

Start using

Obtain lastcommons-test from Maven Central:

Maven Central GitHub license

This project provides a TemporaryFolder or a DataFolder for use in your JUnit tests.

TemporaryFolder gives you an empty folder for each test that lets you create folders and files during your test. JUnit nicely handles the life-cycle of this folder, creating it when necessary and removing it when your test is done.

The DataFolder classes provide a convenient way to refer to files and folders containing test data within your project, so you can fetch data easily during a test without needing to hard code paths.

To use within your own tests, make sure you have either JUnit4 or JUnit5 in your POM. Neither are provided through lastcommons-test.

You can download a JAR file or obtain lastcommons-test from Maven Central using the following identifier:

Building

This project uses the Maven build system.

Contributing

All contributions are welcome. Please use the Last.fm codeformatting profile found in the lastfm-oss-config project for formatting your changes.

Usage examples

Temporary files and folders

Use a fm.last.commons.test.file.TemporaryFolder rule to cleanly obtain a temporary folder for file writing etc. The code looks something like this:

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void myTest() {
  File logged = temporaryFolder.newFile("log.txt");
  File report = temporaryFolder.newFile("2012", "03", "01", "report.tsv");
  ...

Data files and folders

Data folders are now available as JUnit4 Rules and JUnit5 Extensions. Examples can be found below:

Top level data folder

JUnit5 Example

public class MyTest {

  @RegisterExtension
  public DataFolder dataFolder = new RootDataFolderExtension();

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data
    ...

And also a child folder within the root:

public class MyMp3Test {

  @RegisterExtension
  public DataFolder dataFolder = new RootDataFolderExtension("mp3", "128K", "clean");

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data/mp3/128k/clean
    ...

JUnit4 Example

public class MyTest {

  @Rule
  public DataFolder dataFolder = new RootDataFolder();

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data
    ...

And also a child folder within the root:

public class MyMp3Test {

  @Rule
  public DataFolder dataFolder = new RootDataFolder("mp3", "128K", "clean");

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data/mp3/128k/clean
    ...

Per-class data folder

JUnit5 Example

public class MyTest {

  @RegisterExtension
  public DataFolder dataFolder = new ClassDataFolderExtension();

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data/fm/last/project/MyTest
    ...

JUnit4 Example

public class MyTest {

  @Rule
  public DataFolder dataFolder = new ClassDataFolder();

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data/fm/last/project/MyTest
    ...

Per-method data folder

JUnit5 Example

public class MyTest {

  @RegisterExtension
  public DataFolder dataFolder = new MethodDataFolderExtension();

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data/fm/last/project/MyTest/myTestMethod
    ...

JUnit4 Example

public class MyTest {

  @Rule
  public DataFolder dataFolder = new MethodDataFolder();

  @Test
  public void myTestMethod() throws IOException {
    File actualFolder = dataFolder.getFolder();
    // Path: ./src/test/data/fm/last/project/MyTest/myTestMethod
    ...

Note: calls to getFolder() will throw an exception if the folder doesn't exist or is not readable.

Legal

Copyright 2013-2019 Last.fm

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

fm.last.commons

Last.fm

Versions

Version
7.0.2
7.0.1
6.1.0
6.0.0
5.2.1
5.2.0
5.1.2
5.1.1
5.0.1
5.0.0