Using @All In @Before? Or other alternatives?

53 views
Skip to first unread message

Justin J Gardner

unread,
Jun 4, 2015, 6:56:39 PM6/4/15
to juk...@googlegroups.com
I'm working on a system where we have code that behaves differently in different environments. We basically have an environment object we can query:

interface EnvironmentChecker
{
   
Environment getEnvironment(); // ProductionEnvironment, DevelopmentEnvironment, etc.
}

When working in our code, there are some times where we have different behavior, and we can use mocks to test that the code behaves differently. 

However, in many cases, we want the test to succeed regardless, even if it took different code paths to get there.

String concatStrings(String... strings)
{
   
if (DevelopmentEnvironment == getEnvironment())
    {
       
// Try new high-performance string concat tool.
       
return coolStringConcatTool.concat(strings);
   
}

   
else
   
{
        // Production relies on tried-and-true for now.
        return string1 + string2 + string3... etc;
    }
}

What I want to do, is by default, utilize the @All annotation to automatically run all my tests at all environments. But using @All with @Before doesn't seem to work:
// configure test
public static class Module
            extends JukitoModule
{
     @Override
     protected void configureTest()
     {
         bindMany(Environment.class, AlwaysPRODUCTION.class, AlwaysDEVELOPMENT.class);
     }
}

@Before
void before(EnvironmentChecker mockChecker, @All Environment env)
{
    when(mockChecker.getEnvironment()).thenReturn(evn);
}

@Test
void runThisTestMultipleTimes(AnyParams any) {}

Is there a way to do what I'm looking for? If I manually paste the contents of the @Before method into a regular @Test method, this works, but quite tedious for 1000s of test files :)

Thanks!

Stephan Classen

unread,
Jun 5, 2015, 2:13:39 AM6/5/15
to juk...@googlegroups.com
The @All only works for parameters in @Test methods.
But you can add the same parameter (without @All) to the @Before method. This way the same instance is injected into both methods.

Your example:

// configure test
public static class Module
            extends JukitoModule
{
     @Override
     protected void configureTest()
     {
         bindMany(Environment.class, AlwaysPRODUCTION.class, AlwaysDEVELOPMENT.class);
     }
}

@Before
void before(EnvironmentChecker mockChecker, Environment env)
{
    when(mockChecker.getEnvironment()).thenReturn(evn);
}

@Test
void runThisTestMultipleTimes(
@All Environment env, AnyParams any) {}
--
You received this message because you are subscribed to the Google Groups "Jukito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jukito+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michele Ficarra

unread,
Jul 22, 2015, 11:11:52 AM7/22/15
to Jukito
Hi, I've tried the proposed solution but doesn't work.

This are the interface and classes that I used:

public interface TestInterface {
 
void foo();
}

public class ImplOne implements TestInterface {
 
@Override
 
public void foo() {
 
}
}

public class ImplTwo implements TestInterface {
 
@Override
 
public void foo() {
 
}
}

and this is the test:

@RunWith(JukitoRunner.class)
public class JukitoTest {


 
public static class Module extends JukitoModule {

   
@Override
   
protected void configureTest() {

      bindMany
(TestInterface.class, ImplOne.class, ImplTwo.class);
   
}
 
}

 
@Before
 
public void setup(TestInterface test) {
    test
.foo();
 
}

 
@Test
 
public void testGetFlight(TestInterface test) {
    test
.foo();
 
}
}

I expect that in the in the setup method is injected the instance of the TestInterface, instead is injected a mock.
I'm missing something?

Thanks!

Tim Peierls

unread,
Jul 22, 2015, 11:23:24 AM7/22/15
to juk...@googlegroups.com
You omitted @All from the test method.

Michele Ficarra

unread,
Jul 22, 2015, 12:33:32 PM7/22/15
to Jukito
Sorry typo on coping the example, also with the @All annotation doesn't work, the corrected example is:

@RunWith(JukitoRunner.class)
public class JukitoTest {


 
public static class Module extends JukitoModule {

   
@Override
   
protected void configureTest() {
      bindMany
(TestInterface.class, ImplOne.class, ImplTwo.class);
   
}
 
}

 
@Before
 
public void setup(TestInterface test) {
    test
.foo();
 
}

 
@Test

 
public void testGetFlight(@All TestInterface test) {
    test
.foo();
 
}
}

Tim Peierls

unread,
Jul 22, 2015, 1:26:07 PM7/22/15
to Jukito
Oh, I see what you mean. I think the poster who said

The @All only works for parameters in @Test methods.
But you can add the same parameter (without @All) to the @Before method. This way the same instance is injected into both methods.

was correct about the first point but wrong about the second.

Why not just call your setup method manually in each test method?


--tim 

Michele Ficarra

unread,
Jul 23, 2015, 4:37:44 AM7/23/15
to juk...@googlegroups.com
Hi Tim,
yes, the problem was exactly in the second point. Your workaround works but the code isn't so "clean".
I'm going to open an issue on github, may be they can fix it in the next releases.

Thanks for your answers!


You received this message because you are subscribed to a topic in the Google Groups "Jukito" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jukito/gFhJBynvJeA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jukito+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages