I am getting the error message for the following code
import java.util.ArrayList;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class TestTarget extends UiDevice {
TestTarget() {
super();
}
public ArrayList<TestTargetView> testTargetViews;
}
Related
When I am trying to MOC the dependent classes (instance variables), it is not getting mocked for abstract class. But it is working for all other classes. Any idea how to resolve this issue. I know, I could cover this code from child classes. But I want to know whether it is possible to cover via abstract class or not. Also, I want to use Mockito to resolve it.
Currently, I am getting a NULL point exception on the following line because the mamApiDao is null & not getting mocked
OvpStatusResponse ovpStatusResponse = mamApiDao.updateOvpMetadataInMam(null, callbackMessage.getMediaId(), ovpStatus, publishMessage);
Abstract class
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qvc.qq.mamapimodel.model.mamapi.OvpStatus;
import com.qvc.qq.mamapimodel.model.mamapi.response.OvpStatusResponse;
import com.qvc.qq.mammessageprocessor.config.settings.MqMessageSettings;
import com.qvc.qq.mammessageprocessor.dao.MamApiDao;
import com.qvc.qq.mammessageprocessor.manager.ErrorMessageManager;
import com.qvc.qq.mammessageprocessor.model.CdnCallbackMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ResponseStatusException;
public abstract class CallbackManager {
private final MamApiDao mamApiDao;
public CallbackManager( MamApiDao mamApiDao) {
this.mamApiDao = mamApiDao;
}
public void processCallback(CdnCallbackMessage callbackMessage, int retries, OvpStatus ovpStatus, String publishMessage) {
// some code
//mamApiDao is NULL, it is not getting mocked
OvpStatusResponse ovpStatusResponse = mamApiDao.updateOvpMetadataInMam(null, callbackMessage.getMediaId(), ovpStatus, publishMessage);
// some code
}
}
Test Class
import com.qvc.qq.mamapimodel.model.mamapi.OvpStatus;
import com.qvc.qq.mamapimodel.model.mamapi.response.OvpStatusResponse;
import com.qvc.qq.mammessageprocessor.config.settings.MqMessageSettings;
import com.qvc.qq.mammessageprocessor.dao.MamApiDao;
import com.qvc.qq.mammessageprocessor.dao.MqMessagingDao;
import com.qvc.qq.mammessageprocessor.model.CdnCallbackMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.isNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class CallbackManagerTest {
#InjectMocks
CallbackManager callbackManager = Mockito.mock(CallbackManager.class, Mockito.CALLS_REAL_METHODS);
#Mock
MamApiDao mamApiDao;
#BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
#Test
public void processCallbackTest() {
when(mamApiDao.updateOvpMetadataInMam(any(), anyString(), any(), anyString())).thenReturn(new OvpStatusResponse());
callbackManager.processCallback(cdnCallbackMessage, 1, OvpStatus.ACTIVE, "published");
verify(mamApiDao, times(1)).updateOvpMetadataInMam(any(), anyString(), any(), anyString());
}
}
Enum Class
public enum OvpStatus {
SUBMITTED("SUBMITTED"),
ACTIVE("ACTIVE"),
INACTIVE("INACTIVE"),
ERROR("ERROR"),
NONE("NONE");
private String value;
private OvpStatus(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
Dao class
import com.qvc.qq.mamapimodel.model.mamapi.OvpStatus;
import com.qvc.qq.mamapimodel.model.mamapi.response.OvpStatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
#Slf4j
#Component
public class MamApiDao {
public OvpStatusResponse updateOvpMetadataInMam(String mamId, String ovpId, OvpStatus status, String publishMessage) {
OvpStatusResponse ovpStatusResponse = new OvpStatusResponse();
ovpStatusResponse.setId(1);
ovpStatusResponse.setStatus("");
return ovpStatusResponse;
}
}
Test Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
can you try like the below one?
public class CallbackManagerTest {
#InjectMocks
CallbackManager callbackManager = Mockito.mock(CallbackManager.class, Mockito.CALLS_REAL_METHODS);
#MockBean
private MamApiDao mamApiDao;
#BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
//do something
This is the class and the corresponding test I have written for it. Can someone please help me understand why System.out.println(a.size()) prints 0 , when it should print 1000?
WorkingwithLists.java
import java.util.ArrayList;
import java.util.List;
public class WorkingwithLists
{
public static void main(String[] args) {
}
public void ListFunctions()
{
List<String > a=new ArrayList<>();
System.out.println(a.size());
}
}
WorkingwithListsTest.java
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;
public class WorkingwithListsTest
{
#Mock
private List<String> a;
private WorkingwithLists workingwithLists;
#BeforeMethod
public void setup()
{
MockitoAnnotations.openMocks(this);
workingwithLists=new WorkingwithLists();
}
#Test
public void testListFunctions() throws Exception
{
when(a.size()).thenReturn(1000);
workingwithLists.ListFunctions();
}
}
I am not sure why you want that, but to achieve that you have to move the "List a" to class level in order to "Mock" and return whatever you want.
I have below example working, Hope this helps :
import java.util.ArrayList;
import java.util.List;
public class WorkingwithLists{
List<String > a=new ArrayList<>();
public static void main(String[] args) {
}
public void ListFunctions(){
System.out.println(a.size());
}
}
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.List;
#RunWith(MockitoJUnitRunner.class)
public class WorkingwithListsTest{
#InjectMocks
WorkingwithLists workingwithLists;
#Mock
private List<String> a;
#Before
public void setup(){
Mockito.when(a.size()).thenReturn(1000);
}
#Test
public void testListFunctions() throws Exception{
workingwithLists.ListFunctions();
Assert.assertTrue(true);
}
}
Output :
Below snippet of code is from ITDIAgentException.java file
Can some one help me understand "why class name is same as class used in import statement"(ITDIAgentException)
import com.ibm.di.entry.Entry;
import com.ibm.di.exception.ITDIAgentException;
public class ITDIAgentException extends Exception {
private Entry entry = null;
public ITDIAgentException(String paramString) { super(paramString); }
public Entry getEntry() { return this.entry; }
public void setEntry(Entry paramEntry) { this.entry = paramEntry; }
}
EDIT
You have ITDIAgentException twice: Once in the import statement, and once in the class definition. You are not allowed to have both (would create a namespace clash in the code), but you can access com.ibm.di.exception.ITDIAgentException (assuming that it is different from the class you are creating) by using the full package and class name.
import com.ibm.di.exception.ITDIAgentException;
public class ITDIAgentException extends Exception {
I'm having a little problem with a unit-test my professor gave me. Upon compilation, I recieve the following errors:
cannot find symbol import org.junit.Assert.assertArrayEquals;
cannot find symbol import org.junit.Assert.assertEquals;
import org.junit.Assert.assertFalse;
import org.junit.Assert.assertTrue;
I have downloaded JUnit and I can compile a similar file, so why am I having problems with this?
The code is:
import java.util.Comparator;
import org.junit.Assert.assertArrayEquals;
import org.junit.Assert.assertEquals;
import org.junit.Assert.assertFalse;
import org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class SortingTests {
class IntegerComparator implements Comparator<Integer> {
#Override
public int compare(Integer i1, Integer i2) {
return i1.compareTo(i2);
}
}
private Integer i1,i2,i3;
private OrderedArray<Integer> orderedArray;
#Before
public void createOrderedArray(){
i1 = -12;
i2 = 0;
i3 = 4;
orderedArray = new OrderedArray<>(new IntegerComparator());
}
#Test
public void testIsEmpty_zeroEl(){
assertTrue(orderedArray.isEmpty());
}
#Test
public void testIsEmpty_oneEl() throws Exception{
orderedArray.add(i1);
assertFalse(orderedArray.isEmpty());
}
#Test
public void testSize_zeroEl() throws Exception{
assertEquals(0,orderedArray.size());
}
}
What you are looking for is a Static import
The line import org.junit.Assert.assertArrayEquals; is referencing the method assertArrayEquals from the class org.junit.Assert
Importing a static method so that it is callable like assertEquals(0,orderedArray.size()); is done with a static import line. Try out the following:
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Alternatively you can:
import static org.junit.Assert.*;
, or you could:
import org.junit.Assert;
and reference the methods like
Assert.assertEquals(0,orderedArray.size());
Assuming that you have the JUnit dependency in the classpath, use import static for the assert methods:
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Or simply use:
import static org.junit.Assert.*;
You should add the keyword static to import it. An example:
import static org.junit.Assert.assertFalse;
If you are using junit version 5 and above then use org.junit.jupiter.api.assertions .
Path has been moved in junit 5
i am getting "java.lang.Exception: Method tearDown should have no parameters" for below code & result.getStatus is null
i have JUnit to run cases
package com;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
public class DeleteLeter {
#Test
public void testw() throws Exception{
WebDriver driver = new FirefoxDriver();
driver.get("http://e4allapac");
int a =10;
int b=20;
Assert.assertEquals(a, b);
}
#After
public void tearDown(ITestResult result){
System.out.println(result.getStatus());
if(ITestResult.FAILURE==result.getStatus())
System.out.println("Fail");
}
}
It sounds like you want to implement a TestWatcher rule - http://junit.org/junit4/javadoc/4.12/index.html.
The basic idea is that you create a class which extends the TestWatcher class, attach it as a #Rule to your test suite and then it will get notified when a test fails.
public class MyWatcher extends TestWatcher {
#Override
protected void failed(Throwable e, Description description) {
// do whatever Selenium magic here.
}
}
public class MyTests {
#Rule
public final MyWatcher myWatcher = new MyWatcher();
#Test
public void testMethod() {
// test stuff goes in here
}
}