Skip to content

Commit

Permalink
Test void return for providers
Browse files Browse the repository at this point in the history
  • Loading branch information
kejunxia committed Jun 22, 2015
1 parent e9b44d4 commit f9ed789
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void unregister(Component component) {
for (Method method : methods) {
if (method.isAnnotationPresent(Provides.class)) {
Class<?> returnType = method.getReturnType();
if (returnType != Void.class) {
if (returnType != void.class) {
Annotation qualifier = null;
Annotation[] annotations = method.getAnnotations();
for (Annotation a : annotations) {
Expand All @@ -355,7 +355,7 @@ public void unregister(Component component) {
private void registerProvides(final Component component, final Method method, boolean allowOverride)
throws ProvideException, ProviderConflictException {
Class<?> returnType = method.getReturnType();
if (returnType == Void.class) {
if (returnType == void.class) {
throw new ProvideException(String.format("Provides method %s must not return void.",
method.getName()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import javax.inject.Named;
import javax.inject.Qualifier;
import javax.inject.Singleton;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.mockito.Matchers.eq;
Expand Down Expand Up @@ -411,6 +412,80 @@ class Basket {
Assert.assertTrue(shouldCatchProviderMissingException);
}

static class BadComponent extends Component{
@Provides
void provideNothing() {
return;
}
}

@Test
public void should_throw_exception_when_there_is_void_function_in_component()
throws ProvideException, ProviderConflictException {
BadComponent badComponent = new BadComponent();

try {
providerFinder.register(badComponent);
} catch (ProvideException e) {
Assert.assertTrue(e.getMessage().contains("must not return void"));
return;
}
Assert.fail("Should raise ProvideException for provider returning void");
}

@Qualifier @Retention(RUNTIME) @interface Qualifier1 {}

@Qualifier @Retention(RUNTIME) @interface Qualifier2 {}

static class DuplicateComponent extends Component{
@Provides @Qualifier1 @Qualifier2
String provideText() {
return "123";
}
}

@Test
public void should_throw_exception_when_provider_has_more_than_one_qualifier()
throws ProvideException, ProviderConflictException {
DuplicateComponent duplicateComponent = new DuplicateComponent();
try {
providerFinder.register(duplicateComponent);
} catch (ProvideException e) {
Assert.assertTrue(e.getMessage().contains("Only one Qualifier"));
return;
}
Assert.fail("Should raise ProvideException for provider with multiple qualifier");
}

@Test
public void should_throw_exception_if_provider_returns_null()
throws ProvideException, ProviderConflictException, CircularDependenciesException, ProviderMissingException {
Provider<String> provider = new Provider(String.class) {
@Override
protected String createInstance() throws ProvideException {
return null;
}
};
provider.setScopeCache(new ScopeCache());
providerFinder.register(provider);

class Container {
@MyInject
@Singleton
private String name;
}

Container container = new Container();

try {
graph.inject(container, MyInject.class);
} catch (ProvideException e) {
Assert.assertTrue(e.getMessage().contains("should not provide NULL as instance"));
return;
}
Assert.fail("Should raise ProvideException for provider returns null");
}

@Test
public void overridingClassRegisteringShouldWorkAsExpected() throws ProviderConflictException,
ProvideException, CircularDependenciesException, ProviderMissingException {
Expand Down

0 comments on commit f9ed789

Please sign in to comment.