Repository: tomee Updated Branches: refs/heads/master 823790b98 -> 4d1a577c9
TOMEE-1958 mockito integration for app composer doesn't support to be set as property Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/4d1a577c Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/4d1a577c Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/4d1a577c Branch: refs/heads/master Commit: 4d1a577c9d18d2b8e660b38b8cbafc2f26a00798 Parents: 823790b Author: rmannibucau <rmannibu...@apache.org> Authored: Thu Oct 13 13:32:40 2016 +0200 Committer: rmannibucau <rmannibu...@apache.org> Committed: Thu Oct 13 13:32:40 2016 +0200 ---------------------------------------------------------------------- .../openejb/mockito/MockitoExtension.java | 9 ++- .../MockitoAndAppComposerClassLevelTest.java | 71 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/4d1a577c/utils/openejb-mockito/src/main/java/org/apache/openejb/mockito/MockitoExtension.java ---------------------------------------------------------------------- diff --git a/utils/openejb-mockito/src/main/java/org/apache/openejb/mockito/MockitoExtension.java b/utils/openejb-mockito/src/main/java/org/apache/openejb/mockito/MockitoExtension.java index 2e08ea3..854a536 100644 --- a/utils/openejb-mockito/src/main/java/org/apache/openejb/mockito/MockitoExtension.java +++ b/utils/openejb-mockito/src/main/java/org/apache/openejb/mockito/MockitoExtension.java @@ -16,17 +16,19 @@ */ package org.apache.openejb.mockito; +import org.apache.openejb.injection.FallbackPropertyInjector; +import org.apache.openejb.loader.SystemInstance; import org.apache.webbeans.annotation.AnyLiteral; import org.apache.webbeans.annotation.DefaultLiteral; import org.apache.webbeans.annotation.NamedLiteral; import org.mockito.cglib.proxy.Factory; -import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.Dependent; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.AfterBeanDiscovery; import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeforeBeanDiscovery; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.InjectionPoint; import java.lang.annotation.Annotation; @@ -44,6 +46,11 @@ public class MockitoExtension implements Extension { private static final Annotation DEFAULT_ANNOTATION = new DefaultLiteral(); private static final Annotation ANY_ANNOTATION = new AnyLiteral(); + public void addMocks(@Observes final BeforeBeanDiscovery bbd) { + // ensure it is init + SystemInstance.get().getComponent(FallbackPropertyInjector.class); + } + public void addMocks(@Observes final AfterBeanDiscovery abd) { for (Map.Entry<Class<?>, Object> instance : MockRegistry.mocksByType().entrySet()) { abd.addBean(new MockBean(instance.getKey(), instance.getValue())); http://git-wip-us.apache.org/repos/asf/tomee/blob/4d1a577c/utils/openejb-mockito/src/test/java/org/apache/openejb/mockito/MockitoAndAppComposerClassLevelTest.java ---------------------------------------------------------------------- diff --git a/utils/openejb-mockito/src/test/java/org/apache/openejb/mockito/MockitoAndAppComposerClassLevelTest.java b/utils/openejb-mockito/src/test/java/org/apache/openejb/mockito/MockitoAndAppComposerClassLevelTest.java new file mode 100644 index 0000000..d454f58 --- /dev/null +++ b/utils/openejb-mockito/src/test/java/org/apache/openejb/mockito/MockitoAndAppComposerClassLevelTest.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.mockito; + +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.testing.ContainerProperties; +import org.apache.openejb.testing.Module; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; + +import javax.ejb.EJB; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(ApplicationComposer.class) +@ContainerProperties( + @ContainerProperties.Property(name = "org.apache.openejb.injection.FallbackPropertyInjector", value = "org.apache.openejb.mockito.MockitoInjector") +) +public class MockitoAndAppComposerClassLevelTest { + @EJB + private Facade facade; + + @Mock + private Hello mock; + + @Mock(name = "named") + private Hello named; + + @Module + public Class<?>[] classes() { + return new Class<?>[] { Hello.class, Facade.class }; + } + + @Test + public void testDefault() { + // play with mocks + when(mock.hi()) + .thenReturn("openejb-mockito"); + when(mock.id()) + .thenReturn(12345); + + // test + assertEquals("openejb-mockito", facade.hello()); + } + + @Test + public void testName() { + // play with mocks + when(named.hi()) + .thenReturn("named"); + + // test + assertEquals("named", facade.name()); + } +}