Hello guys,
I wanted to use FTPServer with Spring, but I have some unit tests
failing. I am using it to unit test a FTP Client connector that I'm
working on.
You only need to start and stop the server a couple of times. Always
the second test fails with a NPE.
This is the unit test file (it is very very basic):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/ftpContextTest.xml" })
public class FTPServerSpringTest {
@Autowired
private FtpServer ftpServer;
@Before
public void before() throws FtpException {
ftpServer.start();
}
@Test
public void test1() {
System.out.println("nothing");
}
@Test
public void test2() {
System.out.println("nothing");
}
@After
public void after() {
ftpServer.stop();
}
}
This is the spring config file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ftpserver="http://mina.apache.org/ftpserver/spring/v1"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://mina.apache.org/ftpserver/spring/v1
http://mina.apache.org/ftpserver/ftpserver-1.0.xsd">
<ftpserver:server id="ftpServer" anon-enabled="true">
<ftpserver:listeners>
<ftpserver:nio-listener name="default" port="3333"
local-address="localhost" />
</ftpserver:listeners>
</ftpserver:server>
</beans>
The stack trace:
java.lang.NullPointerException
at
org.apache.ftpserver.impl.DefaultFtpServer.start(DefaultFtpServer.java:74)
at
com.pfalabs.ftpserver.FTPServerSpringTest.before(FTPServerSpringTest.java:22)
Digging into this deeper it seems this code will get you the NPE really fast:
ftpServer.start();
ftpServer.stop();
ftpServer.start();
--> java.lang.NullPointerException
at
org.apache.ftpserver.impl.DefaultFtpServer.start(DefaultFtpServer.java:74)
at com.pfalabs.ftpserver.FTPServerTest.test1(FTPServerTest.java:40)
Looking at the code I'm not sure I am supposed to be doing that:
start/stop/start, but this is the way I have my unit tests.
One workaround would be to have a static method to initialize the
FTPServer Component, but that would be ignoring spring altogether.
Or maybe forcing spring to NOT return a singleton each time it will
try to inject the bean can fix this.
This is not a blocking issue for me, Its something I've seen while
trying to take FTPServer for a spin.
thanks,
alex