can't find run as junit test in eclipse

2019-03-14 09:14发布

问题:

I created a test class in Eclipse like this

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration
@Transactional
public class TeamTest extends AbstractTransactionalJUnit4SpringContextTests {

 @Test
 public void testCreate() {


  assert (true);
 }}

However, when I click right click on the file I don't see option to run as JUnit!

What is wrong?

I am using Eclipse 3.6

回答1:

Make sure your eclipse environment is using JUnit 4. JUnit 3 doesn't make use of annotations (it uses the old extends TestCase style)

There are few things to double check:

Window > Preferences > Java > JUnit

Are you seeing junit4 or junit3 imports? If that looks good, make sure the project itself is using JUnit4 instead of JUnit3.

Right Click on project > Properties > Java Build Path > Libraries

Is JUnit4 included there? Is anything JUnit related there? If JUnit3 is in there, click on it and click Remove. Then click Add Library... and follow the prompts from there to add JUnit again.

Out of curiosity, are the JUnits run outside of eclipse? Like with a mvn install or whatever build target you have for Ant that'll run JUnits



回答2:

Write a simple test case to see if Eclipse works correctly or not. If simple test case can be run, check your testcase, especially import classes.

Or make a try in "Run" -> "Run Configurations",fill the "Test class" as "TeamTest"(full class name). Then click "Run", see what will happen...



回答3:

The way I fixed it is by changing Test runner in the Run Configuration from the default Junit3 to Junit4. Once I made this change I could see the option Junit when I right clicked on the test class and expanded Run As



回答4:

Make sure you are using Junit as your testing framwork and not something like TestNg which cover most of Junit's purpose.