how to mock a mehod using spring boot SpringJUnit4ClassRunner where mongo tamplate is used where the method contains static call

huangapple go评论72阅读模式
英文:

how to mock a mehod using spring boot SpringJUnit4ClassRunner where mongo tamplate is used where the method contains static call

问题

我正在分享一个包含静态方法调用的方法代码。

public List<String> getTestContent() {
    DistinctIterable<String> distinctIterable = mongoTemplate.getCollection("test-doc").distinct("testcontent", String.class);
    return StreamSupport.stream(distinctIterable.spliterator(), false).collect(Collectors.toList());
}

我需要模拟这个我分享的方法。我已经尝试了很多方法,但是一直得到空指针错误。

英文:

i am sharing a method code which contains static method call.

public List&lt;String&gt; getTestContent() {
		
		DistinctIterable&lt;String&gt; distinctIterable = mongoTemplate.getCollection(&quot;test-doc&quot;).distinct(&quot;testcontent&quot;,String.class);
		 return StreamSupport.stream(distinctIterable.spliterator(),false).collect(Collectors.toList());
				 
	}

i need to mock this method which i shared. I have tried lot of approach but getting null pointer

答案1

得分: 0

我已经为您提供的方法编写了测试。

@RunWith(SpringJunit4ClassRunner.class)
public class MyTestClass
{

@MockBean
private MongoTemplate mongoTemplate;

@MockBean
private MongoCollection<Document> mongoCollection;

@MockBean
private DistinctIterable<String> distinctIterable;

@InjectMocks
private ServiceImpl service;

@Before
public void setUp()throws Exception{
    MockitoAnnotations.openMocks(this);
}

public void testGetTestContent(){

    List<String> list = new ArrayList<>();
    list.add("test1");
    list.add("test2");

    Spliterator<String> spliterator = list.spliterator();

PowerMockito.when(mongoTamplate.getCollection(Mockito.eq("test-doc"))).thenReturn(mongoCollection);
PowerMockito.when(mongoCollection.distinct(Mockito.eq("testcontent"),Mockito.eq(String.class))).thenReturn(distinctIterable);
PowerMockito.when(distinctIterable.spliterator()).thenReturn(spliterator);
Assert.assertEquals(2,service.getTestContent().size());
}
}

注意: 如果您不想检查返回值的类型,您还可以使用 PowerMockito.doReturn(..).when()

英文:

I have written test for the method you provided.

@RunWith(SpringJunit4ClassRunner.class)
public class MyTestClass
{

@MockBean
private MongoTemplate mongoTemplate;

@MockBean
private MongoCollection&lt;Document&gt; mongoCollection;

@MockBean
private DistinctIterable&lt;String&gt; distinctIterable;

@InjectMocks
private ServiceImpl service;

@Before
public void setUp()throws Exception{
	MockitoAnnotations.openMocks(this);
}

public void testGetTestContent(){
	
	List&lt;String&gt; list = new ArrayList&lt;&gt;();
	list.add(&quot;test1&quot;);
	list.add(&quot;test2&quot;);

	Spliterator&lt;String&gt; spliterator = list.spliterator();

PowerMockito.when(mongoTamplate.getCollection(Mockito.eq(&quot;test-doc&quot;))).thenReturn(mongoCollection);
PowerMockito.when(mongoCollection.distinct(Mockito.eq(&quot;testcontent&quot;),Mockito.eq(String.class))).thenReturn(distinctIterable);
PowerMockito.when(distinctIterable.spliterator()).thenReturn(spliterator);
Assert.assertEquals(2,service.getTestContent().size());
}
}

Note: You can also use PowerMockito.doReturn(..).when() if you don't want to check the type of value returned

huangapple
  • 本文由 发表于 2020年8月28日 16:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63629835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定