Class LuceneTestFrameworkInitializer
LuceneTestFrameworkInitializer may be subclassed in order to use standard dependency injection techniques for classes under test. A subclass of LuceneTestFrameworkInitializer will be executed automatically.
Only one subclass per assembly is allowed, and by convention these subclasses are usually named "Startup". The following abstract factories can be overridden for testing purposes:Abstraction | Factory Method Name | Default |
---|---|---|
Lucene.Net.Codecs.ICodecFactory | CodecFactory | TestCodecFactory |
Lucene.Net.Codecs.IDocValuesFormatFactory | DocValuesFormatFactory | TestDocValuesFormatFactory |
Lucene.Net.Codecs.IPostingsFormatFactory | PostingsFormatFactory | TestPostingsFormatFactory |
Lucene.Net.Configuration.IConfigurationFactory | ConfigurationFactory | TestConfigurationFactory |
Method | Description |
---|---|
Initialize() | Used to set the factories in the above table. In general, dependency injection for the test assembly is setup in this method. No randomized context is available. |
TestFrameworkSetUp() | Used to set assembly-level test setup. Executed before all tests and class-level setup in the assembly. Repeatable randomized content can be generated using the Random property. |
TestFrameworkTearDown() | Used to tear down assembly-level test setup. Executed after all tests and class-level tear down in the assembly. Repeatable randomized content can be generated using the Random property. |
using RandomizedTesting.Generators;
public class Startup : LuceneTestFrameworkInitializer
{
// Run first
protected override void Initialize()
{
// Inject a custom configuration factory
ConfigurationFactory = new MyConfigurationFactory();
// Do any additional dependency injection setup here
}
// Run before all tests in the assembly and before any class-level setup
protected overide void TestFrameworkSetUp()
{
// Get the random instance for the current context
var random = Random;
// Generate random content
string content = random.NextSimpleString();
// Use randomization from LuceneTestCase
int numberOfDocuments = LuceneTestCase.AtLeast(30);
}
// Run after all tests in the assembly and after any class-level setup
protected override void TestFrameworkTearDown()
{
// Tear down everything here
}
}</code></pre>
Inherited Members
Namespace: Lucene.Net.Util
Assembly: Lucene.Net.TestFramework.dll
Syntax
public abstract class LuceneTestFrameworkInitializer
Constructors
LuceneTestFrameworkInitializer()
LuceneTestFrameworkInitializer may be subclassed in order to use standard dependency injection techniques for classes under test. A subclass of LuceneTestFrameworkInitializer will be executed automatically.
Only one subclass per assembly is allowed, and by convention these subclasses are usually named "Startup". The following abstract factories can be overridden for testing purposes:Abstraction | Factory Method Name | Default |
---|---|---|
Lucene.Net.Codecs.ICodecFactory | CodecFactory | TestCodecFactory |
Lucene.Net.Codecs.IDocValuesFormatFactory | DocValuesFormatFactory | TestDocValuesFormatFactory |
Lucene.Net.Codecs.IPostingsFormatFactory | PostingsFormatFactory | TestPostingsFormatFactory |
Lucene.Net.Configuration.IConfigurationFactory | ConfigurationFactory | TestConfigurationFactory |
Method | Description |
---|---|
Initialize() | Used to set the factories in the above table. In general, dependency injection for the test assembly is setup in this method. No randomized context is available. |
TestFrameworkSetUp() | Used to set assembly-level test setup. Executed before all tests and class-level setup in the assembly. Repeatable randomized content can be generated using the Random property. |
TestFrameworkTearDown() | Used to tear down assembly-level test setup. Executed after all tests and class-level tear down in the assembly. Repeatable randomized content can be generated using the Random property. |
using RandomizedTesting.Generators;
public class Startup : LuceneTestFrameworkInitializer
{
// Run first
protected override void Initialize()
{
// Inject a custom configuration factory
ConfigurationFactory = new MyConfigurationFactory();
// Do any additional dependency injection setup here
}
// Run before all tests in the assembly and before any class-level setup
protected overide void TestFrameworkSetUp()
{
// Get the random instance for the current context
var random = Random;
// Generate random content
string content = random.NextSimpleString();
// Use randomization from LuceneTestCase
int numberOfDocuments = LuceneTestCase.AtLeast(30);
}
// Run after all tests in the assembly and after any class-level setup
protected override void TestFrameworkTearDown()
{
// Tear down everything here
}
}</code></pre>
Declaration
protected LuceneTestFrameworkInitializer()
Properties
CodecFactory
The Lucene.Net.Codecs.ICodecFactory implementation to use to load codecs during testing.
Declaration
protected ICodecFactory CodecFactory { get; set; }
Property Value
Type | Description |
---|---|
ICodecFactory |
ConfigurationFactory
The Lucene.Net.Configuration.IConfigurationFactory implementation to use to load configuration settings during testing. See: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/.
Declaration
[CLSCompliant(false)]
protected IConfigurationFactory ConfigurationFactory { get; set; }
Property Value
Type | Description |
---|---|
IConfigurationFactory |
DocValuesFormatFactory
The Lucene.Net.Codecs.IDocValuesFormatFactory implementation to use to load doc values during testing.
Declaration
protected IDocValuesFormatFactory DocValuesFormatFactory { get; set; }
Property Value
Type | Description |
---|---|
IDocValuesFormatFactory |
PostingsFormatFactory
The Lucene.Net.Codecs.IPostingsFormatFactory implementation to use to load postings formats during testing.
Declaration
protected IPostingsFormatFactory PostingsFormatFactory { get; set; }
Property Value
Type | Description |
---|---|
IPostingsFormatFactory |
Random
Access to the current Random instance. It is safe to use this method from multiple threads, etc., but it should be called while within a runner's scope (so no static initializers). The returned Random instance will be different when this method is called inside a BeforeClass() hook (static suite scope) and within NUnit.Framework.OneTimeSetUpAttribute/ NUnit.Framework.OneTimeTearDownAttribute hooks or test methods.
The returned instance must not be shared with other threads or cross a single scope's boundary. For example, a Random acquired within a test method shouldn't be reused for another test case. There is an overhead connected with getting the Random for a particular context and thread. It is better to cache the Random locally if tight loops with multiple invocations are present or create a derivative local Random for millions of calls like this:Random random = new J2N.Randomizer(Random.NextInt64());
// tight loop with many invocations.
Declaration
protected Random Random { get; }
Property Value
Type | Description |
---|---|
Random |
Methods
Initialize()
Overridden in a derived class, provides a way to set CodecFactory, PostingsFormatFactory, DocValuesFormatFactory and ConfigurationFactory as well as other dependency injection setup.
This method is called only one time per assembly. Using the Random property here will result in a test failure. To build randomized global setup, use TestFrameworkSetUp() instead.Declaration
protected virtual void Initialize()
TestFrameworkSetUp()
Overridden in a derived class, can be used to perform one-time initialization of the test framework setup.
Repeatable random setup can be done by calling Random or by using methods of LuceneTestCase. It is not possible to set CodecFactory, PostingsFormatFactory, DocValuesFormatFactory and ConfigurationFactory from this method. Those must be called in Initialize().Declaration
protected virtual void TestFrameworkSetUp()
TestFrameworkTearDown()
Overridden in a derived class, can be used to perform one-time tear down of the test framework setup (whether the setup was done in Initialize() or TestFrameworkSetUp() doesn't matter).
Repeatable random setup can be done by calling Random or by using methods of LuceneTestCase.Declaration
protected virtual void TestFrameworkTearDown()