Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BQRuntimeChecker - static helper for runtime assertions #211
* refactoring to a separate class BQRuntimeChecker, as we are not testing the provider here, but rather the state of runtime * changing a list of modules to vararg. It seems to be simpler to call it like that from the tests
- Loading branch information
Showing
5 changed files
with
80 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
bootique-test/src/main/java/io/bootique/test/junit/BQRuntimeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.bootique.test.junit; | ||
|
||
import com.google.inject.Module; | ||
import io.bootique.BQRuntime; | ||
import io.bootique.meta.module.ModuleMetadata; | ||
import io.bootique.meta.module.ModulesMetadata; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.hamcrest.core.IsCollectionContaining.hasItems; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* A helper class for writing test assertions against a {@link BQRuntime}. | ||
* | ||
* @since 0.25 | ||
*/ | ||
public class BQRuntimeChecker { | ||
|
||
/** | ||
* Verifies that runtime contains expected modules. | ||
* | ||
* @param runtime a Bootique runtime whose contents we are testing. | ||
* @param expectedModules a vararg array of expected module types. | ||
*/ | ||
public static void testModulesLoaded(BQRuntime runtime, Class<? extends Module>... expectedModules) { | ||
|
||
final ModulesMetadata modulesMetadata = runtime.getInstance(ModulesMetadata.class); | ||
|
||
final List<String> actualModules = modulesMetadata | ||
.getModules() | ||
.stream() | ||
.map(ModuleMetadata::getName) | ||
.collect(toList()); | ||
|
||
final String[] expectedModuleNames = new String[expectedModules.length]; | ||
for (int i = 0; i < expectedModules.length; i++) { | ||
expectedModuleNames[i] = expectedModules[i].getSimpleName(); | ||
} | ||
|
||
// Using class names for checking module existing - weak. | ||
assertThat(actualModules, hasItems(expectedModuleNames)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
bootique-test/src/test/java/io/bootique/test/junit/BQRuntimeCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.bootique.test.junit; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Module; | ||
import io.bootique.BQCoreModule; | ||
import io.bootique.BQRuntime; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class BQRuntimeCheckerTest { | ||
|
||
@Rule | ||
public BQTestFactory testFactory = new BQTestFactory(); | ||
|
||
@Test | ||
public void testTestModulesLoaded() { | ||
final BQRuntime runtime = testFactory.app().createRuntime(); | ||
BQRuntimeChecker.testModulesLoaded(runtime, BQCoreModule.class); | ||
} | ||
|
||
@Test(expected = AssertionError.class) | ||
public void testTestModulesNotLoaded() { | ||
final BQRuntime runtime = testFactory.app().createRuntime(); | ||
BQRuntimeChecker.testModulesLoaded(runtime, NonLoadedModule.class); | ||
} | ||
} | ||
|
||
|
||
class NonLoadedModule implements Module { | ||
|
||
@Override | ||
public void configure(Binder binder) { | ||
} | ||
} |