Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pradeep dec
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11943
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep dec
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11943
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic