Hy guys,
我正在尝试在使用spring mvc的Web应用程序上使用mockito进行测试。 当它执行此行“Mockito.reset(notificacaoRepositoryMock);”时,它会抛出“org.mockito.exceptions.misusing.NotAMockException:参数应该是一个模拟,但是: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接口
public interface NotificacaoRepository extends CrudRepository<Notificacao, Long> {
}
并且,我认为,我的pom.xml的相关部分是(spring version and 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();
}
答案 0 :(得分:2)
这是因为Spring在你的bean周围创建了一个代理。
尝试不使用Spring注入模拟
@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();
}
答案 1 :(得分:0)
据我所知,您正试图用模拟版本替换 spring bean。
在这种情况下,您必须使用注释@Primary
注释生成模拟的方法,因此Spring可以在每个自动装配字段中默认选择模拟对象(当然,如果它没有任何限定符)。
如果您在测试中尝试使用Spring上下文,其中包含一些真正的 bean和其他 mock bean,则必须按照以下步骤操作:< / p>
@Configuration
类,该类注入要模拟的bean的模拟实例例如:
注入模拟的配置类
import static org.mockito.Mockito.mock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import xxx.xxx.xxx.NotificacaoRepository;
@Configuration
public class MockConfigurer {
@Bean
@Primary
public NotificacaoRepository registerMock() {
return mock(NotificacaoRepository.class);
}
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppConfig.class})
@WebAppConfiguration
@Import({MockConfigurer.class})
public class NotificacaoControllerTest {
//Now the instance injected here should be a mock object
@Autowired
private NotificacaoRepository notificacaoRepositoryMock;
...
}