Running JUnit tests with JUnitCore runner

1,045 views
Skip to first unread message

Paws

unread,
Jul 4, 2012, 10:48:38 AM7/4/12
to eclim-user
Hello Eric,

I am sending mail as a follow up for discussion we had yesterday.

When I try to run junit test using command :call
eclim#java#util#Java('org.junit.runner.JUnitCore', '<testname>') I
receive error:

JUnit version 4.8.2 2 Could not find class: <testname>

Test is compiled in target/test-classes and I have below entry
in .classpath:

<classpathentry kind="src" path="src/test/java" output="target/test-
classes" including="**/*.java"/>

It looks like eclim isn't handling that 'output' attribute correctly.

Could you please verify this?

Thanks,

Marcin

Eric Van Dewoestine

unread,
Jul 4, 2012, 10:57:19 AM7/4/12
to eclim...@googlegroups.com
Last night I fixed[1] the classpath logic to include the src entry
'output' dirs and I was able to successfully run individual test cases
via the junit runner after making the change.

When you get the chance, give it a shot and let me know how it goes.

[1] https://github.com/ervandew/eclim/commit/ecfd33196ec87cd1e2665a5f8272c9e135929d8f

--
eric

Paws

unread,
Jul 5, 2012, 7:38:49 AM7/5/12
to eclim-user
>
> Last night I fixed[1] the classpath logic to include the src entry
> 'output' dirs and I was able to successfully run individual test cases
> via the junit runner after making the change.
>
> When you get the chance, give it a shot and let me know how it goes.
>
> [1]https://github.com/ervandew/eclim/commit/ecfd33196ec87cd1e2665a5f8272...
>
> --
> eric

I tested this change and it works also for me :)

However in .vimrc I my mapping looks like this: map <leader>t :call
eclim#java#util#Java('org.junit.runner.JUnitCore', '<testname>')<cr>
and <testname> is not evaluated.

Could you let me know how how to fix this?

Thanks,

Marcin

Eric Van Dewoestine

unread,
Jul 5, 2012, 9:08:12 AM7/5/12
to eclim...@googlegroups.com
This should do the trick:

nmap <leader>t :call eclim#java#util#Java(
\ 'org.junit.runner.JUnitCore',
\ eclim#java#util#GetFullyQualifiedClassname())<cr>

--
eric

Marcin Bednarek

unread,
Jul 5, 2012, 11:08:03 AM7/5/12
to eclim...@googlegroups.com

>This should do the trick:

 > nmap <leader>t :call eclim#java#util#Java(
 > \ 'org.junit.runner.JUnitCore',
 > \ eclim#java#util#GetFullyQualifiedClassname())<cr>


Indeed. Works perfect.

Thank You,

Marcin

Paws

unread,
Aug 3, 2012, 11:13:37 AM8/3/12
to eclim-user
Eric,

Is it possbile somehow to run only one test in a class ? (one method)

Thanks,

Marcin

Eric Van Dewoestine

unread,
Aug 3, 2012, 1:21:53 PM8/3/12
to eclim...@googlegroups.com
Eclim doesn't provide that functionality and although maven[1] and ant
provide the means to do that, I couldn't find a reference to the standard
junit runner providing that functionality. Someone did come up with a
way to inject[2] that functionality into their test suite though.

The more I think about it, the means with which I implemented
:JUnitExecute in eclim was a result of the fact that eclim's testing
requires much more than simply running a unit test runner against some
test case classes. Because of that and the assumption that most people
would be using maven or ant, I opted for configuring a command to be
execute to run test cases (namely Ant or Mvn). However, after having
implemented :Java and :Javac, I think it would be nice to have a junit
command that can run test cases without relying on an external ant or
maven build file, so I've added a todo for myself to add that feature.
I'll be sure to make sure the feature can support running individual
test methods.

[1] http://maven.apache.org/plugins/maven-surefire-plugin/examples/single-test.html
[2] http://blogs.steeplesoft.com/2010/10/running-a-single-junit-test/

--
eric

Paws

unread,
Aug 4, 2012, 6:37:30 AM8/4/12
to eclim...@googlegroups.com
 I checked today and looks like below solution works:
 
http://stackoverflow.com/questions/9288107/run-single-test-from-a-junit-class-using-command-line

however I don't know if there is an easy way to integrate it with Eclim.

Thanks,

Marcin 

Eric Van Dewoestine

unread,
Aug 4, 2012, 11:37:37 AM8/4/12
to eclim...@googlegroups.com
Well you can add the new runner to your project's classpath and update
the mapping I gave you earlier in this thread (or add a new one) to
invoke the new runner using eclim's eclim#java#util#Java(..) function.

--
eric

Paws

unread,
Aug 15, 2012, 1:18:08 PM8/15/12
to eclim...@googlegroups.com

Well you can add the new runner to your project's classpath and update
the mapping I gave you earlier in this thread (or add a new one) to
invoke the new runner using eclim's eclim#java#util#Java(..) function.

Sounds good, could you write how can I get method name which is under the cursor?

Eric Van Dewoestine

unread,
Aug 15, 2012, 10:20:53 PM8/15/12
to eclim...@googlegroups.com
Below is a quick function that will search back in the file for the
closest @Test annotated method and return its name, so you can call
this with the cursor pretty much anywhere within the method
body/definition to get its name. It's not 100% accurate since you
could be in some non-annotated method or just outside the test method's
body and it will still return the nearest test method name above the
cursor, but it's pretty simple and should get the job done.

function! TestMethodName()
let pos = getpos('.')
try
if search('@Test\>', 'bWc')
if search('\s\w\+(', 'W')
return expand('<cword>')
endif
endif
finally
call setpos('.', pos)
endtry
return ''
endfunction

--
eric

Paws

unread,
Sep 23, 2012, 8:25:09 AM9/23/12
to eclim...@googlegroups.com
Hello Eric,

I added TestMethodName to my .vimrc, created mapping: 

nnoremap <silent> <buffer> <leader>ju :call eclim#java#util#Java('org.junit.runner.SingleJUnitTestRunner', eclim#java#util#GetFullyQualifiedClassname()."\\#".TestMethodName())<cr> 


When I add this jar to maven dependencies and I run mvn elipse:eclipse jar is added to project classpath to I can run test for only one method. 

I wanted to ask however if I do not want to add my jar to maven dependencies is there some directory in Eclim or Eclipse where I can copy this jar with SingleJUnitTestRunner class so it will be visible to all projects?

Thanks,

Marcin

Eric Van Dewoestine

unread,
Sep 23, 2012, 10:06:14 AM9/23/12
to eclim...@googlegroups.com
On 2012-09-23 05:25:09, Paws wrote:
> Hello Eric,
>
> I added TestMethodName to my .vimrc, created mapping:
>
> nnoremap <silent> <buffer> <leader>ju :call
> eclim#java#util#Java('org.junit.runner.SingleJUnitTestRunner',
> eclim#java#util#GetFullyQualifiedClassname()."\\#".TestMethodName())<cr>
>
> and created jar with class from
> http://stackoverflow.com/questions/9288107/run-single-test-from-a-junit-class-using-command-line
> .
>
> When I add this jar to maven dependencies and I run mvn elipse:eclipse jar
> is added to project classpath to I can run test for only one method.
>
> I wanted to ask however if I do not want to add my jar to maven
> dependencies is there some directory in Eclim or Eclipse where I can copy
> this jar with SingleJUnitTestRunner class so it will be visible to all
> projects?
>
> Thanks,
>
> Marcin

While a little gross, you should be able to accomplish what you want
by adding the jar to the list of jre jars:

Window > Preferences > Java Installed JREs

Then pick the jre you are using, then "Edit", followed by "Add
External Jars". Any jar added there *should* be auto added to the
classpath when running java from eclim.

It's also worth noting that I've started working on an all new :JUnit
command which should be able to replace your custom mapping and the
need for that custom runner. It should be available on github in the
next week or two depending on the amount of time I get to work on it.

--
eric

Paws

unread,
Sep 23, 2012, 3:12:30 PM9/23/12
to eclim...@googlegroups.com
While a little gross, you should be able to accomplish what you want
by adding the jar to the list of jre jars:

    Window > Preferences > Java Installed JREs

Then pick the jre you are using, then "Edit", followed by "Add
External Jars". Any jar added there *should* be auto added to the
classpath when running java from eclim.

It's also worth noting that I've started working on an all new :JUnit
command which should be able to replace your custom mapping and the
need for that custom runner. It should be available on github in the
next week or two depending on the amount of time I get to work on it.


This solution of course works. Will check github each day for this new JUnit ;-)

Thanks,

Marcin 
Reply all
Reply to author
Forward
0 new messages