Skip to content

Feather instance is not properly provided to modules #5

Description

@frenchdwa

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);
    }

}

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions