I have the a reducer class that I wanted to write test cases:
Reduce class:
public class MyReducer extends Reducer<Text, Text, NullWritable, Text> {
private static final Logger LOG = LogManager.getLogger(MyReducer.class);
public static List<String> l1 = new ArrayList<String>();
String id = null;
private MultipleOutputs<NullWritable, Text> mos;
#Override
public void setup(final Context context) throws IOException, InterruptedException {
mos = new MultipleOutputs<NullWritable, Text>(context);
final Path[] uris = DistributedCache.getLocalCacheFiles(context.getConfiguration());
try {
final BufferedReader readBuffer1 = new BufferedReader(new FileReader(uris[0].toString()));
String line;
while ((line = readBuffer1.readLine()) != null) {
l1.add(line);
}
readBuffer1.close();
} catch (Exception e) {
LOG.error(e);
}
}
public void reduce(final Text key, final Iterable<Text> values, final Context context)
throws IOException, InterruptedException {
final String[] key1 = key.toString().split("-");
final String keyA = key1[10];
final String date = key1[1];
/* Some condition check */
mos.write(NullWritable.get(), new Text(inputEventValue), keyA + "//date=" +
date.substring(0, 4) + "-" + date.substring(4, 6));
}
#Override
public void cleanup(final Context context) throws IOException, InterruptedException {
mos.close();
}
}
Test Case looks like :
#RunWith(MockitoJUnitRunner.class)
public class MyTest {
#Mock
private MyReducer.Context mockContext;
MyReducer reducer;
MultipleOutputs<NullWritable, Text> mos;
#Before
public void setUp() {
reducer = new MyReducer();
}
#Test
public void myReducerTest() throws Exception {
MyReducer spy = PowerMockito.spy(new MyReducer());
doNothing().when(spy).setup(mockContext);
mos = new MultipleOutputs<NullWritable, Text>(mockContext);
List<Text> sline = new ArrayList<>() ;
List<String> l1 = new ArrayList<String>();
l1.add(“1234”);
sline.add(new Text(“xyz”));
Whitebox.setInternalState(MyReducer.class,”l1", l1);
Whitebox.setInternalState(MyReducer.class,"mos",mos);
reducer.reduce(new Text(“xyz-20200101-1234),sline,mockContext);
}
#After
public void tearDown() throws Exception {
/*
* this will do the clean up part
*/
verifyNoMoreInteractions(mockContext);
}
When running in Debug mode it goes to the reducer's reduce method and fails with NullPointerException where mos write statement is?
Complete Stack trace:
java.lang.NullPointerException
at org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.getNamedOutputsList(MultipleOutputs.java:196)
at org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.<init>(MultipleOutputs.java:324)
at MyTest.myeducerTest
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
Mocking mos errors as mos is not a static.
Any suggestion.
Junit - ReduceDriver, withInput, withOutput,testRun doesn't work.
Thanks.
I tried mocking Multiple outputs as suggested:
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
#Mock
private MyReducer.Context mockContext;
List<String> namedOut = new ArrayList<>();
namedOut.add("NM1");
namedOut.add("NM2");
MultipleOutputs spy = PowerMockito.spy(new MultipleOutputs<>(mockContext));
when(spy, "getNamedOutputsList(mockContext)").thenReturn(namedOut);
But this gives me error : org.powermock.reflect.exceptions.MethodNotFoundException: no method found with name 'getNamedOutputsList(() anyObject())' with parameter types : [] in class org.apache.hadoop.mapreduce.lib.output.MultipleOutputs.
Looks like you did not define what mockContext.getContext() should return for your test, so it returns null and fails.
Based on this sourcecode the methods looks like this (so you might use a different version):
private static List<String> getNamedOutputsList(JobContext job) {
List<String> names = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(
job.getConfiguration().get(MULTIPLE_OUTPUTS, ""), " ");
while (st.hasMoreTokens()) {
names.add(st.nextToken());
}
return names;
}
JobContext seems to refer to your mock Reducer.Context mockContext, so you need to define the appropriate behaviour so that it returns what it is supposed to return.
Note that this call originates from the constructor of MultipleOutputs.
Also take note of the static getCountersEnabled method that is invoked from the constructor and interacts with the context.
Mocking mos errors as mos is not a static.
You could probably use reflections to put a mocked version of mos into your MyReducer class.
Check here for some example on how to mock a private static field.
Edit:
If you try to mock the conig do it like this:
Configuration config = Mockito.mock(Configuration.class);
when(mockContext.getConfiguration()).thenReturn(config);
As far as I see the get that are invoked on the configuration object always provide a default value, so it shouldn't matter if the key/value pair is in there or not.
Related
I have a java class that searches items, but in the method params this class receive a number of max items per query search (batchLines).
Class:
#Override
public List<OrderEntryItemModel> findOrderEntriesByStore(final BaseStoreModel store, final Date modifiedTime,
final int batchLines, final int start) {
final FlexibleSearchQuery query;
if (modifiedTime != null) {
query = new FlexibleSearchQuery(FIND_ORDER_ENTRY_ITEMS_BY_STORE_QUERY + MODIFIED_TIME_PARAM_QUERY);
query.addQueryParameter("modifiedtime", modifiedTime);
} else {
query = new FlexibleSearchQuery(FIND_ORDER_ENTRY_ITEMS_BY_STORE_QUERY);
}
query.setCount(batchLines);
query.setNeedTotal(true);
query.addQueryParameter("store", store);
query.setStart(start);
return getFlexibleSearchService().<OrderEntryItemModel>search(query).getResult();
}
So I have to test this class works fine with using .setCount(). I tried to do the test but always give me 3 and it must give me 1.
Test class:
#UnitTest
#RunWith(MockitoJUnitRunner.class)
public class DefaultLookerOrderEntryItemDaoTest {
private DefaultLookerOrderEntryItemDao lookerOrderEntryItemDao;
#Mock
private FlexibleSearchService flexibleSearchService;
#Mock
private SearchResult<OrderEntryItemModel> searchResult;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
lookerOrderEntryItemDao = new DefaultLookerOrderEntryItemDao(flexibleSearchService);
final OrderEntryItemModel oei1 = new OrderEntryItemModel();
final OrderEntryItemModel oei2 = new OrderEntryItemModel();
final OrderEntryItemModel oei3 = new OrderEntryItemModel();
final List<OrderEntryItemModel> orderEntryItems = new ArrayList<>();
orderEntryItems.add(oei1);
orderEntryItems.add(oei2);
orderEntryItems.add(oei3);
given(flexibleSearchService.<OrderEntryItemModel>search(isA(FlexibleSearchQuery.class))).willReturn(searchResult);
given(searchResult.getResult()).willReturn(orderEntryItems);
}
#Test
public void findOrderEntries(){
given(searchResult.getCount()).willReturn(1);
final List<OrderEntryItemModel> orderEntries = lookerOrderEntryItemDao.findOrderEntriesByStore(new BaseStoreModel(), null, 1, 0);
assertEquals(1, orderEntries.size());
}
}
I am using AEM Mocks to test a custom servlet that uses a configuration, as such:
#Activate
void activate(final Config config) { ... }
I am following the approach described here and here to register and inject the service together with a HashMap, as such:
private static Map<String, Object> myHashMap = new HashMap<>();
...
myHashMap.put("a", "b");
myHashMap.put("c", "d");
...
servlet = context.registerInjectActivateService(new MyServlet(), myHashMap);
However, this approach doesn't work. The config object passed above, inside the activate function, is corrupted. For every key-value pair above, it sets null as the value. So instead of:
a -> b
c -> d
It sets:
a -> null
c -> null
Inside the HashMap. Can anyone please help? Thanks!
P.S. I should add that I am using version 2.3.0 of AEM Mocks since the recent versions cause an issue with an older artifact. For more info on that, see here.
I tested it, and it works with version 2.3.0 too. Could you check the following example? After that, it is probably a maven issue. Then we would need more information.
Here is my test servlet:
#Component(service = Servlet.class,
property = {
SLING_SERVLET_PATHS + "=/bin/servlet/test",
SLING_SERVLET_METHODS + "=GET",
SLING_SERVLET_EXTENSIONS + "=text"
})
#Designate(ocd = TestServlet.Config.class)
public class TestServlet extends SlingSafeMethodsServlet {
#ObjectClassDefinition
public #interface Config {
#AttributeDefinition(
name = "Name",
description = "Name used in the hello world text"
)
String name() default "Alex";
#AttributeDefinition(
name = "Greeting",
description = "Greeting - Morning, to demonstrate the dot-replacement"
)
String greeting_morning() default "Good Morning";
}
private Config config;
#Override
protected void doGet(#Nonnull SlingHttpServletRequest request, #Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
response.getWriter().println(this.getGreeting());
}
public String getGreeting() {
return config.greeting_morning() + ", " + config.name();
}
#Activate
void activate(final Config config) {
this.config = config;
}
}
Here is a JUnit 4 test:
public class TestServletTest {
#Rule
public final AemContext context = new AemContext();
#Test
public void testWithoutConfig() {
final TestServlet testServlet = context.registerInjectActivateService(new TestServlet());
assertEquals("Good Morning, Alex", testServlet.getGreeting());
}
#Test
public void testWithConfig() {
final Map<String, Object> properties = new HashMap<>();
properties.put("name", "Berndt");
properties.put("greeting.morning", "Keep sleeping");
final TestServlet testServlet = context.registerInjectActivateService(new TestServlet(), properties);
assertEquals("Keep sleeping, Berndt", testServlet.getGreeting());
}
}
I have the following code I'm trying to unit test :
if (networkUtils.isOnline()) {
return remoteDataSource.postComment(postId, commentText)
.doOnSuccess(postCommentResponse ->
localDataSource.postComment(postId, commentText))
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.mainThread());
} else {
return Single.error(new IOException());
}
And this is how I'm trying to test it :
#Test
public void postComment_whenIsOnline_shouldCallLocalToPostComment() throws Exception {
// Given
when(networkUtils.isOnline())
.thenReturn(true);
String postId = "100";
String comment = "comment";
Response<PostCommentResponse> response = postCommentResponse();
when(remoteDataSource.postComment(anyString(), anyString()))
.thenReturn(Single.just(response));
// When
repository.postComment(postId, comment);
// Then
verify(localDataSource).postComment(postId, comment);
}
where I fake Response from Retrofit like :
private Response<PostCommentResponse> postCommentResponse() {
PostCommentResponse response = new PostCommentResponse();
response.setError("0");
response.setComment(postCommentResponseNestedItem);
return Response.success(response);
}
but it results to : Actually, there were zero interactions with this mock.
Any ideas ?
EDIT :
#RunWith(MockitoJUnitRunner.class)
public class CommentsRepositoryTest {
#Mock
private CommentsLocalDataSource localDataSource;
#Mock
private CommentsRemoteDataSource remoteDataSource;
#Mock
private NetworkUtils networkUtils;
#Mock
private PostCommentResponseNestedItem postCommentResponseNestedItem;
private CommentsRepository repository;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
BaseSchedulerProvider schedulerProvider = new ImmediateSchedulerProvider();
repository = new CommentsRepository(localDataSource, remoteDataSource, networkUtils, schedulerProvider);
}
// tests
}
When you want to test an Observable you have to subscribe to it so it will start emitting items.
As soon as I used :
TestObserver<Response<PostCommentResponse>> testObserver = new TestObserver<>();
and subscribed to :
repository.postComment(postId, comment)
.subscribe(testObserver);
the test worked as expected.
I'm trying to write unit test for the following code:
public DesiredCapabilities findCapability(Platforms platform, Types type, String browser, double platformVersion) throws Exception{
Criteria criteria = getSession().createCriteria(BrowserCapabilities.class)
.add(Restrictions.eq("status", ICommonConstants.DB_STATUS_ACTIVE))
.add(Restrictions.eq("platformName", platform.getValue()))
.add(Restrictions.eq("browserName", browser))
.add(Restrictions.eq("platformVersion", platformVersion))
.addOrder(Order.desc("browserVersion"))
.setMaxResults(1);
Object res = criteria.uniqueResult();
if(res!=null)
return this.prepareBrowserCapability((BrowserCapabilities)res);
return null;
}
The mock code is:
private BrowserCapabilityDAO mockBrowserCapability;
private Session mockSession;
private Criteria mockCriteria;
private Criterion mockCriterion;
#Before
public void init() {
mockBrowserCapability = Mockito.spy(new BrowserCapabilityDAO());
mockSession = Mockito.mock(Session.class, Mockito.RETURNS_DEEP_STUBS);
mockCriteria = Mockito.mock(Criteria.class, Mockito.RETURNS_DEEP_STUBS);
mockCriterion = Mockito.mock(Criterion.class, Mockito.RETURNS_DEEP_STUBS);
}
#Test
public void testFindCapability() throws Exception {
DesiredCapabilities desiredCapability = new DesiredCapabilities();
BrowserCapabilities mockBrowserCapabilities = getMockBrowserCapabilities();
//Stub
Mockito.doReturn(mockSession).when(mockBrowserCapability).getSession();
Mockito.when(mockSession.createCriteria(Mockito.eq(BrowserCapabilities.class))).thenReturn(mockCriteria);
Mockito.doReturn(mockCriteria).when(mockCriteria).add(mockCriterion);
Mockito.when(mockCriteria.addOrder(Mockito.any(Order.class))).thenReturn(mockCriteria);
Mockito.when(mockCriteria.setMaxResults(Mockito.anyInt())).thenReturn(mockCriteria);
Mockito.when(mockCriteria.uniqueResult()).thenReturn((Object)mockBrowserCapabilities);
//Mockito.doReturn(desiredCapability).when(mockBrowserCapability).prepareBrowserCapability(mockBrowserCapabilities);
mockBrowserCapability.findCapability(Platforms.WINDOWS, Types.COMPUTER, "FF", 10.0);
}
private BrowserCapabilities getMockBrowserCapabilities() {
BrowserCapabilities mockBrowserCapabilities = new BrowserCapabilities();
mockBrowserCapabilities.setBrowserName("browserName");
mockBrowserCapabilities.setBrowserVersion("browserVersion");
return mockBrowserCapabilities;
}
But I'm getting the ClassCastException at line:
return this.prepareBrowserCapability((BrowserCapabilities)res);
cannot be cast to com.common.xxx.persistence.beans.BrowserCapabilities.
In a unit test with camel, I can make asserts using xpath to check if the output xml is correct. But instead, I'd like to use XMLUnit to validate the xml against another entire xml file. Is that possible? The following test succeeds, but I'd like to adjust it to get the actual XML.
#Test
public void testSupplierSwitch() throws Exception
{
MockEndpoint mock = getMockEndpoint("mock:market-out");
mock.expectedMessageCount(1);
EdielBean edielBean = (EdielBean)context.getBean("edielbean");
edielBean.startSupplierSwitch(createCustomer(), createOrder(), "54", "43");
assertMockEndpointsSatisfied();
}
Here is one example of how you can solve it, using mockEndpoint.getExchanges()
public class XmlUnitTest extends CamelTestSupport{
#EndpointInject(uri = "mock:market-out")
MockEndpoint marketOut;
#Override
#Before
public void setUp() throws Exception {
super.setUp();
context.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
from("direct:in")
.setBody(constant("<xml>data</xml>"))
.to(marketOut.getEndpointUri());
}
});
}
#Test
public void sameXml() throws Exception {
marketOut.expectedMessageCount(1);
template.sendBody("direct:in", "body");
marketOut.assertIsSatisfied();
final String actual = marketOut.getExchanges().get(0).getIn().getBody(String.class);
final Diff diff = XMLUnit.compareXML("<xml>data</xml>", actual);
assertTrue(diff.similar());
assertTrue(diff.identical());
}
#Test()
public void notSameXml() throws Exception {
marketOut.expectedMessageCount(1);
template.sendBody("direct:in", "body");
marketOut.assertIsSatisfied();
final String actual = marketOut.getExchanges().get(0).getIn().getBody(String.class);
final Diff diff = XMLUnit.compareXML("<xml>invalid</xml>", actual);
assertFalse(diff.similar());
assertFalse(diff.identical());
}
}