【发布时间】:2018-11-18 14:22:50
【问题描述】:
这是我的集成测试控制器类。方法get all team,编译出现问题:
@SpringJUnitWebConfig(classes = CrewApplication.class)
public class Team_Controller_Integration_Test {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
MockitoAnnotations.initMocks(this);
}
@Test
void getAccount() throws Exception {
this.mockMvc.perform(get("/teams")
.accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$version").value(null))
.andExpect(jsonPath("$name").value("Apacze"))
.andExpect(jsonPath("$createOn").value(null))
.andExpect(jsonPath("modifiedOn").value(null))
.andExpect(jsonPath("$description").value("grupa programistow"))
.andExpect(jsonPath("$city").value("Włocławek"))
.andExpect(jsonPath("$headcount").value(null));
}
}
这是我的错误:
java.lang.NullPointerException
另一方面,我为 Db 创建测试并遇到问题,因为在将模拟元素添加到 db 后它们返回 null:
@RunWith(SpringRunner.class)
@DataJpaTest
public class Team_database_integration_test {
@MockBean
private TeamRepository teamRepository;
@Autowired
private TestEntityManager testEntityManager;
@Test
public void testDb(){
Team team = new Team(1L,"teamName","teamDescription","krakow",7);
testEntityManager.persist(team);
testEntityManager.flush();
System.out.println(team);
}
}
【问题讨论】:
-
你试过下面的解决方案了吗!
-
是的,我今天早上试试。我有 java.lang.NullPointerException :/
-
我编辑这篇文章并向你展示我的测试现在的样子
标签: java integration-testing h2