Showing posts with label Unit Test. Show all posts
Showing posts with label Unit Test. Show all posts

Monday, September 7, 2026

How to Skip Integration Tests in Maven but Run Unit Tests

How to Skip Integration Tests in Maven but Execute Unit Tests

It was not so easy when I was trying all the possible options and lost 3 hours of my valuable time. It is really very frustrating when you are trying to do something and it is not happening within your expected time.

This happened to me when I was trying to build the project without executing the integration tests. My scenario was to skip only the integration tests, but the unit test cases must still be executed.

I Googled almost many links, but had no luck. Finally, the solution worked for me with the below pom.xml configuration.

Add the below tag into your pom.xml:

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <excludes> <exclude>**/*IntegrationTest.java</exclude> </excludes> </configuration> </plugin> </plugins> </build>

Here you can use the <exclude> tag and mention any test class pattern that you want to skip.

For example, if your Java class name contains ITest.java (i.e. MyMethodITest.java):

<exclude>**/*IT*.java</exclude>

Or:

<exclude>**/*AnyTestClasses.java</exclude>

Not only integration tests, you can skip any test classes using the <exclude> tag, but you need to configure the pom.xml properly.

In the Maven command, you can use the normal command as below:

mvn clean install

Note: Be careful about the plugin version because the configuration may behave differently with different versions. Make sure the version you are using supports the configuration you need.


Thursday, July 16, 2015

Mocking Is Null After @InjectMocks in Mockito

Mocking is null after injecting the mock, this issue is very common in mockito. I faced this issue when trying to write the junit test case . 

This issue seems you need to load the test class using MockitoAnnotations.initMocks. Because , there is certain steps you need to do / setup the data before executing the test method.

@InjectMocks
    private TokenServiceFormatter tokenServiceFormatter ;

As above I have already injected the respective class, that I want to inject. But, while running its showing the tokenServiceFormatter  (injected object ) is null. So, inside the setup() method you need to load the test class as below.


    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);    
    }

 @Test
public void testMethod(){
//Do your test here
}


Happy Mockinggggggggggggg.