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

Ignore fields that are annotated with the @MockBean and @SpyBean annotations of Spring test #757

Merged
merged 4 commits into from
Sep 20, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ final class ErrorProneCLIFlagsConfig extends AbstractConfig {
"javax.inject.Inject", // no explicit initialization when there is dependency injection
"com.google.errorprone.annotations.concurrent.LazyInit",
"org.checkerframework.checker.nullness.qual.MonotonicNonNull",
"org.springframework.beans.factory.annotation.Autowired");
"org.springframework.beans.factory.annotation.Autowired",
"org.springframework.boot.test.mock.mockito.MockBean",
"org.springframework.boot.test.mock.mockito.SpyBean");

private static final String DEFAULT_URL = "http://t.uber.com/nullaway";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,43 @@ public void springAutowiredFieldTest() {
.doTest();
}

@Test
public void springTestAutowiredFieldTest() {
defaultCompilationHelper
.addSourceFile("springboot-annotations/MockBean.java")
.addSourceFile("springboot-annotations/SpyBean.java")
.addSourceLines(
"Foo.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"import org.springframework.stereotype.Component;",
"@Component",
"public class Foo {",
" @Nullable String bar;",
" public void setBar(String s) {",
" bar = s;",
" }",
"}")
.addSourceLines(
"TestCase.java",
"package com.uber;",
"import org.junit.jupiter.api.Test;",
"import org.springframework.boot.test.mock.mockito.SpyBean;",
"import org.springframework.boot.test.mock.mockito.MockBean;",
"public class TestCase {",
" @SpyBean",
" private Foo spy;", // Initialized by spring test (via Mockito).
" @MockBean",
" private Foo mock;", // Initialized by spring test (via Mockito).
" @Test",
" void springTest() {",
" spy.setBar(\"hello\");",
" mock.setBar(\"hello\");",
" }",
"}")
.doTest();
}

@Test
public void springAutowiredConstructorTest() {
defaultCompilationHelper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.springframework.boot.test.mock.mockito;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MockBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.springframework.boot.test.mock.mockito;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SpyBean {
}