我正在尝试为 Spring Boot 应用程序中的控制器编写单元测试。该应用程序运行顺利,我的问题是运行它tests.
这是测试代码:
@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
@AutoConfigureTestEntityManager
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
private MyRepository myRepository;
@Mock
ZendeskNotifier zendeskNotifier;
@Mock
ActivityLogger activityLogger;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void cannotSendFooWithoutMessageBody() throws Exception {
this.mockMvc.perform(post("/api/v1/foo/1/send"))
.andDo(print())
.andExpect(status().is4xxClientError())
.andExpect(content().string(containsString("The message body cannot be empty.")));
}
}
当我尝试运行它时,我得到:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field jobEventRepository in foo.bar.util.memsource.svc.MemsourceEventProcessor required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
这对我来说感觉很奇怪,因为我提供了AutoConfigureTestEntityManager
注释并期望所有EntityManager
相关的东西要到位。
如果 Google 将您带到这里并且您正在使用 Spring Boot,您可能需要添加@DataJpaTest
到你的测试班。它被发现于org.springframework.boot:spring-boot-test-autoconfigure
。重新运行时您可能还会发现需要声明对org.hibernate:hibernate-validator
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)