Java jUnit@beforeAll不工作

Java jUnit@beforeAll不工作,java,junit,Java,Junit,我正在为RESTAPI spring boot编写单元测试 @RunWith(SpringRunner.class) @SpringBootTest(classes = WorkflowEngineApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ActionControllerTest { @LocalServerPort public i

我正在为RESTAPI spring boot编写单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = WorkflowEngineApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ActionControllerTest {
    @LocalServerPort
    public int port;

    private final TestRestTemplate testRestTemplate = new TestRestTemplate();

    private final HttpHeaders headers = new HttpHeaders();

    private long actionId;

    @BeforeClass
    public static void init() throws Exception{
        String url="/api/v1/actions";
        JSONObject action = new JSONObject();
        action.put("name", "Name Test");
        action.put("actionTypeId", 4L);
        action.put("description", null);
        action.put("createdBy", 2L);
        action.put("processId", 2L);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        ResponseEntity<ApiResponse> response = getResponse(url, HttpMethod.POST, action.toString());
    }

    private static ResponseEntity<ApiResponse> getResponse(String url, HttpMethod method, Object action){
        HttpEntity<Object> entity = new HttpEntity<>(action, headers);
        return testRestTemplate.exchange(createURL(url), method, entity, ApiResponse.class);
    }

    /**
     * create url http://localhost:...
     * @param uri String
     * @return String
     */
    public static String createURL(String uri) {
        return "http://localhost:" + port + uri;
    }
}
我想在测试运行之前向Db添加1行。但当我将@BeforeClass与静态方法一起使用时,端口变量必须是静态的,但它不会生成值。还是0。 错误显示:

org.springframework.web.client.ResourceAccessException:上的I/O错误 邮寄申请

我的问题:有没有办法将@BeforeClass与webEnvironment=SpringBootTest.webEnvironment.RANDOM_PORT一起使用,这意味着@LocalServerPort使用静态生成值

我想在测试运行之前向Db添加1行。但当我将@BeforeClass与静态方法一起使用时,端口变量必须是静态的,但它不会生成值。还是0。错误显示:

所以,我的理解是,你们想写下从控制器到数据库的集成测试。这是我的建议

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
@Transactional    
public class MyIntTest {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext ctx;  

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(**Your URL").accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON);

        builder.setContent("Your JSON request data");

        // This will invoke POST request to "URL"
        this.mockMvc.perform(builder);

        // If you want to verify your result
       MvcResult result = this.mockMvc.perform(builder).andReturn();

      // Read response. If its custom class then just change return type.
      List responses = getPojoFromMvcResult(result, List.class);
    }

    public static <T> T getPojoFromMvcResult(MvcResult result, Class<T> valueType) throws IOException {
        try {
            MockHttpServletResponse response = result.getResponse();
            String responseJson = response.getContentAsString();
            return convertJsonToObject(responseJson, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T convertJsonToObject(String content, Class<T> valueType) throws IOException {
        return new ObjectMapper().readValue(content, valueType);
    }
}

但我想直接调用api进行测试。所以我需要在所有测试运行之前添加测试记录!this.mockMvc=MockMvcBuilders.webAppContextSetupctx.build;MockHttpServletRequestBuilder=MockMvcRequestBuilders.post**您的URL.acceptMediaType.APPLICATION\u JSON.contentTypeMediaType.APPLICATION\u JSON;builder.setContentYourJSON请求数据;//这将调用POST请求以URL This.mockMvc.performbuilder;以上代码将调用POST并为您创建数据。