Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Mock annotation from easymock api does not work for two fields of the same type. #755

Closed
ksiczek opened this issue Feb 16, 2017 · 3 comments

Comments

@ksiczek
Copy link

ksiczek commented Feb 16, 2017

Hi,

if we have a simple test class like below

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class TwoObjectsAnnotatedTest {

    @Mock
    private String obj1;
    
    @Mock
    private String obj2;
    
    @Test
    public void test() {
        assertNotNull(obj1);
        assertNotNull(obj2);
    }

}

then both assertions fail. We noticed that whenever two fields of the same type are annotated with
org.powermock.api.easymock.annotation.Mock

If we remove first one or change to

private String obj1 = createMock(String.class);

then it works as expected. We encountered that when migrating from

jdk7 + easymock 3.3 + powermock 1.6.1

to

jdk8 + easymock 3.4 + powermock 1.6.6

OS is: Linux *** 4.4.0-62-generic 83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Does any workaround exist besides replacing annotation with
org.powermock.api.easymock.PowerMock.createMock()
?

UPDATE: it seems to be similar to #668 but I checked and it works for a third field as long as it has different type (I checked for 3 fields - 2 x String and 1 x Integer - and strings do not work but integer does).

@ksiczek
Copy link
Author

ksiczek commented Feb 16, 2017

I've found out that org.powermock.api.easymock.annotation.Mock has a new property called fieldName that solves the problem. However with that additional configuration:

  1. code is less readable and any value different that field name seems to be unreasonable
  2. difference between using @mock and createMock() is not so big

What makes you introduce that feature? Is it documented somewhere?

@thekingn0thing
Copy link
Member

A new fields has been added to fit behaviour of EasyMock and EasyMockRunner.class. Also AnnotationInjectFieldSearcher has been changed with the same purpose. Does it work with @RunWith(EasyMockRunner.class) ?

@ksiczek
Copy link
Author

ksiczek commented Feb 16, 2017

Hi @thekingnothing,

I've checked and it does not change anything. Moreover I almost changed all places in our codes but reached one with org.powermock.api.easymock.annotation.MockNice annotation that is also affected but does not have fieldName to be set.

Regards,

Karol.

@thekingn0thing thekingn0thing added this to the PowerMock 1.7.1 milestone Feb 17, 2017
@ijuma ijuma mentioned this issue Sep 15, 2017
thekingn0thing pushed a commit that referenced this issue Sep 15, 2017
thekingn0thing pushed a commit that referenced this issue Sep 15, 2017
thekingn0thing pushed a commit that referenced this issue Sep 16, 2017
thekingn0thing pushed a commit that referenced this issue Sep 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants