java.lang.RuntimeException: no such layout layout/main

377 views
Skip to first unread message

ksncho

unread,
Nov 19, 2010, 10:48:31 AM11/19/10
to robolectric
I am trying to make a Robolectric test project for HelloAndroid
project which is made by eclipse.

But When I run the test project, test case fails with an error message
like “java.lang.RuntimeException: no such layout layout/main”.
I know there is a such layout.

I have no idea what to do next.
What should I do next? plz help me.

Michael Portuesi

unread,
Nov 19, 2010, 1:00:05 PM11/19/10
to robolectric
Odds are, the RobolectricTestRunner class cannot find the resources
for your main application.

One thing you can try is to make a subclass of
RobolectricTestRunner. Override the constructor, and pass in paths
to your main application and resource folder:

public class RobolectricMyAppTestRunner extends RobolectricTestRunner
{

public RobolectricMyAppTestRunner(Class testClass ) throws
InitializationError {
super(testClass, "path/to/your/application", "path/to/your/
application/resources" );
}

}

Then use this test runner for your test cases:

@RunWith(RobolectricMyAppTestRunner.class)
public class MyActivityTest {
// ...
}

M.

Phil

unread,
Nov 19, 2010, 1:29:37 PM11/19/10
to robolectric
The most likely cause for this is that the Working Directory is not
set up for the test run. In order for Robolectric to run tests on code
from a separate Android project it is necessary to set up a separate
JUnit run configuration and use the workspace that you are testing as
the Working Directory under the Arguments tab, making sure to use the
Eclipse JUnit launcher instead of the one for Android. Here's how to
do it:

* Main Menu -> Run -> Run Configurations...
* add a new run configuration under JUnit (not Android JUnit Test), by
right-clicking on the "JUnit" item in the tree control and selecting
"New"
* select the "Run all tests in the selected project, package or source
folder:" radio button, and put the folder containing your test code in
the associated edit box ("test" for the RobolectricSample project)
* select the "Arguments" tab
(this is where we will directly address your issue)
* at the bottom under "Working Directory", select the "Other" radio
button
* click the "Workspace..." button and select the Android project that
you are running tests against (the HelloAndroid project in your case)
* at the very, very bottom of the page there may be a prompt,
"Multiple launchers available -- Select one...", click the link, check
"Use configuration specific settings", and select "Eclipse JUnit
Launcher"
* click "Run" and your configuration will be saved and your tests run.

Let us know if this fixes the problem for you,

Phil


On Nov 19, 7:48 am, ksncho <ksn...@gmail.com> wrote:

ksncho

unread,
Nov 19, 2010, 7:41:03 PM11/19/10
to robolectric
Thanks for quick reply. Michael.

But I have tried that already and same error occurred.

ksncho

unread,
Nov 19, 2010, 8:02:49 PM11/19/10
to robolectric
Thanks for reply, Phil.

I tried what you explained but nothing changed.

This is the projects which I am working on.

http://chumbal.net/AndroidProjects.zip

Sorry to bother you.

Phil Goodwin

unread,
Nov 19, 2010, 8:28:05 PM11/19/10
to robol...@googlegroups.com
I'll look at it.

2010/11/19 ksncho <ksn...@gmail.com>

Phil Goodwin

unread,
Nov 19, 2010, 9:01:03 PM11/19/10
to robol...@googlegroups.com
Wish I'd been pairing with you when you wrote this: you've got a local variable 'm' in SetUp() that is preventing the field of the same name from being set. If you remove the type declaration from that variable it references the field instead and the test passes.

2010/11/19 Phil Goodwin <ph...@pivotalsf.com>

ksncho

unread,
Nov 20, 2010, 2:34:00 AM11/20/10
to robolectric
I arranged source code before uploading it and made mistake not erase
that declaration. sorry for that.

even though I erase that declaration, It's same.

My code is exactly same as that of Robolectric Welcome page.
I copy that code and paste it, and only change Activity declaration to
Main which is the start activity of HelloAndroid Project.

as follows..
========================================================
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
private Main activity;

@Before
public void setUp() throws Exception {
activity = new Main();
activity.onCreate(null);
}

@Test
public void shouldUpdateResultsWhenButtonIsClicked() throws
Exception {

}
}
=========================================================





On 11월20일, 오전11시01분, Phil Goodwin <p...@pivotalsf.com> wrote:
> Wish I'd been pairing with you when you wrote this: you've got a local
> variable 'm' in SetUp() that is preventing the field of the same name from
> being set. If you remove the type declaration from that variable it
> references the field instead and the test passes.
>
> 2010/11/19 Phil Goodwin <p...@pivotalsf.com>

Phil Goodwin

unread,
Nov 22, 2010, 4:44:26 PM11/22/10
to robol...@googlegroups.com
You still need to set up a run configuration in order to make the working directory point at the root of your main project. I did this myself on Friday and your test passed for me. 

2010/11/19 ksncho <ksn...@gmail.com>

ksncho

unread,
Nov 23, 2010, 6:14:54 AM11/23/10
to robolectric
I saw new Quick Start for Eclipse article at Robolectric Site and It
was very helpful.
I appreciate that.

I made new Projects and succeed the test given by you as following.
===================================================================
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

@Test
public void shouldHaveHappySmiles() throws Exception {
MyActivity activity = new MyActivity();
assertThat(activity.getResources().getString(R.string.hello),
equalTo("Hello World, MyActivity!"));
}
}
===================================================================

but when I add my own code to it as following, I got error message.
"java.lang.RuntimeException: no such layout layout/main" again.
===================================================================
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

@Test
public void shouldHaveHappySmiles() throws Exception {
MyActivity activity = new MyActivity();
activity.onCreate(null); <---- added code.
assertThat(activity.getResources().getString(R.string.hello),
equalTo("Hello World, MyActivity!"));
}
}
===================================================================

I definitely need to use "activity.onCreate(null)" for my project.

If I don't call that function. I got "java.lang.RuntimeException: you
should have called setContentView() first" error message.

Please let me know how I can implement "activity.onCreate(null)" with
your given code without any errors.

Since you have posted sample code "activity.onCreate(null)" in it at
welcome page (http://pivotal.github.com/robolectric/index.html), I can
suspect two things

(1) I am using "activity.onCreate(null)" in wrong way
(2) "activity.onCreate(null)" should meet some condition for its use
which i don't or you haven't told me yet.

Please help me out.

Thanks in advanced ~~~

P.S. I have completed setting with "Run Configuration" you told me
to.

Phil Goodwin

unread,
Nov 23, 2010, 1:35:57 PM11/23/10
to robol...@googlegroups.com
We've updated the guide again in order to address this. The problem is that in Eclipse there are multiple ways to launch tests and you will need a Run/Debug Configuration for each of the ones that you use. The instructions on the page should guide you through this. Any time you see that "no such layout..." exception you should immediately suspect that the test has been run without a configuration that points it to the correct working directory.

Hope that helps,

Phil

2010/11/23 ksncho <ksn...@gmail.com>

ksncho

unread,
Nov 23, 2010, 9:42:48 PM11/23/10
to robolectric
When I don't set correct woking path at Run/Debug Configuration,
"java.io.FileNotFoundException" error occur not "no such layout"
error.

I made a video clip of my setting up robolectric projects.

http://www.youtube.com/watch?v=WZGSWCj0KPw

I am so sorry to bother you too many times.
plz forgive me.

Phil Goodwin

unread,
Nov 24, 2010, 8:49:49 PM11/24/10
to robol...@googlegroups.com
We watched your video a couple of times, but we couldn't see where things went wrong. Maybe the Android jars should be lower in the classpath?

We followed the instructions again and then added the call to activity.onCreate(null); at the same point that you did and the tests still passed. I've zipped up the resulting projects and attached them here. Hopefully that'll get you going.

Phil

2010/11/23 ksncho <ksn...@gmail.com>

ksncho

unread,
Nov 24, 2010, 11:29:39 PM11/24/10
to robolectric
Thanks phil. but I can't find zipped projects. where can I find it??

Tyler Schultz

unread,
Nov 24, 2010, 11:43:15 PM11/24/10
to robol...@googlegroups.com
ksncho, 

Would it be possible for you to include the source jar in your project and debug into com.xtremelabs.robolectric.res.ResourceLoader, line 47?  This is where the layout files are loaded and parsed, maybe you'll see something that can help us understand what is going wrong.

--Tyler


2010/11/24 Phil Goodwin <ph...@pivotalsf.com>

ksncho

unread,
Nov 25, 2010, 12:26:55 AM11/25/10
to robolectric
I captured screenshot of eclipse.
Is it helpful??

Here is the link of the screenshot.
http://cfile25.uf.tistory.com/original/207890044CEDF2E83357E3




On 11월25일, 오후1시43분, Tyler Schultz <tylerschu...@gmail.com> wrote:
> ksncho,
>
> Would it be possible for you to include the source jar in your project and
> debug into com.xtremelabs.robolectric.res.ResourceLoader, line 47? This is
> where the layout files are loaded and parsed, maybe you'll see something
> that can help us understand what is going wrong.
>
> --Tyler
>
> 2010/11/24 Phil Goodwin <p...@pivotalsf.com>

ksncho

unread,
Nov 26, 2010, 8:56:30 AM11/26/10
to robolectric
I solved this problem.
I am not sure this is a right way, but hope it helps phil.

I changed the constructer of ResourceLoader like as follows.
####################################################################
if (resourceDir != null) {
DocumentLoader resourcesDocumentLoader = new
DocumentLoader(stringResourceLoader, stringArrayResourceLoader,
colorResourceLoader, attrResourceLoader);
resourcesDocumentLoader.loadResourceXmlDir(new File(resourceDir,
"values"));

viewLoader = new ViewLoader(resourceExtractor,
stringResourceLoader, attrResourceLoader);
DocumentLoader viewDocumentLoader = new
DocumentLoader(viewLoader);

/*
File[] layoutDirs = resourceDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getPath().contains("/layout");
}
});
*/

File layoutDirs = new File("res/layout");

viewDocumentLoader.loadResourceXmlDirs(layoutDirs);
} else {
viewLoader = null;
}
###################################################################



On 11월25일, 오후2시26분, ksncho <ksn...@gmail.com> wrote:
> I captured screenshot of eclipse.
> Is it helpful??
>
> Here is the link of the screenshot.http://cfile25.uf.tistory.com/original/207890044CEDF2E83357E3
> ...
>
> 추가 정보 >>

Phil Goodwin

unread,
Nov 28, 2010, 10:33:49 PM11/28/10
to robol...@googlegroups.com
I think that it might work to change "/layout" to File.separator + "layout"

2010/11/26 ksncho <ksn...@gmail.com>

ksncho

unread,
Nov 29, 2010, 1:15:25 AM11/29/10
to robolectric
Thanks Phil.

I tried what you said and It works well.
Would you mind applying this to (maybe) 0.9.3?
> ...
>
> 추가 정보 >>

lsim001

unread,
Dec 3, 2010, 4:36:33 PM12/3/10
to robolectric
I am having the same problem as well. I've followed the instructions
on http://pivotal.github.com/robolectric/eclipse-quick-start.html a
couple of times but still can't get Robolectric to work if i need to
call onCreate for my app.

Tyler Schultz

unread,
Dec 3, 2010, 8:10:03 PM12/3/10
to robol...@googlegroups.com
We just pushed a new release that should fix this problem 0.9.3.  We're short on windows boxes, so we're a bit behind on whats working and not working for the windows population.  Let me know if it doesn't work for you.

--Tyler

2010/12/3 lsim001 <lim...@gmail.com>
Reply all
Reply to author
Forward
0 new messages