junit

Grails Unit Tests: Why does this statement fail?

I've developed in Java in the past, and now I'm trying to learn Grails/Groovy using this slightly dated tutorial. import grails.test.* class DateTagLibTests extends TagLibUnitTestCase { def dateTagLib protected void setUp() { super.setUp() dateTagLib = new DateTagLib() } protected void tearDown() { ...

Is there an automated way to make sure that all parts of code is unit tested?

I have written JUnit tests for my class, and would like it to tell me if there is any part of my code that is not unit tested. Is there a way to do this? ...

How do I run JUnit tests from inside my java application?

Is it possible to run JUnit tests from inside my java application? Are there test frameworks I can use (such as JUnit.jar?), or am I force to find the test files, invoke the methods and track the exceptions myself? The reason why I am asking is my application requires a lot of work to start launch (lots of dependencies and configuratio...

Jmock mock DAO object

Hi all, I wrote a method that retrieves certain list of strings, given a correct string key. Now when I create a list(the one to be retrieved by method descibed in previous sentence) and create test I can easily get results and test passes successfully. Now on the other hand if I save the content of this list to database in 2 columns, ...

Java and junit: derivative of polynomial method testing issue

Hello all, im trying to finish up my junit testing for finding the derivative of a polynomial method and im having some trouble making it work. here is the method: public Polynomial derivative() { MyDouble a = new MyDouble(0); MyDouble b = this.a.add(this.a); MyDouble c = this.b; Polynomial poly = new Polynomial (a, b, c); ...

How to override/control the way the JVM gets the system date?

How do you mock-up/trick the JVM to get a date other that the current system date? I have a set of tests in JUnit I don't want to change, but instead I want to change a setting so that when the JVM retrieves the date it retrieves the date I want. Have you done something similar before? Thanks. ...

How To Run integrational Tests

In our project we have a plenty of Unit Tests. They help to keep project rather well-tested. Besides them we have a set of tests which are unit tests, but depends on some kind of external resource. We call them external tests. They can access web-service sometimes or similar. While unit tests is easy to run the integrational tests coul...

Ensure that all getter methods were called in a JUnit test

I have a class that uses XStream and is used as a transfer format for my application. I am writing tests for other classes that map this transfer format to and from a different messaging standard. I would like to ensure that all getters on my class are called within a test to ensure that if a new field is added, my test properly checks f...

Force IOException during file reading

I have the piece of code that reads data from file. I want to force IOException in this code for testing purpose (I want to check if code throws correct custom exception in this case). Is there a some way to create a file which is protected from being read, for example? Maybe dealing with some security checks can help? Please, note th...

Testing Hibernate with JUnit: "session is closed" exception

Hi, sometimes when testing some CRUD operations in my DAO classes using JUnit 4.5, Hibernate throws an exception: org.hibernate.SessionException: Session is closed! The session is not closed explicitly, so what happens? Thanks ...

What’s the strategy to recover data during DAO unit test?

When I test DAO module in JUnit, an obvious problem is: how to recover testing data in database? For instance, a record should be deleted in both test methods testA() and testB(), that means precondition of both test methods need an existing record to be deleted. Then my strategy is inserting the record in setUp() method to recover data....

Instrumentation class in the Android API.

Hi All, I have question on the Android API. Android API provides a class called "Instrumentation" class. What is the use of this class? Is the Instrumentation class be used only together with Junit for unit testing. Can Junit framework can be used to test the methods of the Android API without using the Instrumentation class. Since Ju...

How can I specifiy JUnit test dependencies?

Our toolkit has over 15000 JUnit tests, and many tests are known to fail if some other test fails. For example, if the method X.foo() uses functionality from Y.bar() and YTest.testBar() fails, then XTest.testFoo() will fail too. Obviously, XTest.testFoo() can also fail because of problems specific to X.foo(). While this is fine and I st...

Eclipse doesn't see my new junit test

I'm using eclipse to run the tests in a single junit(4) test class. The tests in the class all run just fine. Then I add an additional test and run the class through the test running in ecplise again. Only the old tests are run. The new test isn't seen by eclipse. There's no error or anything, it's just as if eclipse is looking at an old...

junit assert in thread throws exception

What am I doing wrong that an exception is thrown instead of showing a failure, or should I not have assertions inside threads? @Test public void testComplex() throws InterruptedException { int loops = 10; for (int i = 0; i < loops; i++) { final int j = i; new Thread() { @Override public void run() { ApiProxy.se...

Easy to get a test file into JUnit

Can somebody suggest an easy way to get a reference to a file as a String/InputStream/File/etc type object in a junit test class? Obviously I could paste the file (xml in this case) in as a giant String or read it in as a file but is there a shortcut specific to Junit like this? public class MyTestClass{ @Resource(path="something.xml"...

Hudson CI project doesn't run NetBeans JUnit tests of dependent projects

I have a set of NetBeans java projects with dependencies between them. I added the project at the top of the dependency tree to Hudson for continuous integration. Everything works fine, except that the unit tests of dependent projects don't get run by Hudson. This is because the ant scripts that NetBeans creates has dependent projects se...

spring - @ContextConfiguration fail to load config file in src/test/resources

Hi, I've tried to load the spring config file in src/test/resources classpath with the following abstract class: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:/applicationContext.xml"}) public class BaseIntegrationTests { } I have the applicationContext.xml file in src/test/resources but spring ...

Junit: splitting integration test and Unit tests.

Hello all, I've inherited a load of Junit test, but these tests (apart from most not working) are a mixture of actual unit test and integration tests (requiring external systems, db etc). So I'm trying to think of a way to actually separate them out, so that I can run the unit test nice and quickly and the integration tests after that....

Spring Dependency Injection with TestNG

Spring support JUnit quite well on that: With the RunWith and ContextConfiguration annotation, things look very intuitive @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:dao-context.xml") This test will be able to run both in Eclipse & Maven in correctly. I wonder if there is similar stuff for Test...