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

Extension system added for junit 5+ #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ buildscript {
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
google()
}
Expand Down
1 change: 1 addition & 0 deletions daggermock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ targetCompatibility = 1.8
dependencies {
compileOnly "org.mockito:mockito-core:$MOCKITO_VERSION"
compileOnly 'junit:junit:4.12'
compileOnly 'org.junit.jupiter:junit-jupiter:5.6.1'
compileOnly "com.google.dagger:dagger:$DAGGER_VERSION"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package it.cosenonjaviste.daggermock;

import java.lang.reflect.Field;
import java.util.*;

import org.junit.jupiter.api.extension.*;
import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.commons.util.ReflectionUtils;

public class DaggerMockExtension implements BeforeEachCallback {

@SuppressWarnings("unchecked")
@Override
public void beforeEach(ExtensionContext context) throws Exception {
context.getTestInstance().ifPresent(testInstance -> {
DaggerMockTest annotation = testInstance.getClass().getDeclaredAnnotation(DaggerMockTest.class);

ArrayList<Object> modules = new ArrayList<>();

List<Field> fields = AnnotationSupport.findAnnotatedFields(testInstance.getClass(),
DaggerMockModules.class);

for (Field field : fields) {
try {
modules.addAll((Collection<Object>) ReflectionUtils.tryToReadFieldValue(field, testInstance).get());
} catch (Exception e) {
e.printStackTrace();
}
}

//TODO Extract helper class from Rule to remove junit 4 dependency
DaggerMockRule<?> rule = new DaggerMockRule<>(annotation.value(), modules.toArray());

try {
rule.initMocks(testInstance);
} catch (Throwable e) {
e.printStackTrace();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.cosenonjaviste.daggermock;

import java.lang.annotation.*;

import org.junit.jupiter.api.extension.ExtendWith;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(DaggerMockExtension.class)
public @interface DaggerMockModules {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.cosenonjaviste.daggermock;

import java.lang.annotation.*;

import org.junit.jupiter.api.extension.ExtendWith;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(DaggerMockExtension.class)
public @interface DaggerMockTest {

Class<?> value();
}
12 changes: 11 additions & 1 deletion daggermockTests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ dependencies {

testImplementation project(':daggermock')
testImplementation "org.mockito:mockito-core:$MOCKITO_VERSION"
testImplementation 'junit:junit:4.12'
testImplementation "org.junit.jupiter:junit-jupiter:5.6.1"
testImplementation 'org.junit.vintage:junit-vintage-engine:5.6.1'
testCompile("org.junit.platform:junit-platform-commons:1.6.1")
testCompile 'org.junit.platform:junit-platform-engine:1.6.1'
testImplementation "com.google.dagger:dagger:$DAGGER_VERSION"
testImplementation 'org.glassfish:javax.annotation:10.0-b28'
testAnnotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
testImplementation 'org.assertj:assertj-core:2.5.0'
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

task codeCoverageReport(type: JacocoReport) {
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2016 Fabio Collini.
*
* Licensed 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 it.cosenonjaviste.daggermock.injectfromcomponent;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.Collections;

import org.junit.jupiter.api.extension.ExtendWith;

import dagger.Module;
import dagger.Provides;
import dagger.Component;
import it.cosenonjaviste.daggermock.DaggerMockExtension;
import it.cosenonjaviste.daggermock.DaggerMockTest;
import it.cosenonjaviste.daggermock.DaggerMockModules;
import it.cosenonjaviste.daggermock.InjectFromComponent;

@ExtendWith(DaggerMockExtension.class)
@DaggerMockTest(InjectFromComponentExtensionTest.MyComponent.class)
public class InjectFromComponentExtensionTest {

@DaggerMockModules
Collection<Object> list = Collections.singleton(new MyModule());

String s1 = "test1";

@InjectFromComponent MainService mainService;

@Test
public void testInjectFromComponent() {
assertThat(mainService).isNotNull();
assertThat(mainService.get()).isEqualTo("test1");
}

@Module
public static class MyModule {
@Provides public String provideS1() {
return "s1";
}
}

@Component(modules = MyModule.class)
public interface MyComponent {
MainService mainService();
}
}