How to use JUnit tests with Spring Roo? (Problems with EntityManager)

后端 未结 7 560
忘了有多久
忘了有多久 2021-01-02 19:40

I\'m trying to write a JUnit test for a Spring Roo project. If my test requires use of the entity classes, I get the following Exception:

java.lang.IllegalS         


        
7条回答
  •  旧巷少年郎
    2021-01-02 20:26

    Your unit test class should have @MockStaticEntityMethods annotation.

    Just wanted to add more detail to the above answer by @migue as it took me a while to figure out how to get it to work. The site http://java.dzone.com/articles/mock-static-methods-using-spring-aspects really helped me to derive the answer below.

    Here is what I did to inject entity manager via test class. Firstly annotate your test class with @MockStaticEntityMethods and create MockEntityManager class (which is a class that just implements EntityManager interface).

    Then you can do the following in your ServiceExampleTest test class:

    @Test
    public void testFoo() {
      // call the static method that gets called by the method being tested in order to
      // "record" it and then set the expected response when it is replayed during the test
      Foo.entityManager();
      MockEntityManager expectedEntityManager = new MockEntityManager() {
        // TODO override what method you need to return whatever object you test needs
      };
      AnnotationDrivenStaticEntityMockingControl.expectReturn(expectedEntityManager);
    
      FooService fs = new FooServiceImpl();
      Set foos = fs.getFoos();
    }
    

    This means when you called fs.getFoos() the AnnotationDrivenStaticEntityMockingControl will have injected your mock entity manager as Foo.entityManager() is a static method.

    Also note that if fs.getFoos() calls other static methods on Entity classes like Foo and Bar, they must also be specified as part of this test case.

    So say for example Foo had a static find method called "getAllBars(Long fooId)" which gets called when fs.getFoos() get called, then you would need to do the following in order to make AnnotationDrivenStaticEntityMockingControl work.

    @Test
    public void testFoo() {
      // call the static method that gets called by the method being tested in order to
      // "record" it and then set the expected response when it is replayed during the test
      Foo.entityManager();
      MockEntityManager expectedEntityManager = new MockEntityManager() {
        // TODO override what method you need to return whatever object you test needs
      };
      AnnotationDrivenStaticEntityMockingControl.expectReturn(expectedEntityManager);
    
      // call the static method that gets called by the method being tested in order to
      // "record" it and then set the expected response when it is replayed during the test
      Long fooId = 1L;
      Foo.findAllBars(fooId);
      List expectedBars = new ArrayList();
      expectedBars.add(new Bar(1));
      expectedBars.add(new Bar(2));
      AnnotationDrivenStaticEntityMockingControl.expectReturn(expectedBars);
    
      FooService fs = new FooServiceImpl();
      Set foos = fs.getFoos();
    }
    

    Remember the AnnotationDrivenStaticEntityMockingControl must be in the same order that fs.getFoos() calls its static methods.

提交回复
热议问题