junit4

How make JUnit to print asserts and results

泄露秘密 提交于 2019-12-02 18:01:48
I have some tests like this: @Test public void test01() { Position p = getPositionAt('a', 1); assertNotNull("a1 exists", p); assertNotNull("figure exists a1", p.getFigure()); p = getPositionAt('a', 2); assertNotNull("exists a2", p); assertNull("figure exists a2", p.getFigure()); p = getPositionAt('b', 1); assertNotNull("exists b1", p); assertNull("figure exists b1", p.getFigure()); } What I need while running tests is to print each assert message to stdout and then result of the assert. This is require format from test class: a1 exists -success figure exists a1 -success exists a2 -success

How to pass input from command line to junit maven test program

折月煮酒 提交于 2019-12-02 17:57:58
I wrote a junit test to add two numbers. I need to pass this numbers from command line. I am running this junit test from maven tool as mvn -Dtest=AddNumbers My test program looks like this int num1 = 1; int num2 = 2; @Test public void addNos() { System.out.println((num1 + num2)); } How to pass these numbers from command line? Passing the numbers as system properties like suggested by @artbristol is a good idea, but I found that it is not always guaranteed that these properties will be propagated to the test. To be sure to pass the system properties to the test use the maven surefire plugin

Why Can't I access src/test/resources in Junit test run with Maven?

喜欢而已 提交于 2019-12-02 17:37:24
I am having a problems running the following code: configService.setMainConfig("src/test/resources/MainConfig.xml"); From within a Junit @Before method. Is this the way Maven builds out its target folder? Tomasz Nurkiewicz Access MainConfig.xml directly. The src/test/resources directory contents are placed in the root of your CLASSPATH. More precisely: contents of src/test/resources are copied into target/test-classes , so if you have the following project structure: . └── src └── test ├── java │ └── foo │ └── C.java └── resources ├── a.xml └── foo └── b.xml It will result with the following

maven error: package org.junit does not exist

无人久伴 提交于 2019-12-02 16:28:16
I'm trying to create the javadoc with maven and it fails. It also fails when doing the verify. mvn verify I get the following error: (...) [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /home/miquel/creaveu/createOmegaMatrix/src/main/java/edu/url/salle/gtm/hnm/dataStructures/HFrame.java:[6,23] package org.junit does not exist [ERROR] /home/miquel/creaveu/createOmegaMatrix/src/main/java/edu/url/salle/gtm/hnm/dataStructures/HFrame.java:[6,0] static import only from

What's the actual use of 'fail' in JUnit test case?

二次信任 提交于 2019-12-02 16:02:17
What's the actual use of 'fail' in JUnit test case? Some cases where I have found it useful: mark a test that is incomplete, so it fails and warns you until you can finish it making sure an exception is thrown: try{ // do stuff... fail("Exception not thrown"); }catch(Exception e){ assertTrue(e.hasSomeFlag()); } Note: Since JUnit4, there is a more elegant way to test that an exception is being thrown: Use the annotation @Test(expected=IndexOutOfBoundsException.class) However, this won't work if you also want to inspect the exception, then you still need fail() . lets say you are writing a test

Java/ JUnit - AssertTrue vs AssertFalse

半世苍凉 提交于 2019-12-02 15:24:09
I'm pretty new to Java and am following the Eclipse Total Beginner's Tutorials . They are all very helpful, but in Lesson 12, he uses assertTrue for one test case and assertFalse for another. Here's the code: // Check the book out to p1 (Thomas) // Check to see that the book was successfully checked out to p1 (Thomas) assertTrue("Book did not check out correctly", ml.checkOut(b1, p1)); // If checkOut fails, display message assertEquals("Thomas", b1.getPerson().getName()); assertFalse("Book was already checked out", ml.checkOut(b1,p2)); // If checkOut fails, display message assertEquals("Book

Wanted but not invoke: Mockito PrintWriter

只谈情不闲聊 提交于 2019-12-02 09:47:15
问题 Hi I am working on a project and using PrintWriter class for opening and writing in the file. But when I am writing the test case for same it gives following error at Line 153 Wanted but not invoked: mockPrintWriter.println("ID url1 "); -> at x.y.z.verify(ProcessImageDataTest.java:153) Actually, there were zero interactions with this mock. Code: (Uses Lombok Library) ProcessImageData.java @Setter @RequiredArgsConstructor public class ProcessImageData implements T { private final File

How to write a junit testcase for a void method that creates a new object

大憨熊 提交于 2019-12-02 09:09:24
public class SupportController{ public void disableUserAccount(String username) throws Exception { UserAccount userAccount = new UserAccount(Constants.SYSTEM, Constants.CONTAINER, username); UserAccount.disableAccount(); } } How would i test that the useraccount created is disabled? I would suggest using Mock Objects . Besides that, you can check the JUnit FAQ , where you can find a section about testing methods that return void . Often if a method doesn't return a value, it will have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing

Junit 4.12 Issue testing exception

回眸只為那壹抹淺笑 提交于 2019-12-02 08:44:34
I have a simple method trying to fetch some file. I would like to test when the file is none existent and this is where my problem begins. The test keeps failing. The method is something like : public Configuration populateConfigs(Configuration config) throws UnRetriableException { try { .... } catch (IOException | ConfigurationException e) { log.error(" getConfiguration : ", e); throw new UnRetriableException(e); } throw new UnRetriableException("problem getting config files."); } In my tests I have tried two separate solutions without success. Using the new style as suggested in the SO

JUnit testing for IO

半腔热情 提交于 2019-12-02 06:20:22
I am new here and new to junit testing. I have a class with two methods and I want to write unit tests for it. I am not sure how to start I read some basic tutorials but I am not able to start some how. Can anyone of you provide me some basic skeleton to start with. My class is public class CreateCSV { MyWriter csvOutput = null; public void createSortedSet ( final HashMap< String, Signal > map, final long totalSize, final long totalSizewithHeader, File file ) { ArrayList< Signal > messages = new ArrayList< Signal >(); try { messages.addAll( map.values() ); map.clear(); for ( Signal signal :