Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » How to start unit testing
How to start unit testing [message #1739801] Thu, 04 August 2016 14:24 Go to next message
Onno Kouwenberg is currently offline Onno KouwenbergFriend
Messages: 18
Registered: January 2015
Junior Member
I've created my projects using the wizards, and I'm missing the place to start using JUnit. If I create a JUnit test in a separate 'test' folder Eclipse kindly offers to add Junit to the required bundles, but of course that's not what I want.

To be honest, I've been using Gradle as a build system lately and there it is very easy as Gradle expects test to be in the test/java and the Eclipse Gradle plugin completely understands this too.

I opened the maven pom.xml, but I have the impression that all this XML isn't meant for humans to read.

What's the best way to start unit testing with JUnit?
Re: How to start unit testing [message #1739803 is a reply to message #1739801] Thu, 04 August 2016 14:44 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 478
Registered: December 2010
Location: Germany
Senior Member
You could add junit to the required bundles (or required packages) and set the dependency to be optional.
Re: How to start unit testing [message #1739808 is a reply to message #1739803] Thu, 04 August 2016 15:56 Go to previous messageGo to next message
Onno Kouwenberg is currently offline Onno KouwenbergFriend
Messages: 18
Registered: January 2015
Junior Member
Although it no longer complains about the JUnit class, it seems that it requires the complete target environment to run.

java.lang.NoClassDefFoundError: org/eclipse/core/runtime/ISafeRunnable
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:497)
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.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.ClassNotFoundException: org.eclipse.core.runtime.ISafeRunnable
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 24 more

Re: How to start unit testing [message #1739860 is a reply to message #1739808] Fri, 05 August 2016 06:44 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2494
Registered: July 2012
Senior Member
The question is what you want to test how? Do you only want to perform Java unit tests? Than your setup should be ok. Although in a Maven setup I would recommend to separate your tests in a fragment. If you want to do integration tests and need the OSGi runtime, you should create a new bundle that contains your tests.

I have written a blog post about that recently with regards to OSGi declarative services. But it should explain the idea.

http://blog.vogella.com/2016/07/04/osgi-component-testing/
Re: How to start unit testing [message #1739882 is a reply to message #1739860] Fri, 05 August 2016 10:46 Go to previous messageGo to next message
Onno Kouwenberg is currently offline Onno KouwenbergFriend
Messages: 18
Registered: January 2015
Junior Member
The first test I tried to create was the following:
@Test
	public void emptyDocument() {
		RecipePartitioner partitioner = new RecipePartitioner();
		Document document = new Document("");
		partitioner.connect(document);
		assertSame(partitioner, document.getDocumentPartitioner());
	}


Where the implementation code is:
	@Override
	public void connect(IDocument document) {
		this.document = document;
		document.setDocumentPartitioner(this);
	}


This triggers the document class to start partitioning with a SafeRunnable, which requires more of the OSGi framework.
Of course I can use Mockito to mock the document class, in Gradle I'd simply add a testCompile dependency. In the current setup I can of course add a mockito dependency as optional, but it seems a rather wierd strategy. I'm sure there must be another way.

@Dirk, In my Felix based OSGi project I'm do JUnit testing in a fairly standard, and easy, way. A bundle has a compile dependency on a service api, and I use mockito to mock the service. As with the Gradle conventions source is in src/main/java, tests are in src/test/java. Only the src/main/java end up in the OSGi bundle. I expect Maven to have a similar mechanism.
Re: How to start unit testing [message #1739885 is a reply to message #1739882] Fri, 05 August 2016 11:55 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2494
Registered: July 2012
Senior Member
That depends on your project setup. The project layout you expect is not the default in Eclipse. If you want to use maven tycho for building you shouldn't expect that layout.
Re: How to start unit testing [message #1739946 is a reply to message #1739885] Fri, 05 August 2016 19:10 Go to previous messageGo to next message
Onno Kouwenberg is currently offline Onno KouwenbergFriend
Messages: 18
Registered: January 2015
Junior Member
I understand. It feels like a barrier of entry at the moment, where it is hard to make the choice either to abandon Eclipse as a target platform. Time will learn.
Re: How to start unit testing [message #1739952 is a reply to message #1739946] Fri, 05 August 2016 21:29 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6361
Registered: July 2009
Senior Member
Maybe tycho & pde is not the right platform we ship a r5 repo which you can use with bndtools in the midterm we'll switch to that platform but there still open issues.
Previous Topic:Running and exporting Tutorial 3 (Java8) problems
Next Topic:Possibly wrong dependency on EMF Edit (in 2.4 release)
Goto Forum:
  


Current Time: Fri Sep 22 04:43:09 GMT 2017

Powered by FUDForum. Page generated in 0.04428 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software