我是 Spring Boot 的新手,我真的很喜欢它,尤其是在消除样板代码方面。 我创建了一个测试类来测试我的
NBRController
:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NewBusinessRevitalizationApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"management.port=0"})
public class NBRControllerTest extends TestCase {
@LocalServerPort
private int port;
@Value("${local.management.port}")
private int mgt;
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void getApplicationByAgencyIdAndStatusTest() {
String uri = "http://localhost:" + this.port + "/nbr-services/applications/{status}?agencyIds=123456,56765,678576";
Map<String, String> vars = new HashMap<String, String>();
vars.put("status", "SAVED");
ResponseEntity<String> response = testRestTemplate.getForEntity(uri, String.class, vars);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}
如果我在调试模式下运行它,我只能调试 Test 类,而不能调试我的
NBRController
类:
@RestController
@RequestMapping("/nbr-services")
public class NBRController {
@Autowired
private NBRServices nbrServices;
private static Logger logger = LoggerFactory.getLogger(NBRController.class);
@RequestMapping(value = "/configuration/environment/{environment}", method = RequestMethod.GET)
@ResponseBody
public String getConfiguration(@PathVariable("environment") String environment) throws RemoteException {
logger.debug("environment={}", environment);
String result = nbrServices.getConfiguration(environment);
return result;
}
}
我尝试设置 Tomcat 调试端口,但不成功。 我调试
NBRController
的唯一方法是在调试模式下运行它并从浏览器调用我的 RestAPI,但我想使用我的单元测试。预先感谢!
当我不小心有 2 个具有相同路径映射的控制器方法时,就发生了这种情况。
其他调试替代方案:
可以不使用分割的 webEnvironment 来调试系统,而是使用 spring MockMVC 对控制器进行直接方法调用而不是 http 调用。
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.MOCK // this is the default
)
@AutoConfigureMockMvc
class MyTest {
@Autowired
private MockMvc mockMvc;
@Test public void myTest() {
mockMvc.perform("/mypath");
// ...
}
}
这实际上不会在 jUnit 类和控制器之间进行 http 调用,因此不会测试此 http 处理部分。
这将生成 2 个 JVM,但 IDE 已连接到这两个 JVM,因此所有断点都可以工作。
我正在使用 Intellij 2020.3,我可以调试我的控制器。
您运行的端口可能与您认为的端口不同。
SpringBootTest 注解用于控制测试端口,例如
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
在您要附加的 Uri 中 http://local host +port ,这不是必需的, testRestTemplate 会为您完成。删除它并尝试您可能会达到调试点
这里是为 Spring boot 2 + JUnit 5 的 Rest 层编写 Junit 的示例
@ExtendWith(MockitoExtension.class)
public class RestTest {
@InjectMocks
private Rest rest;
private MockMvc mockMvc;
@BeforeEach
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(rest).build();
}
@Test
public void getTest() throws Exception {
String url = "/test";
ResultActions resultActions = mockMvc.perform(get(url));
resultActions.andExpect(status().isOk());
}
}
@RestController
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class Rest {
@GetMapping
public @ResponseBody String get() {
return "success";
}
}
就个人而言,我使用
maven-spring-boot
插件,当我调试时,它来自 Maven 运行配置。也许这就是你所做的事情的问题所在? maven-spring-boot
插件在测试阶段将在测试运行之前启动 Spring Boot 服务器。
如果您想使用命令行应用程序来执行此操作,则必须在测试运行之前手动加载 Spring 上下文并通过几行代码执行主类。我不记得如何做。