@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class) //测试类最好放在主包(带有Application类的包里面)
public class MetricsTest {
private MockMvc mockMvc; //没有使用@Component 注解的类 需要使用new 自己创建
@Autowired
ObjectMapper objectMapper; // 使用@Component 注解的类,只能通过注入的方式引用(因为Spring默认是单例模式)
@Before
public void setup() throws Exception {
mockMvc = standaloneSetup(new MetricAPI()).build();
}
/**
* Adds a valid metric
* @throws Exception
*/
@Test
public void addMetricTest() throws Exception{
mockMvc.perform(post("/addMetric")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(new Metric())))
.andExpect(status().isOk());
}
}