我有一个带有后处理程序的restcontroller,如下所示。
class Master_programmer:
STATIC_VARIABLE_ONE_FOR_EVERY_INSTANCES = 'This is Static Var'
def __init__(self, name):
self.name = name
self.capabilities = []
self.student = "SoloLearn Student"
self.programmer = "Programmer"
def get_student(self):
return self.student
def get_programmer(self):
return self.programmer
def add_capabilities(self, capability):
self.capabilities.append(capability)
# Create instance for your class and name it coder (or whatever you like)
coder = Master_programmer('Replace me with student name')
# to call coder object's variable
# you need to call it by object name just like below
print('capabilities: ', coder.capabilities)
print(coder.programmer)
print(coder.student)
coder.add_capabilities('Stay Inspired')
coder.add_capabilities('Find Clients')
print(coder.get_student())
print(coder.get_programmer())
print('capabilities: ', coder.capabilities)
print()
# you can invoke Static variables usign directly class name
# you can invoke usign instance name as well but, it is not convention
print(Master_programmer.STATIC_VARIABLE_ONE_FOR_EVERY_INSTANCES)
print()
# if you change Static member, it will get change for all of your instances
coder_2 = Master_programmer('Replace me with student name')
Master_programmer.STATIC_VARIABLE_ONE_FOR_EVERY_INSTANCES = 'changed'
print()
# print static var using both ways
print(Master_programmer.STATIC_VARIABLE_ONE_FOR_EVERY_INSTANCES)
print(coder.STATIC_VARIABLE_ONE_FOR_EVERY_INSTANCES)
print(coder_2.STATIC_VARIABLE_ONE_FOR_EVERY_INSTANCES)
现在,我正在尝试为此控制器编写一个单元测试(请参见下面的代码),但是当我执行该测试文件时,我得到一个“ HttpMessageNotReadableException”。但是,当我使用邮递员发送POST请求时,它就可以正常工作。
@RestController
public class TrajectoryController {
private TrajectoryService trajectoryService;
TrajectoryController(TrajectoryService trajectoryService) { this.trajectoryService = trajectoryService; }
@PostMapping(
path = "api/trajectory",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)
public void createTrajectory(@RequestBody Trajectory t) { trajectoryService.createTrajectory(t); }
我已经查找过它,并且以为自己发布了错误的请求正文,但是正如在控制器中看到的那样,“ createTrajectory”方法请求的是Trajectory类的对象。 我想知道如何解决此问题? 您可以在下面找到输出。
@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TrajectoryControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper mapper;
@Autowired
TrajectoryRepository repository;
private Trajectory trajectory;
@BeforeAll
void clean(){
repository.deleteAll();
}
@Test
@Order(1)
void createTrajectory() throws Exception {
this.trajectory = new Trajectory("MyTrajectory1","MT1");
this.trajectory.setId(1);
MvcResult result = mockMvc.perform(post("/api/trajectory")
.contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(trajectory)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name", is(trajectory.getName())))
.andReturn();
this.trajectory = mapper.readValue(result.getResponse().getContentAsString(), Trajectory.class);
assertNotNull(this.trajectory.getId());
}