Junit测试 - 每项测试的操作。如何最小化代码? [关闭]

问题描述 投票:0回答:2

我真的不知道如何在标题中更好地描述它,但在这里解释:

我想为Rest Api编写测试。含义:我为每次测试登录服务器,运行我的呼叫并注销。如果我可以在测试开始时以某种方式登录到服务器,执行所有调用(尽管仍处于单独的测试中)然后注销,这将是更少的代码和更高效的方式。

有一种聪明的方法吗?

谢谢你的每一个回复!

unit-testing testing junit server assert
2个回答
0
投票

你看过注释标签了吗?即@Before和@After标签

例如:

@Before
private void loginToServer() throws Exception {
   /* Some code to do your login 
    and some code to do your repetitive tests
}

@Test
private void testEvents() {
//// Your test code 
}

@After
private void logoutServer() throws Exception {
/// Code to logout of your server
}

这样,在运行您在@Test类中设置的任何内容之前,您的代码将始终执行Before标记。并且@After课程在完成后将始终注销。


0
投票

你应该使用@BeforeClass和@AfterClass。

© www.soinside.com 2019 - 2024. All rights reserved.