gpt4 book ai didi

java - Mockito 使用 spring mvc 注入(inject)抛出 NotAMockException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:39 26 4
gpt4 key购买 nike

大家好,

我正在尝试在使用 spring mvc 的 Web 应用程序上使用 mockito 进行测试。当它执行这一行 "Mockito.reset(notificacaORepositoryMock);" 它抛出 "org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class com.sun .proxy.$Proxy40"

我在我的例子中看到它有效,我在这里找不到我做错了什么。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebAppConfig.class})
@WebAppConfiguration
public class NotificacaoControllerTest {

private MockMvc mockMvc;

@Autowired
private NotificacaoRepository notificacaoRepositoryMock;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() {
// *** Error here ***
Mockito.reset(notificacaoRepositoryMock);

mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

我的 TestContext 是:

@Configuration
public class TestContext {

@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

messageSource.setBasename("i18n/messages");
messageSource.setUseCodeAsDefaultMessage(true);

return messageSource;
}

@Bean
public NotificacaoRepository notificacaoRepository() {
return Mockito.mock(NotificacaoRepository.class);
}
}

我想要模拟的类是 CrudRepository 接口(interface)

public interface NotificacaoRepository extends CrudRepository<Notificacao, Long> {
}

而且,我认为,我的 pom.xml 的相关部分是(spring 版本和 mockito)

<properties>
<hibernate.version>4.2.0.Final</hibernate.version>
<mysql.connector.version>5.1.21</mysql.connector.version>
<spring.version>4.1.6.RELEASE</spring.version>
<spring.data.version>1.8.0.RELEASE</spring.data.version>
</properties>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>

更新

@jfcorugedo 我完全按照你说的做了尝试,但我一直收到同样的错误。我的测试上下文只是

@Configuration
public class TestContext {
@Bean
@Primary
public NotificacaoRepository notificacaoRepository() {
return Mockito.mock(NotificacaoRepository.class);
}
}

现在我的测试类是:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppConfig.class})
@WebAppConfiguration
@Import({TestContext.class})
public class NotificacaoControllerTest {

@Autowired
NotificacaoRepository notificacaoRepositoryMock;

@Before
public void setUp() {
Mockito.reset(notificacaoRepositoryMock); // Error >> org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class com.sun.proxy.$Proxy55
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

最佳答案

这是因为 Spring 围绕您的 bean 创建了一个代理。

尽量不要使用 Spring 注入(inject) mock

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebAppConfig.class})
@WebAppConfiguration
public class NotificacaoControllerTest {

private MockMvc mockMvc;

private NotificacaoRepository notificacaoRepositoryMock;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() {
notificacaoRepositoryMock = Mockito.mock(NotificacaoRepository.class);
Mockito.reset(notificacaoRepositoryMock);

mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

关于java - Mockito 使用 spring mvc 注入(inject)抛出 NotAMockException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com