admin管理员组文章数量:1291004
I have this test class in intellij, with multiple test cases when i run them individually they pass but not when run all at once
package comp.pnp.pgs.ab.topology.bolts;
import comp.pnp.pgs.ab.topology.config.SidelineFailTupleKafkaBoltConfig;
import comp.pnp.pgs.ab.topology.kafka.SidelineFailTuplePublisher;
import comp.pnp.pgs.ab.topology.models.SidelineEvent;
import comp.pnp.pgs.ab.topology.models.SidelineMetaData;
import comp.pnp.pgsmons.models.ListingPriceAnomalyRequest;
import com.google.inject.Injector;
import .apache.storm.task.OutputCollector;
import .apache.storm.task.TopologyContext;
import .apache.storm.tuple.Tuple;
import .junit.Before;
import .junit.Test;
import comp.pnpmons.init.StormGuiceContext;
import .mockito.*;
import java.util.HashMap;
import static comp.pnp.pgs.ab.topology.Constants.DYNAMIC_ANOMALY_KAFKA_EVENT;
import static comp.pnp.pgs.ab.topology.Constants.SIDELINE_METADATA;
import static .mockito.Mockito.*;
public class SidelineFailTupleBoltTest {
@Mock
private OutputCollector outputCollector;
@Mock
private TopologyContext topologyContext;
@Mock
private SidelineFailTuplePublisher sidelineFailTuplePublisher;
@Mock
private SidelineFailTupleKafkaBoltConfig sidelineFailTupleKafkaBoltConfig;
@Mock
private Tuple tuple;
@Mock
private Injector injector;
private SidelineFailTupleBolt sidelineFailTupleBolt;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(injector.getInstance(SidelineFailTuplePublisher.class)).thenReturn(sidelineFailTuplePublisher);
StormGuiceContext.init(injector);
when(topologyContext.registerMeter(anyString())).thenReturn(null);
sidelineFailTupleBolt = new SidelineFailTupleBolt(sidelineFailTupleKafkaBoltConfig);
sidelineFailTupleBolt.prepare(new HashMap<>(), topologyContext, outputCollector);
Mockito.reset(sidelineFailTuplePublisher);
}
@Test
public void testExecuteSuccess() {
ListingPriceAnomalyRequest request = new ListingPriceAnomalyRequest();
SidelineMetaData metaData = new SidelineMetaData();
when(tuple.getValueByField(DYNAMIC_ANOMALY_KAFKA_EVENT)).thenReturn(request);
when(tuple.getValueByField(SIDELINE_METADATA)).thenReturn(metaData);
when(sidelineFailTuplePublisher.publish(any(SidelineEvent.class))).thenReturn(true);
sidelineFailTupleBolt.execute(tuple);
verify(outputCollector, times(1)).ack(tuple);
}
@Test
public void testExecuteFailure() {
ListingPriceAnomalyRequest request = new ListingPriceAnomalyRequest();
SidelineMetaData metaData = new SidelineMetaData();
when(tuple.getValueByField(DYNAMIC_ANOMALY_KAFKA_EVENT)).thenReturn(request);
when(tuple.getValueByField(SIDELINE_METADATA)).thenReturn(metaData);
when(sidelineFailTuplePublisher.publish(any(SidelineEvent.class))).thenReturn(false);
when(sidelineFailTuplePublisher.isRetryLimitExceeded(any(SidelineEvent.class))).thenReturn(false);
sidelineFailTupleBolt.execute(tuple);
verify(outputCollector, times(1)).fail(tuple);
}
@Test
public void testExecuteException() {
when(tuple.getValueByField(DYNAMIC_ANOMALY_KAFKA_EVENT)).thenThrow(new RuntimeException("Test Exception"));
sidelineFailTupleBolt.execute(tuple);
verify(outputCollector, times(1)).fail(tuple);
}
@Test
public void testExecuteCallsPushDataToSidelineKafka() {
ListingPriceAnomalyRequest request = new ListingPriceAnomalyRequest();
SidelineMetaData metaData = new SidelineMetaData();
SidelineEvent sidelineEvent = new SidelineEvent(request, metaData);
when(tuple.getValueByField(DYNAMIC_ANOMALY_KAFKA_EVENT)).thenReturn(request);
when(tuple.getValueByField(SIDELINE_METADATA)).thenReturn(metaData);
when(sidelineFailTuplePublisher.publish(sidelineEvent)).thenReturn(true);
sidelineFailTupleBolt.execute(tuple);
verify(outputCollector, times(1)).ack(tuple);
}
}
getting the following error
Wanted but not invoked: outputCollector.fail(tuple); -> at comp.pnp.pgs.ab.topology.bolts.SidelineFailTupleBoltTest.testExecuteFailure(SidelineFailTupleBoltTest.java:86)
However, there were other interactions with this mock: outputCollector.ack(tuple); -> at comp.pnp.pgs.ab.topology.bolts.SidelineFailTupleBolt.execute(SidelineFailTupleBolt.java:54)
even when i mock the response when(sidelineFailTuplePublisher.publish(sidelineEvent)).thenReturn(false); to return false it is returning true. i think it is due to data leak between the test cases. so i have tried mock.reset() for all mocks. and an @After method to reset the mocks. nothing worked
本文标签: junittest cases failing when run togetherbut pass when run individuallyStack Overflow
版权声明:本文标题:junit - test cases failing when run together, but pass when run individually - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741522165a2383252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论