可以建一个基类
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
private static final String token = "eyJ0";
public void setRequestHeader(){
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Authorization",token);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
}
继承这个类,before初始化下,调用方法即可
public class PushTest extends BaseTest {
@Autowired
private DataClassificationService dataClassificationService;
@Before
public void init(){
setRequestHeader();
}
@Test
public void testFenjiFenlei(){
DataClassification d1 = new DataClassification();
d1.setId("ca19528800a341829b0c770eeec866a5");
d1.setParentId("402889126c9d541f016c9daeeeaa001c");
d1.setClassification("access");
d1.setCode("02");
d1.setName("22");
dataClassificationService.updateDataClassification(d1);
}
}