简体   繁体   中英

How to create JUnit test cases for Guava's ArrayListMultiMap class in Eclipse?

I have to create some JUnit test cases for Guava's ArrayListMultimap class, but so far, I'm clueless as to how I should proceed. I have successfully been able to create a normal testing class, ArrayListMultimapTest (which I've used to test the methods of the class), in which I added Guava's 15 release JAR file as an external library in the build path. But I've been specifically asked to create JUnit test cases for ArrayListMultimap, and I'm not sure as to how I should proceed. Any help would be appreciated, thanks!

There's already such a test. No idea about maven, but just do

git clone https://code.google.com/p/guava-libraries

Then do everything you need to get in Eclipse running (which needs tons of JARs, but you get them all using maven, add them to the build path), open the file and click "run as junit test". Done.

__

Unless you suspect a specific problem, there's no need to write your own tests for Guava as it gets tested and used heavily. Anyway, even if feel like writing it, you should look at the existing ones first (they're sometimes a bit hard to understand as there's a whole testing framework, which is necessary to test that many classes thoroughly).


git is a version control system free to download here . It uses command line, but the only one you need to know is written above. maven is an incomprehensible "software project management and comprehension tool" also free to download and use.

As a programmer, you'll need them (or other such tools). Even more, you'll need to utfg a lot.

Although the library is extensively tested by Google, you can do this easily yourself, if you want.

  1. right-click your project in Eclipse
  2. select Properties
  3. select Java Build Path
  4. click Add Library... Java Build Path对话框
  5. Select JUnit, pick JUnit4, confirm 添加库对话框

Now you're basically done and you can write your testcases - simply annotate any public void someName() method with the @Test annotation. If you won't have a public static main(String... args) method in your code, then when you'll try to run the class, Eclipse will automatically run it with JUnit.

@Test
public void multimapAcceptsMultipleEqualValuesForOneKey() {
    ListMultimap<String, String> listMultimap = ArrayListMultimap.create();
    listMultimap.put("aha", "eh");
    listMultimap.put("aha", "eh");
    Assert.assertEquals(2, listMultimap.get("aha").size());
}

After that, try to dig into JUnit to learn all the goodness it offers you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM