Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: beanContainer.getBeanByType(..) works from Main but not from test

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default beanContainer.getBeanByType(..) works from Main but not from test

    Hi,

    I have a factory class that returns a bean fine using org.cdisource.beancontainer.BeanContainer calling ...
    beanContainer.getBeanByType(..)
    ... when I run it from a ..Main class.
    If I run the same lines of code in a test I get ...

    org.cdisource.beancontainer.BeanNotFoundException: Could not locate a bean of type xxxxx

    Is there some step I've missed to get this working from a unit test (.. really an integration test - strictly speaking)?
    I have a beans.xml in src/main/resources/META-INF
    I've tried putting the same in src/test/resources/META-INF - but this makes no difference.
    As far as I understand though this shouldn't be necessary anyway as there is no CDI use in the test itself - only in the factory class that is used in the test.

    I've compared the runtime and compile classpaths for main and test and apart from the mocking related dependencies there is no difference.

    Could someone give me any pointers of where to look next?

    Regards

    E


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: beanContainer.getBeanByType(..) works from Main but not from test

    Where does the code look for that item when it is executed? Is there a path that need to be set or a file moved so that it can be found?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: beanContainer.getBeanByType(..) works from Main but not from test

    Thanks - how would I find out where the beanContainer looks when run from a main class and when run from the test?

    The factory is in a package like ...
    com.abc.zyx.stuff
    - this contains
    - MyFactoryImpl.java
    - and also the interface MyServiceBean.java
    The bean I'm trying to inject is in ...
    com.abc.zyx.stuff.mybeans
    - this contains the bean MyServiceBeanImpl.java

    The tests are under "Test Packages" in Netbeans IDE but are also in the package ..
    com.abc.zyx.stuff.
    I have a main (which I can run successfully - using beanContainer.getBeanByType in MyFactoryImpl and giving me an instance of MyServiceBeanImpl) in
    com.abc.zyx.main

    I also tried adding another main class to the test packages and this works when I run it - but the unit tests in the same package give me the BeanNotFoundException.

    Any ideas?

    thanks

    E

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: beanContainer.getBeanByType(..) works from Main but not from test

    How do you execute the code?
    Is it by a command on the commandline
    or is it buried inside of an IDE?

    I don't know anything about configuring an IDE.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: beanContainer.getBeanByType(..) works from Main but not from test

    I was running the main from Netbeans IDE. I can also run the test from Maven but I don't know how to configure maven's pom file yet.
    I'll try to see if I can run this from the command line. Thanks for the questions. I keep hitting a brick wall on this one and you are giving me new things to think about.

Similar Threads

  1. Replies: 9
    Last Post: September 15th, 2013, 02:48 PM
  2. Replies: 4
    Last Post: September 3rd, 2012, 02:47 AM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. dunno how to test this code so i dont know if it works help please
    By jonathanfox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 24th, 2012, 04:43 AM