Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6214267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:50:47+00:00 2026-05-24T06:50:47+00:00

I have implemented a programm with the strategy pattern. So I have an interface

  • 0

I have implemented a programm with the strategy pattern. So I have an interface which is used at some places and the concrete implementation may be replaced.

Now I want to test this programm. I would like to do it in a similar way. Write a test once, which tests against the interface. The concrete interface implementation should be injected at the beginning of the test, so that I may replace it easily.

My testclass looks similar to this one:

public class MyTestClass {

    private StrategeyInterface strategy;

    public MyTestClass(StrategeyInterface strategy) {
        this.strategy = strategy;
    }
    ....test methods using the strategy.
}

The parameterized contructor must used to be inject the concrete strategy implementation at thr beginning og the tests.

Now I did not get TestNG to run it and inject the concrete implementation instance. I tried several ways with inheritance, @DataProvider, @Factory and the corresponding methods, but without luck.

Here is what the testNG report says:

Can't invoke public void MyClass.myTestMethod(): either make it static or add a no-args constructor to your class

I use the maven surefire plugin to run the tests. Here is the relevant part of the pom.xml:

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

How do I write and run the tests, with injecting a concrete implementation into the test class?

Thanks in advance.

P.S. I could deliver more code I tried. I did not post it here, yet, because I tried so many variants so that I am kind of confused now and all of them fail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:50:48+00:00Added an answer on May 24, 2026 at 6:50 am

    You have several options. If you are using Guice, here is a very straightforward way to inject your implementation.

    If not, you can use a mix of factories and data provider:

    @Factory(dataProvider = "dp")
    public FactoryDataProviderSampleTest(StrategyInterface si) {
    }
    
    @DataProvider
    static public Object[][] dp() {
      return new Object[][] {
        new Object[] { new Strategy1Impl() },
        new Object[] { new Strategy2Impl() },
      };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a small Windows Service which runs every 9 minutes and writes
i have a input tag which is non editable, but some times i need
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented a SAX parser in Java by extending the default handler. The
I have implemented VirtualPathProvider class so I can keep all my views in the
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
We have implemented a popup window as a modal dialog using the IE method:
I have implemented authentication systems for webapps several times over the years, but before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.