Skip to content

Commit

Permalink
Ignore fields that are annotated with the @MockBean and @SpyBean
Browse files Browse the repository at this point in the history
…annotations of Spring test (#757)

This is a small followup to #477. When you are testing Spring (Boot)
applications then you are replacing controllers that are normally
injected via `@Autowired` with stubs, mocks or spies from Mockito. This
is done with two similar annotations:

- `@MockBean`
- `@SpyBean`

So it would be helpful if these are ignored as well.
  • Loading branch information
uhafner committed Sep 20, 2023
1 parent 70af259 commit ab387b9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
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 {
}

0 comments on commit ab387b9

Please sign in to comment.