Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load different plugins in Play framework unit tests?

I have different plugins implemented the Plugin interface. Now I have them hard-coded in play.plugins like this:

100:test.A
200:test.B

However in my unit tests I don't want both of them to be loaded at one time. In other words, in test A I want to load only plugin A and in test B load B only. Instead of changing the configuration file manually, is there a way to change it programmatically? I guess when fakeApplication() is invoked all plugins are loaded by default.

like image 426
NSF Avatar asked Dec 25 '22 23:12

NSF


1 Answers

You can start a FakeApplication with added or excluded plugins and also with custom configuration.

Here is an example in scala (I believe the Java API has an equivalent mechanism) :

val app = FakeApplication(  
  withoutPlugins = List("test.A", "test.B"),  
  additionalPlugins = List("test.C"),  
  additionalConfiguration = Map("customConfigKey" -> "customConfigValue")  
)

In Java, you can use one of the fakeApplication static methods available in the play.test.Helpers class. Here is the equivalent of the scala example :

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import play.test.FakeApplication;
import static play.test.Helpers.*;

Map<String, Object> additionalConfiguration = new HashMap<String, Object>();
additionalConfiguration.put("customConfigKey", "customConfigValue");
List<String> withoutPlugins = Arrays.asList("test.A", "test.B");
List<String> additionalPlugins = Arrays.asList("test.C");

FakeApplication app = fakeApplication(additionalConfiguration, additionalPlugins, withoutPlugins);
like image 178
mguillermin Avatar answered Dec 28 '22 14:12

mguillermin