Package org.mongounit

Class MongoUnit

java.lang.Object
org.mongounit.MongoUnit

public class MongoUnit extends Object
MongoUnit class contains utilities for manually seeding and performing assertion matches.
  • Constructor Details

    • MongoUnit

      public MongoUnit()
  • Method Details

    • dropAllCollectionsInDatabase

      public static void dropAllCollectionsInDatabase()
      Finds and drops all of the collections in the database that the MongoUnit framework is configured to automatically connect to.
    • seedWithDataset

      public static List<MongoUnitCollection> seedWithDataset(String[] locations, LocationType locationType, Class<?> relativePackageClass) throws MongoUnitException
      Seeds the database with the JSON-based datasets pointed to by the provided 'locations' path array. Returns a list of MongoUnitCollections that can optionally be reused for assertions in the 'assertMatches*' methods.
      Parameters:
      locations - Array paths to the files containing datasets.
      locationType - Type of location the provided 'locations' are.
      relativePackageClass - If 'locationType' is 'CLASS', this is the class type whose packaged location should be used for relativity of the 'location' path. Otherwise, it's ignored and can be null.
      Returns:
      List of MongoUnitCollections based on the data pointed to by provided 'locations'.
      Throws:
      MongoUnitException - If something goes wrong with loading datasets from the specified locations.
    • seedWithDataset

      public static List<MongoUnitCollection> seedWithDataset(String[] locations, Class<?> testClass)
      Seeds the database with the JSON-based datasets pointed to by the provided 'locations' path array. Returns a list of MongoUnitCollections that can optionally be reused for assertions in the 'assertMatches*' methods.
      Parameters:
      locations - Array of paths to the files containing datasets. The locations type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      testClass - Class instance of the test class.
      Returns:
      List of MongoUnitCollections based on the data pointed to by provided 'locations' and 'testClass'.
    • seedWithDataset

      public static List<MongoUnitCollection> seedWithDataset(String location, Class<?> testClass)
      Seeds the database with the JSON-based datasets pointed to by the provided 'locations' path array. Returns a list of MongoUnitCollections that can optionally be reused for assertions in the 'assertMatches*' methods.
      Parameters:
      location - Path to the file containing a dataset. The location type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      testClass - Class instance of the test class.
      Returns:
      List of MongoUnitCollections based on the data pointed to by provided 'location' and 'testClass'.
    • assertMatchesDataset

      public static void assertMatchesDataset(String[] locations, LocationType locationType, Class<?> relativePackageClass) throws MongoUnitException
      Asserts that whatever is currently in the database connected to by the MongoUnit framework matches the JSON datasets contained in files pointed to by the provided 'locations' array of paths.
      Parameters:
      locations - Array paths to the files containing datasets.
      locationType - Type of location the provided 'locations' are.
      relativePackageClass - If 'locationType' is 'CLASS', this is the class type whose package should be used for package relative 'location' path. Otherwise, it's ignored and can be null.
      Throws:
      MongoUnitException - If something goes wrong with loading datasets from the specified locations.
    • assertMatchesDataset

      public static void assertMatchesDataset(String[] locations, Class<?> testClass) throws MongoUnitException
      Asserts that whatever is currently in the database connected to by the MongoUnit framework matches the JSON datasets contained in files pointed to by the provided 'locations' array of paths. The locations type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      Parameters:
      locations - Array paths to the files containing datasets. The locations type is 'CLASS' . See javadoc org.mongounit.LocationType#CLASS for more detail.
      testClass - Class instance of the test class.
      Throws:
      MongoUnitException - If something goes wrong with loading datasets from the specified locations.
    • assertMatchesDataset

      public static void assertMatchesDataset(String location, Class<?> testClass) throws MongoUnitException
      Asserts that whatever is currently in the database connected to by the MongoUnit framework matches the JSON datasets contained in files pointed to by the provided 'location' path. The location type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      Parameters:
      location - Path to the file containing a dataset. The location type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      testClass - Class instance of the test class.
      Throws:
      MongoUnitException - If something goes wrong with loading the dataset from the specified location.
    • assertMatchesDataset

      public static void assertMatchesDataset(List<MongoUnitCollection> expectedDatasets) throws MongoUnitException
      Asserts that whatever is currently in the database connected to by the MongoUnit framework matches the dataset collectively contained in the provided 'datasets'.
      Parameters:
      expectedDatasets - List of MongoUnitCollections that collectively represent the dataset with which to compare the actual database state with. Note that the MongoUnitCollections can repeat the same-named collection more than once. This method will combine the documents in the same-named collections into 1 same-named collection while keeping the original order of the documents.
      Throws:
      MongoUnitException - If something goes wrong interpreting the comparisons contained in the provided 'datasets'.
    • assertMatchesEmptyDataset

      public static void assertMatchesEmptyDataset() throws MongoUnitException
      Assert database is empty.
      Throws:
      MongoUnitException - If something goes wrong during the assertion.
    • loadDatasetFromLocations

      public static List<MongoUnitCollection> loadDatasetFromLocations(String[] locations, LocationType locationType, Class<?> relativePackageClass) throws MongoUnitException
      Returns a list of MongoUnitCollections that represent the JSON-based data pointed to by the provided 'locations' array of paths.
      Parameters:
      locations - Array paths to the files containing datasets.
      locationType - Type of location the provided 'locations' are.
      relativePackageClass - If 'locationType' is 'CLASS', this is the class type whose package should be used for package relative 'location' path. Otherwise, it's ignored and can be 'null'.
      Returns:
      List of MongoUnitCollections that represent the JSON-based data pointed to by the provided 'locations' array of paths.
      Throws:
      MongoUnitException - If something goes wrong with loading datasets from the specified 'locations'.
    • loadDatasetFromLocations

      public static List<MongoUnitCollection> loadDatasetFromLocations(String[] locations, Class<?> testClass) throws MongoUnitException
      Returns a list of MongoUnitCollections that represent the JSON-based data pointed to by the provided 'locations' array of paths. The locations type is 'CLASS'. See javadoc org .mongounit.LocationType#CLASS for more detail.
      Parameters:
      locations - Array paths to the files containing datasets. The locations type is 'CLASS' . See javadoc org.mongounit.LocationType#CLASS for more detail.
      testClass - Class instance of the test class.
      Returns:
      List of MongoUnitCollections that represent the JSON-based data pointed to by the provided 'locations' array of paths.
      Throws:
      MongoUnitException - If something goes wrong with loading datasets from the specified 'locations'.
    • loadDatasetFromLocation

      public static List<MongoUnitCollection> loadDatasetFromLocation(String location, Class<?> testClass) throws MongoUnitException
      Returns a list of MongoUnitCollections that represent the JSON-based data pointed to by the provided 'locations' array of paths. The location type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      Parameters:
      location - Path to the file containing a dataset. The location type is 'CLASS'. See javadoc org.mongounit.LocationType#CLASS for more detail.
      testClass - Class instance of the test class.
      Returns:
      List of MongoUnitCollections that represent the JSON-based data pointed to by the provided 'location' path.
      Throws:
      MongoUnitException - If something goes wrong with loading dataset from the specified 'location'.