Solved

invoking unit test scripts automatically at build time

Posted on 2013-12-04
10
520 Views
Last Modified: 2013-12-18
Hi,

I would like to run my junit unit test scripts automatically(not manually) when i initiate the build, deploy process on the server. How can i do that. I am using maven.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 6
  • 4
10 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 39698431
I created about 10 of test cases(TestA.java, TestB.java etc). I created test suite named AllTestsVendor.java. How to add test cases TestA.java, TestB.java to  the test suite(AllTestsVendor.java).

I saw below link

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-junit.htm

which says

 You can add or remove test classes from the test suite in two ways:

    Manually by editing the test suite file
    By re-running the wizard and selecting the new set of test classes.

i am not sure how to add either of above ways.

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39698819
i have used this entry in my pom after creating TestSuite and adding test cases

 2 down vote accepted
      

You can run it with -Dit.test=[package].AllTest (-Dtest with surefire), or configure the included tests in the pom:
</plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <configuration>
      <includes>
        <include>AllTest.java</include>
      </includes>
    </configuration>
  </plugin>
	</plugins>

Open in new window




When i do
mvn eclipse:eclipse

then
 mvn clean install
from my command prompt

looks like surefire getting invoked but tests getting skipped.
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ vendor project
[INFO] Not compiling test sources
[INFO] Tests are skipped.
[INFO]


 How to resolve this issue. please advise
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39699937
Something a little strange must be happening, for it to not compile your tests and skip them. I think it may have to do with using a TestSuite. You shouldn't have to do that, Maven can do this all for you, automatically.

Take a look at the below screenshot...
Simple Junit Maven project
In the above you can see that I have setup a simple Maven project. All it contains is 1 class that represents your business logic (Adder in this case) which has 1 method which does something. Note that this class is in "src/main/java".

Then you want to test what this class does. So I have created another class called "AdderTest" (note the fact that it ends with the word "Test", that's important) and put it in "src/test/java". It is just a standard JUnit test case, so I have also added a dependency on junit in my pom.xml (Note that I should have changed the scope of that dependency to "test" but doesn't really affect the result here).

So now everything is in place, I right-click on the project, go to "Run As..." -> "Maven Build..." and then I type in "test" as the maven Goal to run. I could have done "install" too that would have done the same plus packaged and installed the jar into my local repository, for this "test" is enough to demonstrate.

As you can see in the Console output, without specifying plugins or Test Suites, etc Maven has compiled all .java files (in both src/main/java and src/test/java) and then maven-surefire has located "AdderTest" because it is in "src/test/java" and ends with "Test" and asked JUnit to run AdderTest to test my code.


Hopefully the above might be enough for you to work through *your* issue but if not, post back here and include your full pom.xml and the structure of your project, ie. what files and named what and where are they located. Do your Test Cases run ok when run manually from in Eclipse?
0
 
LVL 7

Author Comment

by:gudii9
ID: 39700068
i will be deploying the code on integration/production tomcat servers where while building i would like to invoke these junit scripts automatically (not through eclipse IDE). Please advise how can I achieve it.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39700093
Running them from Eclipse was just an example. There is nothing that requires Eclipse in what I had shown above. You can go to the command line and invoke "mvn install" whilst in that project directory and you would get the same results, ie. the tests would be run for you automatically after the java files are compiled, and only if the tests pass will maven continue to package/install your project.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Author Comment

by:gudii9
ID: 39701394
My test script looks as below
@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes=DifConfig.class, loader=AnnotationConfigContextLoader.class)
public class TestVendor {

	private Logger logger=Logger.getLogger(VendorService.class);

	@Inject
	transient private VendorService vendorService;
	@Test
	public void testVendorList() {

		Map<String, String> vendorList = vendorService.getVendorList();
		
	}

Open in new window


>>Map<String, String> vendorList = vendorService.getVendorList();

what kind of Assert statment i can write around above map object(vendorList ) which comes as result of get method call(getVendorList) on business service class(vendorService)
what is the purpose of

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes=DifConfig.class, loader=AnnotationConfigContextLoader.class)


what is the purpose of @Test
@Inject.



when  we have to  give below command in the command prompt

mvn -Dmaven.test.skip=false -Dapplication.env=production  clean install

I read i can try below way in pom.xml

<plugin>      
      
                                <groupId>org.apache.maven.plugins</groupId>      
      
                                <artifactId>maven-surefire-plugin</artifactId>      
      
                                <version>2.4.2</version>      
      
                                <configuration>      
      
                                        <systemPropertyVariables>      
      
                                                <application.env>production</application.env>      
      
                                        </systemPropertyVariables>      
      
                                </configuration>      
      
                        </plugin>      
what is the purpose of configuration and systemPropertyVariables and plugin.

Is 2.4.2 is version of surefire plugin or maven how do I know.
Interestingly it is reading all the Tests that starts with Test like TestABC, TestCDE, Test000 etc
not ending with Test like FGHTest, 123Test. Not sure why.

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39717340
since i have  below entry in the pom.xml
<configuration>      
     
                                        <systemPropertyVariables>      
     
                                                <application.env>production</application.env>      
     
                                        </systemPropertyVariables>      
     
                                </configuration>  

I updated my command

mvn -Dmaven.test.skip=false -Dapplication.env=production  clean install

as

mvn -Dmaven.test.skip=false  clean install
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39723654
what kind of Assert statment i can write around above map object
Best to open a new question on this, since you are now asking about building the test case rather than the original question of how to run the test case. And when you do, you will need to provide a lot more detail about what VendorService is and what i does, etc.


what is the purpose of

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes=DifConfig.class, loader=AnnotationConfigContextLoader.class)
@RunWith is used to tell JUnit to use a different class (different to JUnit's built in test runner class) to run the tests. 'SpringJUnit4ClassRunner' supports the initialization of a normal Spring application context that the test class can utilise, via standard @Autowired (and @Inject) annotations.

@ContextConfiguration is used to tell the above SpringJUnit4ClassRunner class what app context to initialize/load


what is the purpose of @Test
@Inject.
@Test is your standard JUnit annotation for declaring a particular method is actually code that runs that particular test.

@Inject is a standard annotation that Spring can use (in addition to it's own @Autowired annotation) to know when it should automatically populate an member (of method parameter) with an object/bean from the application context. This is no different to it's use in Spring, ie. outside of a test case.


Is 2.4.2 is version of surefire plugin or maven how do I know
It's the version of surefire plugin. You know this because it appears within the <plugin></plugin> element that describes the surefire plugin.


Interestingly it is reading all the Tests that starts with Test like TestABC, TestCDE, Test000 etc not ending with Test like FGHTest, 123Test. Not sure why.
What I said above, about the class ending with 'Test' for it to be automatically run, was just an example. The surefire plugin actually looks, by default, for a number of class file name patterns. If you look at the documentation for the plugin (http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html) you will see under the "includes" parameter description that the default patterns are set as...
<includes>
 <include>**/Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

Open in new window

This means that it will run tests in any class that starts with 'Test', the end with 'Test' or that end with 'TestCase'. It also means that you could change the default to something else, by changing the configuration in your pom, but you should need to do that. Just keep to that convention (of one of those 3 naming conventions) and you will be ok.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39724296
<includes>
 <include>**/Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

This means that it will run tests in any class that starts with 'Test', the end with 'Test' or that end with 'TestCase'

I wonder why it did not include
FGHTest, 123Test


If you look at the documentation for the plugin (http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html) you will see under the "includes" parameter description that the default patterns are set as...

The documentation on every topic seems huge and i wonder how and where i go pin pointedly to go the one area i am looking like how you pointed above in above documentation. How to improve in that area of searching to find right documentation, information. Please advise
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39725820
I wonder why it did not include FGHTest, 123Test
I think I misread your previous post then. I thought you were wondering why it picked up the Test___ as well as the ___Test classes.

I don't know then why it is picking up FGHTest or 123Test. It's hard to tell what is going on without having everything in front of me.


How to improve in that area of searching to find right documentation, information
Yeah, in terms of Maven things, such as plugins and stuff, I agree that it is a bit hard to find exactly what you need. Even for me, trying to find the reference about what those default "includes" were took a little bit of hunting around. It's hard really to give you any useful advice to make it easier straight away, for me it has just come with experience of needing to do it many times before! Sorry, that that isn't a lot of help to you! :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now