Skip to content

ynovytskyy/selective-class-loader-junit-runner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

selective-class-loader-junit-runner

(descriptive name is more important than short)

Build Status Release

Some bigger modern libraries (e.g. Spring) can leverage parts of their functionality depending on whether one or the other class is present on the classpath in the runtime.

If you ever wanted to use the same approach and wanted to test such a code, you might have faced some difficulties doing that.

Suppose the code you wanted to test looks like this:

private static boolean isFeatureClassPresent = ClassUtils.isPresent("com.somelibrary.Feature",
		FeatureConsumer.class.getClassLoader());

public Strategy chooseStrategy() {
	if (isFeatureClassPresent) {
		return new FeaturefulStrategy(new Feature());
	} else {
		return new SimpleInternalStrategy();
	}
}

It can be quite simple to test the path of execution that does use the "Feature" class. In most cases it would be just placing that optional library on the classpath. It would be needed on the compilation classpath anyways to be able to compile our code.

But to have a test, that wouldn't find the class on the classpath, would be much trickier. Here's when SelectiveClassLoaderJUnitRunner will come handy.

@RunWith(SelectiveClassLoaderRunner.class)
@BanClassNamesStartingWith("com.somelibrary.Feature")
public class SelectiveClassLoaderRunnerWithBannedClassTest {
	@Test
	public void testOptionalFeatureNotUsed() {
		FeatureConsumer consumer = new FeatureConsumer();
		Strategy strategy = consumer.chooseStrategy();

		Assert.assertFalse(strategy instanceof FeaturefulStrategy);
		Assert.assertTrue(strategy instanceof SimpleInternalStrategy);
	}
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages