当我尝试使用模拟mvc时,没有得到此类bean异常。看来自动配置mvc方法不起作用。不确定为什么会这样。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 28 more
下面是代码
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AssessmentanalyticsApplication.class)
@AutoConfigureMockMvc
public class AssessmentanalyticsApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void contextLoads() throws Exception {
AssessmentAttemptData data = new AssessmentAttemptData();
data.setAssessmentAttemptCount("1");
data.setAssessmentCompletionCount("2");
data.setAssessmentId("1");
data.setHighScore(new BigDecimal(1));
data.setLowScore(new BigDecimal(1));
MvcResult result = (MvcResult) mockMvc.perform(MockMvcRequestBuilders.post("/assessmentanalytics/recordAttempt")
.accept(MediaType.APPLICATION_JSON));
}
}