У меня была очень похожая проблема. Цель заключалась в том, чтобы издеваться над сервисом, который сохраняет объекты и может вернуть их по имени. Служба выглядит так:
public class RoomService {
public Room findByName(String roomName) {...}
public void persist(Room room) {...}
}
Служебный макет использует карту для хранения экземпляров комнаты.
RoomService roomService = mock(RoomService.class);
final Map<String, Room> roomMap = new HashMap<String, Room>();
// mock for method persist
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0 && arguments[0] != null) {
Room room = (Room) arguments[0];
roomMap.put(room.getName(), room);
}
return null;
}
}).when(roomService).persist(any(Room.class));
// mock for method findByName
when(roomService.findByName(anyString())).thenAnswer(new Answer<Room>() {
@Override
public Room answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0 && arguments[0] != null) {
String key = (String) arguments[0];
if (roomMap.containsKey(key)) {
return roomMap.get(key);
}
}
return null;
}
});
Теперь мы можем запустить наши тесты на этот макет. Например:
String name = "room";
Room room = new Room(name);
roomService.persist(room);
assertThat(roomService.findByName(name), equalTo(room));
assertNull(roomService.findByName("none"));
Здесь:
jdata = [] # create a list
for n in response:
#convert byte string to string
jdata.append({n.decode("utf-8")}) # to get rid of "b" prefix
Вы создаете список наборов. Это должно быть:
jdata = [] # create a list
for n in response:
#convert byte string to string
jdata.append(n.decode("utf-8"))
или более просто:
jdata = [n.decode("utf-8") for n in response]
также, целью bytes.decode(encoding)
является не «избавление от префикса b», а построить Unicode-строку из байтовой строки