Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feather instance is not properly provided to modules #5

Open
frenchdwa opened this issue Mar 26, 2016 · 0 comments
Open

Feather instance is not properly provided to modules #5

frenchdwa opened this issue Mar 26, 2016 · 0 comments

Comments

@frenchdwa
Copy link

The following unit test fails to excute due to a "java.lang.ClassCastException: org.codejargon.feather.Feather$1 cannot be cast to org.codejargon.feather.Feather
"

I think line 32 of Feather.java should be "return Feather.this;" instead of "return this;"

Content of InjectedFeatherTest.java

package org.codejargon.feather;

import static org.junit.Assert.*;

import javax.inject.Inject;
import javax.inject.Provider;

import org.junit.Test;

public class InjectedFeatherTest {

@Test
public void featherProviderInjection() {
    Feather feather = Feather.with();

    InjectedFeatherProvider injected = feather
            .instance(InjectedFeatherProvider.class);

    assertEquals(feather, injected.feather());
}

@Test
public void featherInjection() {
    Feather feather = Feather.with();

    InjectedFeather injected = feather.instance(InjectedFeather.class);

    assertEquals(feather, injected.feather());
}

@Test
public void featherInjectionThroughModule() {
    Feather feather = Feather.with(new Module());

    InjectedFeather injected1 = feather.instance(InjectedFeather.class);

    assertEquals(feather, injected1.feather());

    InjectedFeatherProvider injected2 = feather
            .instance(InjectedFeatherProvider.class);

    assertEquals(feather, injected2.feather());
}

// ------------------------------------------------------------------

public static class InjectedFeatherProvider {
    private final Feather injected;

    @Inject
    public InjectedFeatherProvider(Provider<Feather> f) {
        injected = f.get();
    }

    Feather feather() {
        return injected;
    }
}

public static class InjectedFeather {
    private final Feather injected;

    @Inject
    public InjectedFeather(Feather f) {
        injected = f;
    }

    Feather feather() {
        return injected;
    }
}

public static class Module {
    @Provides
    public InjectedFeather providesInjectedFeather(Feather f) {
        return new InjectedFeather(f);
    }

    @Provides
    public InjectedFeatherProvider providesInjectedFeatherProvider(
            Provider<Feather> f) {
        return new InjectedFeatherProvider(f);
    }

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant