Skip to content

LinkedInAttic/play-testng-plugin

Repository files navigation

TestNG plugin

This plugin is a TestNG runner for Play Framework 2.6. It using the TestNG sbt interface by jmhofer to run TestNG Test suites, and adds Helpers. Example can be found in the sample folder.

Usage

Installation

Plugin

Add the plugin in your project/plugins.sbt file.

addSbtPlugin("com.linkedin.play-testng-plugin" % "play-plugins-testng" % "2.6.0")

Add the following to your project's build.sbt file:

import com.linkedin.plugin.NGPlugin
lazy val {{project name}} = (project in file ("."))
  .settings(
    {{your settings}}
  )
  {{{enable plugins, set dependencies, etc.}}}
  .enablePlugins(NGPlugin)

Helpers

Add the dependency in your build.sbt file.

val appDependencies = Seq(
 "com.linkedin.play-testng-plugin" %% "play-testng-helpers" % "2.6.0"
)

Test classes must extend NGTests to use the helpers. Use com.linkedin.plugin.j.* in Java code and com.linkedin.plugin.s.* from Scala code.

Annotations

@WithFakeApplication

@WithFakeApplication runs the test in the context of a Play application.

It's equivalent to:

running(fakeApplication(), new Runnable() {
  public void run() {
    // Your
  }
});

This Annotation can be used on Test methods and test classes. When it's used on a class, a fake application created via a default GuiceApplicationBuilder will be started at the beginning of each test, and stop when the test finishes.

@WithFakeApplication
public class AllWithFakeApp extends NGTests {
 @Test
 public void aFastTest() {
    String f = play.Play.application().configuration().getString("test.loutre");
    if(!f.equals("oink"))
       throw new RuntimeException("Assertion failed");
 }
}

Or

public class SimpleTest extends NGTests {
 @Test
 @WithFakeApplication
 public void aFailingTest() {
   String f = play.Play.application().configuration().getString("test.fakeconf");
   if(!f.equals("fake"))
     throw new RuntimeException("Assertion failed");
 }
}

@Conf

You can change the application configuration for testing purpose using the @Conf annotation:

public class SimpleTest extends NGTests {
 
 @Test
 public void aFastTest() {
    System.out.println("Fast test");
 }
 
 @Test
 @WithFakeApplication
 @Conf(key="test.fakeconf", value="fake") // wil override the application.conf value for "test.fakeconf"
 public void aFailingTest() {
   String f = play.Play.application().configuration().getString("test.fakeconf");
   if(!f.equals("fake"))
     throw new RuntimeException("Assertion failed");
 }
}

Alternatively, if you need to change multiple configuration entries, you can wrap those @Conf into a @Confs:

public class SimpleTest extends NGTests {
 
 @Test
 public void aFastTest() {
    System.out.println("Fast test");
 }
 
 @Test
 @WithFakeApplication
 @Confs({
    @Conf(key="test.fakeconf", value="fake"),
    @Conf(key="test.loutre", value="oink")
 })
 public void aFailingTest() {
   String f = play.Play.application().configuration().getString("test.fakeconf");
   if(!f.equals("fake"))
     throw new RuntimeException("Assertion failed");
 }
}

The @Conf and @Confs annotation also work on test Classes. When used on classes, the new configuration will be used for every test in this class:

@WithFakeApplication
@Conf(key="test.fakeconf", value="fake")
public class AllWithFakeApp extends NGTests {
 @Test
 public void anotherFastTest() {
   String f = play.Play.application().configuration().getString("test.fakeconf");
   if(!f.equals("fake"))
     throw new RuntimeException("Assertion failed");
 }
}

Declaration on classes and methods can be mixed:

@WithFakeApplication
@Confs({
  @Conf(key="test.fakeconf", value="fake"),
  @Conf(key="test.anotherConf", value="fake")
})
public class AllWithFakeApp extends NGTests {
 
 @Test
 public void testAnotherConf() {
   String f = play.Play.application().configuration().getString("test.anotherConf");
   if(!f.equals("fake"))
     throw new RuntimeException("Assertion failed");
 }
 
 @Test
 @Conf(key="test.singleConf", value="fake")
 public void testASingleConf() {
   String f = play.Play.application().configuration().getString("test.singleConf");
   if(!f.equals("fake"))
     throw new RuntimeException("Assertion failed");
 }
 
}

Note: @Confs and @Conf will be ignored if tests (class or method) are not annotated with @WithFakeApplication.

License

Copyright 2012–2017 LinkedIn

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.

About

TestNG runner for the Play Framework 2.4

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published