Why am getting this stacktrace? - java

After compiling my code I'm getting this Exception
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
I'm using eclipse IDE with ADT plugin.
import android.support.test.uiautomator.*;
import android.support.test.InstrumentationRegistry;
import android.support.test.internal.runner.junit3.*;
import android.support.test.internal.runner.listener.InstrumentationResultPrinter;
import android.support.test.rule.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import android.os.RemoteException;
import android.support.*;
#RunWith(JUnit4.class)
public class uia {
private UiDevice d;
#Before
public void setup() throws RemoteException {
d = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
d.isScreenOn();
}
#Test
public void home(){
d.pressHome();
}
}
After running this code am getting the following trace:
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at ui.uia.setup(uia.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Any suggestions?

Instrumentation tests need to be #RunWith(AndroidJUnit4.class) and not #RunWith(JUnit4.class)
Please check the documentation for more details,

Related

Mockito is throwing org.mockito.exceptions.base.MockitoException: At the moment you cannot provide own implementations of MockHandler

While trying to mock an object inside a JUnit test in android, mockito is throwing org.mockito.exceptions.base.MockitoException: At the moment you cannot provide own implementations of MockHandler
Below is the code of unit test
import androidx.lifecycle.Observer
import com.google.android.gms.maps.model.LatLng
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner
//#RunWith(MockitoJUnitRunner::class)
class StoreViewModelTest {
lateinit var observer: Observer<StoreList>
#Mock lateinit var storeUseCase: StoreUseCase
private lateinit var viewModel: StoreViewModel
#Before
fun setUp() {
MockitoAnnotations.initMocks(this)
viewModel = StoreViewModel(storeUseCase)
}
#Test
fun loadstore_ExecutesUseCase() {
viewModel.setId(123)
verify(storeUseCase, times(1)).execute(any())
}
}
The error is thrown at the statement MockitoAnnotations.initMocks(this)
StackTrace :
org.mockito.exceptions.base.MockitoException: At the moment you cannot provide own implementations of MockHandler.
Please see the javadocs for the MockMaker interface.
at com.app.ui.store.StoreViewModelTest.setUp(StoreViewModelTest.kt:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

How to solve java.lang.NoClassDefFoundError? Selenium

Why this code doesn't work?
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MainTest {
private WebDriver wd;
private String urll;
#Before
public void openGoogle()
{
wd = new FirefoxDriver();
urll = "https://google.com";
}
#Test
public void firstTest()
{
wd.get(urll);
}
#After
public void closeBrow()
{
wd.quit();
}
}
Error stack trace :
java.lang.NoClassDefFoundError: com/google/common/base/Function
at MainTest.openGoogle(MainTest.java:15)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 24 more
java.lang.NullPointerException
at MainTest.closeBrow(MainTest.java:28)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
The error says it all :
java.lang.NoClassDefFoundError: com/google/common/base/Function
at MainTest.openGoogle(MainTest.java:15)
While working with Selenium v3.x you have to download geckodriver.exe from mozilla/geckodriver and place it in your system. Next you have to set the system property through the line System.setProperty() as follows and provide the absolute path of the GeckoDriver binary within your system as follows :
#Before
public void openGoogle()
{
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
wd = new FirefoxDriver();
urll = "https://google.com";
}
Finally Instead of mentioning import org.junit.*; as per best practices mention the distinct exports as follows :
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
Here you can find a detailed discussion on NoClassDefFoundError

Transform Scala future to CompletableFuture Error

I´m using this library (https://github.com/scala/scala-java8-compat) to transform a Scala Future to Java 8 CompletableFuture.
But running this example:
package utils;
import akka.dispatch.Futures;
import org.junit.Test;
import scala.concurrent.Future;
import java.util.concurrent.CompletableFuture;
import scala.compat.java8.FutureConverters;
import scala.concurrent.Promise;
public class ScalaFuture {
#Test
public void test() throws InterruptedException {
Promise promise = Futures.promise();
Future scalaFuture = promise.future();
CompletableFuture cf = FutureConverters.toJava(scalaFuture).toCompletableFuture();
cf.whenComplete((result, t) -> {
System.out.println(result);
});
promise.success("works");
Thread.sleep(1000);
}
I have this error
java.lang.NoClassDefFoundError: scala/Function1$class
at scala.concurrent.java8.FuturesConvertersImpl$CF.<init>(FutureConvertersImpl.scala:18)
at scala.compat.java8.FutureConverters$.toJava(FutureConverters.scala:60)
at scala.compat.java8.FutureConverters.toJava(FutureConverters.scala)
at utils.ScalaFuture.test(ScalaFuture.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: scala.Function1$class
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 26 more
Any idea what's wrong with my code?

Getting ExceptioninitializerError in JUnit

Below is the error which i am getting while running JUnit test case.
While running JUnit test case for SV as code, I am getting the below error, even though all the SV as Code jars. the code is for create virtual services. In case i remove the line "forGet(URL)......;" the test case would pass, else it throws an error.
java.lang.ExceptionInInitializerError
at com.ca.svcode.protocols.http.agent.HttpAgentProtocol.getInterceptor(HttpAgentProtocol.java:77)
at com.ca.svcode.protocols.http.agent.HttpAgentProtocol.getInterceptor(HttpAgentProtocol.java:48)
at com.ca.svcode.engine.IpvConnectedServer.start(IpvConnectedServer.java:135)
at com.ca.svcode.engine.IpvProtocolServer.withConnection(IpvProtocolServer.java:67)
at com.ca.svcode.protocols.http.fluent.impl.HttpTransactionBuilderImpl.buildTransaction(HttpTransactionBuilderImpl.java:77)
at com.ca.svcode.protocols.http.fluent.AbstractHttpTransactionBuilder.doReturn(AbstractHttpTransactionBuilder.java:132)
at TestClass.testSimpleHttpGetWithResponseCodeAndStringBody(TestClass.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.RuntimeException: Cannot load initialize agent with HTTP Protocol.
at com.ca.svcode.protocols.http.agent.HttpInterceptor.<clinit>(HttpInterceptor.java:45)
... 32 more
Caused by: com.ca.svcode.agent.AgentException: Failed to initialize agent for protocol
at com.ca.svcode.agent.Agent.initialize(Agent.java:76)
at com.ca.svcode.protocols.http.agent.HttpInterceptor.<clinit>(HttpInterceptor.java:43)
... 32 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at com.ca.svcode.agent.AgentLoader.loadAgent(AgentLoader.java:62)
at com.ca.svcode.agent.Agent.initialize(Agent.java:70)
... 33 more
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.ca.svcode.agent.AgentLoader.dynamicLoadOfVirtualMachine(AgentLoader.java:87)
at com.ca.svcode.agent.AgentLoader.loadAgent(AgentLoader.java:60)
... 34 more
Below is the code:
import static com.ca.svcode.protocols.http.fluent.HttpFluentInterface.*;
import static org.junit.Assert.*;
import static com.ca.svcode.protocols.http.fluent.HttpFluentInterface.forGet;
import com.ca.svcode.engine.junit4.VirtualServerRule;
import org.junit.Rule;
import org.junit.Test;
public class TestClass {
GetCurrentWeather gcw = new GetCurrentWeather();
String URL = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=14c206d2c4fc6e04f152395f98fbfe20";
String JSON_EXAMPLES_PORTFOLIO = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"stations\",\"main\":{\"temp\":291.6,\"pressure\":1018,\"humidity\":48,\"temp_min\":291.15,\"temp_max\":292.15},\"visibility\":10000,\"wind\":{\"speed\":8.2,\"deg\":270},\"clouds\":{\"all\":56},\"dt\":1503161400,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0042,\"country\":\"GB\",\"sunrise\":1503118420,\"sunset\":1503169985},\"id\":2643743,\"name\":\"London\",\"cod\":200}";
int CUSTOM_STATUS_CODE = 200;
#Rule
public VirtualServerRule vs = new VirtualServerRule();
#Test
public void testSimpleHttpGetWithResponseCodeAndStringBody() throws Exception {
forGet(URL).doReturn(aMessage(200).withJsonBody("{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"stations\",\"main\":{\"temp\":291.6,\"pressure\":1018,\"humidity\":48,\"temp_min\":291.15,\"temp_max\":292.15},\"visibility\":10000,\"wind\":{\"speed\":8.2,\"deg\":270},\"clouds\":{\"all\":56},\"dt\":1503161400,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0042,\"country\":\"GB\",\"sunrise\":1503118420,\"sunset\":1503169985},\"id\":2643743,\"name\":\"London\",\"cod\":200}"));
/*GetCurrentWeather gw = new GetCurrentWeather();
String printResponse = gw.place("http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=14c206d2c4fc6e04f152395f98fbfe20");
System.out.println(printResponse);*/
assertEquals(200, CUSTOM_STATUS_CODE);
}
}
Pls suggest.
One of the SV jars, has a dependency on JDK1.8 jar(tools.jar), so we need to add JDK1.8 library in buildpath of the project.

Java Mocking FTP Session

I have an application that deals with ftp server, now I want to write some test, but I got trouble in the beginning, I am trying to mock the session factory but it throws exception and could not understand why, here is my code:
#Mock
SessionFactory<FTPFile> sessionFactory;
#Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost(host);
sf.setPort(port);
sf.setUsername(username);
sf.setPassword(password);
Mockito.when(sessionFactory.getSession()).thenReturn((Session<FTPFile>) new CachingSessionFactory<FTPFile>(sf));
}
and here is the exception I got:
java.lang.IllegalArgumentException: [Assertion failed] - this String argument must have text; it must not be null, empty, or blank
at org.springframework.util.Assert.hasText(Assert.java:168)
at org.springframework.util.Assert.hasText(Assert.java:181)
at org.springframework.integration.ftp.session.AbstractFtpSessionFactory.setHost(AbstractFtpSessionFactory.java:101)
at com.melita.maltapay.schedule._ScheduledTasks.initMocks(_ScheduledTasks.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Can someone tell me where I did it wrong? :/
If you are looking to test your FtpClient you could use MockFtpServer is very simple to configure and use in tests.
The MockFtpServer project provides mock/dummy FTP server
implementations that can be very useful for testing of FTP client
code. Two FTP Server implementations are provided, each at a different
level of abstraction.

Categories