9

考虑我的情况

public class SomeClass {
  @Autowired @Qualifier("converter1") private IConverter converter1;
  @Autowired @Qualifier("converter2") private IConverter converter2;

  public void doSomeAction(String mimeType) {
    converter1.execute();
    converter2.execute();
  }
}

这是我的代码。

为了测试这个

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
  @Mock(name="converter1") IConverter converter1;
  @Mock(name="converter2") IConverter converter2;
  @InjectMocks SomeClass class = new SomeClass();
  @Test
  public void testGetListOfExcelConverters() throws Exception {
    class.doSomeAction("abcd");
  }
}

这里没有注入模拟,请帮助模拟合格 bean 的正确机制。

如果这不是使用 spring 编码的正确方法,请告诉我正确的使用方法。

4

5 回答 5

5

Not sure what error you are getting, but your test class doesn't compile because you have what looks like you intend to be a variable name using the keyword class. This worked for me:

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
    @Mock(name="converter1") IConverter converter1;
    @Mock(name="converter2") IConverter converter2;

    @InjectMocks
    SomeClass clazz = new SomeClass();

    @Test
    public void testGetListOfExcelConverters() throws Exception {
        clazz.doSomeAction("abcd");
        verify(converter1).execute();
        verify(converter2).execute();
    }
}

And by "worked for me" I mean that the test actually ran and passed. Note I added a couple of verify statements to assert that the injected mocks got called.

I used the SomeClass code you provided as-is.

于 2016-06-07T19:58:06.200 回答
5

对我来说,现有的两个答案都是不够的。

@riddy 的回答没有考虑不同的测试用例。

@jhericks 的回答没有使用 Spring 上下文,这导致了其他问题。

这是我的解决方案:

@MockBean
@Qualifier("myNamedBean")
private SomeBean someBean;

就如此容易。

于 2019-07-01T09:55:43.703 回答
4

您可以使用测试配置模拟 bean:

@Configuration
public class TestConfig {
   @Bean
   public MyService myService() {
      return Mockito.mock( MyService.class );
   }
}
于 2016-06-07T08:55:23.880 回答
3

我找到了这个解决方案:

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {

  @Mock()
  @Qualifier("converter1")
  IConverter converter1;

  @Mock() 
  @Qualifier("converter1")
  IConverter converter2;

  @InjectMocks SomeClassTest testObj = new SomeClassTest();

  @Test
  public void testGetListOfExcelConverters() throws Exception {
    testObj.doSomeAction("abcd");
    verify(converter1).execute();
    verify(converter2).execute();
  }
}

顺便说一句,我在文档中没有找到这个。

于 2020-07-01T11:39:17.233 回答
0

在我的应用程序中,@Autowired bean 作为构造函数参数传递。所有变体(尽管是 JUnit 5 版本)都不起作用。相反,我不得不“踢它老派”并直接实例化模拟。

public class SomeClass {
  private final IConverter converter1;
  private final IConverter converter2;

  public SomemClass( @Autowired @Qualifier("converter1") conv1,
                     @Autowired @Qualifier("converter2") conv2 ) {
    this.converter1 = conv1;
    this.converter2 = conv2;
  }

  public void doSomeAction(String mimeType) {
    converter1.execute();
    converter2.execute();
  }
}

public class SomeClassTest {
  IConverter converter1;
  IConverter converter2;
  SomeClass pojo;

  @BeforeEach
  public void setup() {
    converter1 = Mockito.mock( IConverter.class );
    converter2 = Mockito.mock( IConverter.class );
    pojo = new SomeClass( converter1, converter2 );
  }

  @Test
  public void testGetListOfExcelConverters() throws Exception {
    pojo.doSomeAction("abcd");
  }
}
于 2022-01-29T19:06:00.137 回答