我正在测试服务层,但不确定如何ObjectMapper().readValue在该类中进行模拟。我是新手mockito,可以弄清楚该怎么做。
以下是我的代码,
private configDetail fetchConfigDetail(String configId) throws IOException {
final String response = restTemplate.getForObject(config.getUrl(), String.class);
return new ObjectMapper().readValue(response, ConfigDetail.class);
}
Run Code Online (Sandbox Code Playgroud)
@Test
public void testgetConfigDetailReturnsNull() throws Exception {
restTemplate = Mockito.mock(restTemplate.class);
Service service = new Service();
Config config = Mockito.mock(Config.class);
ObjectMapper objMapper = Mockito.mock(ObjectMapper.class);
Mockito.doReturn("").when(restTemplate).getForObject(anyString(), eq(String.class));
Mockito.doReturn(configDetail).when(objMapper).readValue(anyString(),eq(ConfigDetail.class));
assertEquals(configDetail, service.getConfigDetail("1234"));
}
Run Code Online (Sandbox Code Playgroud)
运行此测试时,我得到以下结果,
com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
at [Source: (String)""; line: 1, column: 0]
Run Code Online (Sandbox Code Playgroud)
在此处发布ServiceTest.Java
@RunWith(MockitoJUnitRunner.class)
public class ConfigServiceTest {
@Mock
private ConfigPersistenceService …Run Code Online (Sandbox Code Playgroud) 我指的是问题,我们可以创建一个std::unique_ptr已删除默认构造函数的类的数组,如下所示,如何传递string参数.
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class A
{
string str;
public:
A() = delete;
A(string _str): str(_str) {}
string getStr()
{
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
unique_ptr<A[]> arr[3] = make_unique<A[]>(3);
// Do something here
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在我的项目的一个头文件中,以下行包含在inline方法中
typedef boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64< boost::archive::iterators::remove_whitespace<std::string::const_iterator>>, 8, 6> Base64ToBin;
Run Code Online (Sandbox Code Playgroud)
当我使用gcc 4.8.2编译它时,我收到以下错误:
错误:'boost :: archive :: iterators :: remove_whitespace <__ gnu_cxx :: __ normal_iterator >>'有一个字段'boost :: archive :: iterators :: remove_whitespace <__ gnu_cxx :: __ normal_iterator >>> ::'其类型使用匿名命名空间[-Werror]
我真的很努力,但无法解决这个问题,同样来自link1和link2,看起来这是一个较低版本的gcc的问题.有人可以建议如何使这个警告保持沉默或者克服这个警告.我正在使用-Werror标志编译.
刚刚开始在我们的项目中使用 Kotlin。要初始化不可变的映射或列表(可能是 Kotlin 中的任何集合),我可以看到两个选项mapOf()和emptyMap()(listOf()和emptyList()表示列表)。
基本上,mapOf只不过是一个返回 的内联函数emptyMap()。
@kotlin.internal.InlineOnly
public inline fun <K, V> mapOf(): Map<K, V> = emptyMap()
Run Code Online (Sandbox Code Playgroud)
哪个比另一个更受欢迎?为什么 Kotlin 公开两者?
我只是想更详细地了解向下转换,我知道这是不可取的,因为它会导致未定义的运行时行为。我们什么时候真的需要使用向下转换?
还要考虑向下转换的编码片段和 dynamic_cast 返回,但是当我使用 reinterpret_cast 时,它正确地打印出派生类函数,如何?只有在按值分配对象而不是按引用或指针分配对象时,对象切片才会发生?
#include <iostream>
using namespace std;
class base
{
public:
void func1()
{
std::cout<<"\n Base Function"<<std::endl;
}
virtual ~base(){cout<<"\n Base Destructor"<<endl;}
};
class derived:public base
{
public:
void func2()
{
std::cout<<"\n Derived Function"<<std::endl;
}
};
int main()
{
base *ptr = dynamic_cast<base*>(new derived); // Up casting
if (ptr)
{
ptr->func1();
}
else
{
cout<<"\n casting failed"<<endl;
}
derived *ptr1 = dynamic_cast<derived*>(new base); // Down casting
if (ptr1)
{
ptr1->func2();
}
else
{
cout<<"\n ptr1 casting …Run Code Online (Sandbox Code Playgroud) c++ ×3
visual-c++ ×2
boost ×1
c++11 ×1
c++14 ×1
eclipse ×1
gcc ×1
gcc-warning ×1
iot ×1
junit ×1
kotlin ×1
libmosquitto ×1
mockito ×1
mosquitto ×1
paho ×1