Tano
Tano

Reputation: 1377

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? :/

Upvotes: 0

Views: 5034

Answers (1)

Pau
Pau

Reputation: 16116

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.

Upvotes: 1

Related Questions