junit

Set property to mock object jmock

Is it possible to assing value to mock object. Ex: myMockObject = context.mock(MyObject.class); myMockObject.setId("someId"); My method which I'm testing reaches the end, but at the end there is method for validation of that object so object without id is considered to be invalid. Is there anything else I can do about this? Can I so...

hibernate - junit noob - class variable value lost between 2 test methods

... I got a simple integration test going and all test methods run fine ... BUT... I set up a class var int tempId; for use by the following methods. testSaveTag() sets the value when successfully executed ( for now everything's auto committed) and the testUpdateTag() updates the freshly created tag. @Test public void testSaveTag()...

Designing a robust unit test - testing the same logic in several different ways?

Hi, In unit test design, it is very easy to fall into the trap of actually just calling your implementation logic. For example, if testing an array of ints which should all be two higher than the other (2, 4, 6, 8, etc), is it really enough to get the return value from the method and assert that this pattern is the case? Am I missing...

JUnit Theory problem

Hello, I am writing a test case where in I want to run a one DataPoint for one test case and second DataPoint for second test case. @RunWith(Theories.class) public class DummyTest { @DataPoints public static String[] getFileNames() { return new String[] { "firstFile.txt","firstFile1.txt" }; } @Theory publi...

Format an int with zero padding in Java

How do I get the following assertion to succeed? { int i = 5; assertEquals("005", String.format("%1??s", i)); } Problem: I need to format an int as a string of equal length. ...

How does Java generate signatures for Methods?

Hey there! I have an Java class with a static final method getAll: public static final Vector<Category> getAll(Context context, ContentValues where) { ArrayList<Integer> IDs = null; if(where != null && where.containsKey(DatabaseAdapter.KEY_PRODUCT)) { IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_PRODUCT_CAT...

Where to place a supplementary classes, used in JUnit?

My unit tests need supplementary classes, mostly as factories for creating objects of classes under test. For example: // EmployeeTest.java public class EmployeeTest { @Test public void testName() { Employee emp = EmployeeTestFactory.get(); // static method assert(emp.getName() instanceof String); } } // SalaryTest.java publ...

@BeforeClass still running the method everytime any test is run

I am using the @BeforeClass annotation to ensure that a set of activities are done just once in a set of 5-6 tests. There is a hierarch of 3 java files. File1 extends TestCase File2 extends File 1 (this is where i have to put the beforeclass annotation at the setUp method) File3 extends File2 (File 3 has the tests.. 5 in number, b...

Drools testing with junit

Hi What is the best practice to test drools rules with junit? Until now we used junit with dbunit to test rules. We had sample data that was put to hsqldb. We had couple of rule packages and by the end of the project it is very hard to make a good test input to test certain rules and not fire others. So the exact question is that how ...

Unit testing both the presence/absence of a library

The question: Is there a way I can use a ClassLoader to test both the presence and absence of the library I'm checking for? I have a some code which will use a particular library if available, or fall back to some embedded code if not. It works fine but I'm not sure how to unit test both cases in the same run, or if it's even possible. ...

Unit testing Spring 3 database methods

I have abstract class that defines database methods, such as inserts and updates, and a concrete class that implements it. The database configurations (dataSource, DatabaseTarget etc) are defined as beans in context.xml-file. The database with its methods work in Controller when I'm using Spring 3 anotations by private AbsractClass a;...

How to get failure trace with Junit4

hello, in my Junit test, I use usually "AssertEquals" and when the test fails, the trace is properly displayed in the Failure trace of JUnit/eclipse I would like to know how to get these trace to show it in a file? @Test public void testButtons() { SelectionButton().ButtonFile(); assertEquals("selected button should be...

Running .class file with a .java file

I have a ListTester.java file in which I've created with some unit tests in there to check the built in List class in Java. I have also been given a List.class file to have the junit tests check against to make sure they are correct. However, i'm not sure how to make sure the .class file is utilized by my .java file when it runs the ...

Defining global source lookup settings for all JUnit tests in Eclipse

Hi. Is it possible to define global source lookup settings for all JUnit tests in Eclipse? Currently I have to set it for every test in the Debug configuration settings. If there would be a global way to do this it would be much more comfortable for me. ...

HTML Custom JUnit Report Uneven Table Alignment

I am coding a java class that generates HTML table reports for JUnit tests and use CSS for visual formatting. I am having an issue aligning the cells since the number of colummns generated is unforseeable since some of these columns represent parameters passed into a variadic function. Therefore there is inherent misalignment in the colu...

Can JUnit simulate OutOfMemoryErrors?

I have a method which tries to call an in-memory image converter, and if that fails, then tries to do the image conversion on disk. (The in-memory image converter will try to allocate a second copy of the image, so if the original is very large, we might not have sufficient memory for it.) public BufferedImage convert(BufferedImage img,...

Testing for Exceptions using JUnit. Test fails even if the Exception is caught.

Hi, I am new to testing with JUnit and I need a hint on testing Exceptions. I have a simple method that throws an exception if it gets an empty input string: public SumarniVzorec( String sumarniVzorec) throws IOException { if (sumarniVzorec == "") { IOException emptyString = new IOException("The input...

Direction with JUnit Testing

I am trying to wrap my head around Junit testing and have read examples and what not, but still finding it difficult to understand how and what to test. Below is the Class with methods that I am creating my test cases from (as well as my test case Class). import java.util.Iterator; /** * The Probability class understands the likeliho...

How to isolate other methods in the same class using EasyMock

I have a method that invokes other method in the same class such as: Class MyClass{ public int methodA(){ ... methodB() ... } public int methodB(){ ... } } And I just wanna test methodA(), so how can I isolate methodB() using EasyMock. My damn way is to create a fake instance of MyClass and inject ...

Run IntelliJ JUnit Tests in Serial

Hi, I'm using the JUnit runner in IntelliJ to run all tests in a package. When I run my tests in isolation they pass, when I run the entire package some fail, suggesting interplay between the tests. I'd like to force the tests to be run in serial rather than parallel to prove this - can anyone tell me how I can configure this to happen?...