File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes How do i define Junit Testing without using @Test annotation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How do i define Junit Testing without using @Test annotation?" Watch "How do i define Junit Testing without using @Test annotation?" New topic
Author

How do i define Junit Testing without using @Test annotation?

Pradeep dec
Greenhorn

Joined: Aug 08, 2013
Posts: 3
I have a Junit Test file which has more number of methods. usually i perform testing for all the methods using @Test annotation. Now, i don't wish to give @Test annotation in all the methids without using annotation how to perform Junit Testing.

Please Note: I'm trying in Test Case not in Test suite.

I have attached my example below without @test annotation can anyone suggest me to write the junit test file without @Test annotation ?

public void Test() throws Exception {
try {
Long autoId = 8435L;
List<MySample> Test = MySampleDao.Test(autoId);
log.info("Test:"+ Test.size());
assertEquals(1, Test.size());
} catch (Exception e) {
log.error("Error in Test", e);
}
public void Sample() throws Exception {
try {
Long autoId = 8435L;
List<MySample> Sample = MySampleDao.Sample(autoId);
log.info("Sample:"+ Sample.size());
assertEquals(1, Sample.size());
} catch (Exception e) {
log.error("Error in Sample", e);
}
}
Note: If i run it shows no methods to test.

*Skip:*I'm trying this concept because i'm going for automation testing
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Note that in Java, there is a convention that methods begin with lower case.

I don't follow why you don't want to use the @Test that JUnit requires. You can do build automation while still using annotations.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

I agree with Jeanne - you can do automation using annotations. No matter whether you use them or not, someone has to write the code. If you are writing the code (whether for automation or unit testing or functional testing or ...) you can add annotations.

This sounds more like an interview question, albeit not one I would want to ask. Something like - "how would you run a unit test in JUnit if annotations weren't available", or "how would you run a unit test in JUnit version 3". Both questions would feel very backward to me, but I could see them being asked if the company had a lot of legacy test cases, and wanted to find out if the candidate could work with them.

Pradeep - the standard for JUnit 3 was use a naming convention for tests. Test classes were named <something>Test.java, and the methods to be run were named test<Someting>. Something like:


By the way - see how I put my code between [code] and [/code] ubb code blocks? This makes it much easier to read the code. You might want to go back and edit your post to add the code tags to make your post more readable.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Pradeep dec
Greenhorn

Joined: Aug 08, 2013
Posts: 3
Thanks Jeane. It could help me if you give me the suitable example to run the junit testing using automation tool by having @Test annotated in all the methods.
Pradeep dec
Greenhorn

Joined: Aug 08, 2013
Posts: 3
Thanks Andrew Monkhouse. My question is:

1. As you said Junit 3 follows naming convention if a class has more than ten methods then how to run all the methods at a stretch ?
2. Please refer this url they follows test case without @Test annotation - > https://github.com/junit-team/junit/blob/master/src/test/java/junit/tests/extensions/RepeatedTestTest.java

Note: Please don't mind if i'm wrong somewhere.

Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Pradeep,
It would depend on which automation tool you are using. Ant provides a task to run JUnit tests. Maven runs JUnit tests. As does Sonar. All use the standard JUnit conventions.

The example you cited - RepeatedTestTest is a JUnit 3.8 test. It uses the naming convention where tests begin with "test". For example
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Pradeep dec wrote:... if a class has more than ten methods then how to run all the methods at a stretch ?


JUnit is not limited by the number of test methods in a class, or the number of test classes. You could have 200 test methods in a single class, and JUnit will just run them all.
 
 
subject: How do i define Junit Testing without using @Test annotation?
 
Similar Threads
Trying to get started using JUnit
Bean values are null
logging test messages
Performace of Updatable resultset
easy Junit Generic ? (Not compiling, did i leave out a class or is my design flawed?)